DaveM@13
|
1 #!/usr/bin/env python
|
DaveM@15
|
2
|
DaveM@15
|
3
|
DaveM@13
|
4 import dParse as dp
|
DaveM@13
|
5 import synastry as syn
|
DaveM@19
|
6 # import requests
|
DaveM@19
|
7 # import time
|
DaveM@13
|
8 import csv
|
DaveM@13
|
9 import pdb
|
DaveM@13
|
10 import os
|
DaveM@13
|
11 import pickle
|
DaveM@15
|
12 import sys
|
DaveM@13
|
13 from bs4 import BeautifulSoup
|
DaveM@13
|
14
|
DaveM@19
|
15 # def parsePage(resp):
|
DaveM@19
|
16 # gotLocation = 0
|
DaveM@19
|
17 # horiscope = syn.planetPositions()
|
DaveM@19
|
18 # soup = BeautifulSoup(resp.content, 'lxml')
|
DaveM@19
|
19 # tcCell = soup.find_all('div', attrs={'class':'right-sedy-banner-svetlejsi'})
|
DaveM@19
|
20 # for cell in tcCell:
|
DaveM@19
|
21 # if "Planets in partner's house" in cell.get_text():
|
DaveM@19
|
22 # gotLocation = 1
|
DaveM@19
|
23 # divList = cell.find_all('div')
|
DaveM@19
|
24 # for i in range(len(divList)):
|
DaveM@19
|
25 # planetName = divList[i].getText().lower().strip().replace(':','').split(' ')[0]
|
DaveM@19
|
26 # if planetName in syn.planetPositions.planetNames:
|
DaveM@19
|
27 # if gotLocation and not '/' in planetName:
|
DaveM@19
|
28 # horiscope.planets[planetName].setHouse(divList[i+2].getText(),divList[i+4].getText())
|
DaveM@19
|
29 # else:
|
DaveM@19
|
30 # horiscope.planets[planetName].setLocation(divList[i+2].getText(),divList[i+4].getText())
|
DaveM@19
|
31 # return horiscope
|
DaveM@13
|
32
|
DaveM@13
|
33 def makePeople(filename):
|
DaveM@13
|
34 stream = csv.DictReader(open(filename,'rb'))
|
DaveM@13
|
35 dictList = []
|
DaveM@13
|
36 people = []
|
DaveM@13
|
37 for line in stream:
|
DaveM@13
|
38 thisPerson = syn.Person(dp.regulateData(line))
|
DaveM@13
|
39 people.append(thisPerson)
|
DaveM@13
|
40 # pdb.set_trace()
|
DaveM@13
|
41 return people
|
DaveM@13
|
42
|
DaveM@19
|
43 def uniquify(itemList):
|
DaveM@19
|
44 keyDict = {}
|
DaveM@19
|
45 for item in itemList:
|
DaveM@19
|
46 keyDict[item] = 1
|
DaveM@22
|
47 return sorted(keyDict.keys())
|
DaveM@19
|
48
|
DaveM@19
|
49 def outputPeople(filename,people):
|
DaveM@19
|
50 with open(filename, "wb") as csv_file:
|
DaveM@19
|
51 dictKeys = []
|
DaveM@19
|
52 for person in people:
|
DaveM@19
|
53 if person.issue is None:
|
DaveM@22
|
54 person.horiscope.makeAllAspectTreple()
|
DaveM@22
|
55 dictKeys += person.horiscope.aspectTreple.keys()
|
DaveM@19
|
56 dictKeys = uniquify(dictKeys)
|
DaveM@22
|
57 writer = csv.DictWriter(csv_file, ['id','score']+dictKeys)
|
DaveM@19
|
58 writer.writeheader()
|
DaveM@19
|
59 for person in people:
|
DaveM@19
|
60 if person.issue is None:
|
DaveM@22
|
61 tempDict = {'id':person.id,'score':person.score}
|
DaveM@22
|
62 tempDict.update(person.horiscope.aspectTreple)
|
DaveM@19
|
63 # pdb.set_trace()
|
DaveM@19
|
64 writer.writerow(tempDict)
|
DaveM@19
|
65
|
DaveM@19
|
66 def outputScores(filename,people):
|
DaveM@19
|
67 with open(filename, "wb") as csv_file:
|
DaveM@19
|
68 csv_file.write('Person ID, Score \n')
|
DaveM@19
|
69 for person in people:
|
DaveM@19
|
70 if person.issue is None:
|
DaveM@19
|
71 csv_file.write(str(person.id)+','+str(person.score)+'\n')
|
DaveM@19
|
72 else:
|
DaveM@19
|
73 csv_file.write(str(person.id)+','+str(person.issue)+'\n')
|
DaveM@19
|
74
|
DaveM@19
|
75 def outputIssues(filename,people):
|
DaveM@19
|
76 with open(filename, "wb") as csv_file:
|
DaveM@19
|
77 csv_file.write('Person ID, Issue \n')
|
DaveM@19
|
78 for person in people:
|
DaveM@19
|
79 if person.issue is not None:
|
DaveM@19
|
80 csv_file.write(str(person.id)+','+str(person.issue)+'\n')
|
DaveM@19
|
81
|
DaveM@19
|
82 # def requestURL(url,payload):
|
DaveM@19
|
83 # r = requests.get(url, params=payload)
|
DaveM@19
|
84 # time.sleep(5)
|
DaveM@19
|
85 # return r
|
DaveM@13
|
86
|
DaveM@23
|
87 # def loadPickUpdate(filename):
|
DaveM@22
|
88
|
DaveM@13
|
89 def loadPick(filename):
|
DaveM@13
|
90 with open(filename, 'rb') as handle:
|
DaveM@13
|
91 b = pickle.load(handle)
|
DaveM@13
|
92 return b
|
DaveM@13
|
93
|
DaveM@13
|
94 def savePick(filename,data):
|
DaveM@13
|
95 with open(filename, 'wb') as handle:
|
DaveM@13
|
96 pickle.dump(data,handle)
|
DaveM@13
|
97
|
DaveM@13
|
98 def parseHoriscope(people,saveFile):
|
DaveM@13
|
99 for person in people:
|
DaveM@15
|
100 issue = person.identifyIssues()
|
DaveM@15
|
101 if issue is not None:
|
DaveM@15
|
102 print 'SKIPPING person '+ person.id + ' error with ' + issue
|
DaveM@13
|
103 else:
|
DaveM@13
|
104 print 'parsing person '+ person.id
|
DaveM@22
|
105 if person.resp is None:
|
DaveM@22
|
106 print 'Posting Request for person '+ person.id
|
DaveM@22
|
107 person.makePayload()
|
DaveM@22
|
108 person.requestURL()
|
DaveM@22
|
109 print 'parsing person '+ person.id
|
DaveM@19
|
110 person.horiscope = person.parsePage()
|
DaveM@15
|
111 # person.horiscope.printPositions()
|
DaveM@15
|
112 if saveFile is not None:
|
DaveM@15
|
113 savePick(saveFile,people)
|
DaveM@13
|
114
|
DaveM@16
|
115 # def testMain():
|
DaveM@16
|
116 # pickFile = 'outData.pick'
|
DaveM@16
|
117 # restartDataFile = 0
|
DaveM@16
|
118 # if(restartDataFile):
|
DaveM@16
|
119 # # people = makePeople('individuals.csv')
|
DaveM@16
|
120 # # savePick(pickFile,people)
|
DaveM@16
|
121 # people = loadPick(pickFile)
|
DaveM@16
|
122 # parseSaveFile = pickFile.split('.')[0]+'_collect.pick'
|
DaveM@16
|
123 # parseHoriscope(people,parseSaveFile)
|
DaveM@16
|
124 # else:
|
DaveM@16
|
125 # people = loadPick('outData_collect.pick')
|
DaveM@16
|
126 # comp = syn.compatibility()
|
DaveM@16
|
127 # comp.parseCompatRules('compatibilityRules.csv')
|
DaveM@16
|
128 # for person in people:
|
DaveM@16
|
129 # if person.issue is None:
|
DaveM@16
|
130 # person.score = comp.calcCompatibility(person.horiscope)
|
DaveM@16
|
131 # if person.score is None:
|
DaveM@16
|
132 # person.issue = 'None Planet Locations'
|
DaveM@16
|
133 # else:
|
DaveM@16
|
134 # print person.id,person.score
|
DaveM@16
|
135 # pdb.set_trace()
|
DaveM@13
|
136
|
DaveM@13
|
137 def _main():
|
DaveM@13
|
138 pickFile = 'outData.pick'
|
DaveM@13
|
139 if not os.path.exists(pickFile):
|
DaveM@13
|
140 people = makePeople('individuals.csv')
|
DaveM@13
|
141 savePick(pickFile,people)
|
DaveM@13
|
142 else:
|
DaveM@13
|
143 people = loadPick(pickFile)
|
DaveM@13
|
144 parseSaveFile = pickFile.split('.')[0]+'_collect.pick'
|
DaveM@16
|
145 if not os.path.exists(parseSaveFile):
|
DaveM@16
|
146 parseHoriscope(people,parseSaveFile)
|
DaveM@16
|
147 else:
|
DaveM@16
|
148 people = loadPick(parseSaveFile)
|
DaveM@23
|
149 # if not os.path.exists('fullResults.pick'):
|
DaveM@23
|
150 if os.path.exists('fullResults.pick'):
|
DaveM@19
|
151 comp = syn.compatibility()
|
DaveM@19
|
152 comp.parseCompatRules('compatibilityRules.csv')
|
DaveM@19
|
153 for person in people:
|
DaveM@23
|
154 print person.id
|
DaveM@19
|
155 person.score = None
|
DaveM@19
|
156 if person.issue is None:
|
DaveM@19
|
157 person.score = comp.calcCompatibility(person.horiscope)
|
DaveM@19
|
158 if person.score is None:
|
DaveM@19
|
159 person.issue = 'None Planet Locations'
|
DaveM@19
|
160 # pdb.set_trace()
|
DaveM@19
|
161 f = open('issues/'+str(person.id)+'.html','w')
|
DaveM@19
|
162 f.write(person.resp.content)
|
DaveM@19
|
163 f.close()
|
DaveM@19
|
164 savePick('fullResults.pick',people)
|
DaveM@19
|
165 else:
|
DaveM@19
|
166 people = loadPick('fullResults.pick')
|
DaveM@21
|
167 outputPeople('fullResult.csv',people)
|
DaveM@21
|
168 outputScores('scores.csv',people)
|
DaveM@21
|
169 outputIssues('issues.csv',people)
|
DaveM@13
|
170
|
DaveM@13
|
171 if __name__ == "__main__":
|
DaveM@16
|
172 _main()
|