view V4/runme.py @ 16:b11cff4b7f83

implement compatibility scores
author DaveM
date Mon, 05 Mar 2018 14:22:09 +0000
parents 50a95089414d
children ae220e89cb3a
line wrap: on
line source
#!/usr/bin/env python


import dParse as dp
import synastry as syn
import requests
import time
import csv
import pdb
import os
import pickle
import sys
from bs4 import BeautifulSoup

def parsePage(resp):
	gotLocation = 0
	horiscope = syn.planetPositions()
	soup = BeautifulSoup(resp.content, 'lxml')
	tcCell = soup.find_all('div', attrs={'class':'right-sedy-banner-svetlejsi'})
	for cell in tcCell:
		if "Planets in partner's house" in cell.get_text():
			gotLocation = 1
		divList = cell.find_all('div')
		for i in range(len(divList)):
			planetName = divList[i].getText().lower().strip().replace(':','').split(' ')[0]
			if planetName in syn.planetPositions.planetNames:
				if gotLocation and not '/' in planetName:
					horiscope.planets[planetName].setHouse(divList[i+2].getText(),divList[i+4].getText())
				else:
					horiscope.planets[planetName].setLocation(divList[i+2].getText(),divList[i+4].getText())
	return horiscope

def makePeople(filename):
	stream = csv.DictReader(open(filename,'rb'))
	dictList = []
	people = []
	for line in stream:
		thisPerson = syn.Person(dp.regulateData(line))
		people.append(thisPerson)
		# pdb.set_trace()
	return people

def requestURL(url,payload):
	r = requests.get(url, params=payload)
	time.sleep(5)
	return r

def loadPick(filename):
	with open(filename, 'rb') as handle:
		b = pickle.load(handle)
	return b

def savePick(filename,data):
	with open(filename, 'wb') as handle:
		pickle.dump(data,handle)

def parseHoriscope(people,saveFile):
	for person in people:
		issue = person.identifyIssues()
		if issue is not None:
			print 'SKIPPING person '+ person.id + ' error with ' + issue 
		else:
			print 'parsing person '+ person.id
			person.makePayload()
			person.resp = requestURL(person.url,person.payload)
			person.horiscope = parsePage(person.resp)
			# person.horiscope.printPositions()
		if saveFile is not None:
				savePick(saveFile,people)

# def testMain():
# 	pickFile = 'outData.pick'
# 	restartDataFile = 0
# 	if(restartDataFile):
# 		# people = makePeople('individuals.csv')
# 		# savePick(pickFile,people)
# 		people = loadPick(pickFile)
# 		parseSaveFile = pickFile.split('.')[0]+'_collect.pick'
# 		parseHoriscope(people,parseSaveFile)
# 	else:
# 		people = loadPick('outData_collect.pick')
# 	comp = syn.compatibility()
# 	comp.parseCompatRules('compatibilityRules.csv')
# 	for person in people:
# 		if person.issue is None:
# 			person.score = comp.calcCompatibility(person.horiscope)
# 			if person.score is None:
# 				person.issue = 'None Planet Locations'
# 			else:
# 				print person.id,person.score
# 	pdb.set_trace()

def _main():
	pickFile = 'outData.pick'
	if not os.path.exists(pickFile):
		people = makePeople('individuals.csv')
		savePick(pickFile,people)
	else:
		people = loadPick(pickFile)
	parseSaveFile = pickFile.split('.')[0]+'_collect.pick'
	if not os.path.exists(parseSaveFile):
		parseHoriscope(people,parseSaveFile)
	else:
		people = loadPick(parseSaveFile)
	comp = syn.compatibility()
	comp.parseCompatRules('compatibilityRules.csv')
	for person in people:
		person.score = None
		if person.issue is None:
			person.score = comp.calcCompatibility(person.horiscope)
			if person.score is None:
				person.issue = 'None Planet Locations'
	savePick('fullResults.pick',people)

if __name__ == "__main__":
	_main()