diff V4/runme.py @ 19:ae220e89cb3a

fixing parse bugs, and angle calculation bugs
author DaveM
date Tue, 06 Mar 2018 17:25:38 +0000
parents b11cff4b7f83
children 0264a7888d54
line wrap: on
line diff
--- a/V4/runme.py	Mon Mar 05 14:23:45 2018 +0000
+++ b/V4/runme.py	Tue Mar 06 17:25:38 2018 +0000
@@ -3,8 +3,8 @@
 
 import dParse as dp
 import synastry as syn
-import requests
-import time
+# import requests
+# import time
 import csv
 import pdb
 import os
@@ -12,23 +12,23 @@
 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 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'))
@@ -40,10 +40,49 @@
 		# pdb.set_trace()
 	return people
 
-def requestURL(url,payload):
-	r = requests.get(url, params=payload)
-	time.sleep(5)
-	return r
+def uniquify(itemList):
+	keyDict = {}
+	for item in itemList:
+		keyDict[item] = 1
+	return keyDict.keys()
+
+def outputPeople(filename,people):
+	with open(filename, "wb") as csv_file:
+		dictKeys = []
+		for person in people:
+			if person.issue is None:
+				person.horiscope.calcAllAspects()
+				dictKeys += person.horiscope.aspect.keys()
+		dictKeys = uniquify(dictKeys)
+		writer = csv.DictWriter(csv_file, ['id']+dictKeys)
+		writer.writeheader()
+		for person in people:
+			if person.issue is None:
+				tempDict = {'id':person.id}
+				tempDict.update(person.horiscope.aspect)
+				# pdb.set_trace()
+				writer.writerow(tempDict)
+
+def outputScores(filename,people):
+	with open(filename, "wb") as csv_file:
+		csv_file.write('Person ID, Score \n')
+		for person in people:
+			if person.issue is None:
+				csv_file.write(str(person.id)+','+str(person.score)+'\n')
+			else:
+				csv_file.write(str(person.id)+','+str(person.issue)+'\n')
+
+def outputIssues(filename,people):
+	with open(filename, "wb") as csv_file:
+		csv_file.write('Person ID, Issue \n')
+		for person in people:
+			if person.issue is not None:
+				csv_file.write(str(person.id)+','+str(person.issue)+'\n')
+
+# def requestURL(url,payload):
+# 	r = requests.get(url, params=payload)
+# 	time.sleep(5)
+# 	return r
 
 def loadPick(filename):
 	with open(filename, 'rb') as handle:
@@ -62,8 +101,8 @@
 		else:
 			print 'parsing person '+ person.id
 			person.makePayload()
-			person.resp = requestURL(person.url,person.payload)
-			person.horiscope = parsePage(person.resp)
+			person.requestURL()
+			person.horiscope = person.parsePage()
 			# person.horiscope.printPositions()
 		if saveFile is not None:
 				savePick(saveFile,people)
@@ -102,15 +141,24 @@
 		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 not os.path.exists('fullResults.pick'):
+		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'
+					# pdb.set_trace()
+					f = open('issues/'+str(person.id)+'.html','w')
+					f.write(person.resp.content)
+					f.close()
+		savePick('fullResults.pick',people)
+	else:
+		people = loadPick('fullResults.pick')
+		outputPeople('fullResult.csv',people)
+		outputScores('scores.csv',people)
 
 if __name__ == "__main__":
 	_main()