Mercurial > hg > horiscopes
comparison V4/synastry.py @ 19:ae220e89cb3a
fixing parse bugs, and angle calculation bugs
author | DaveM |
---|---|
date | Tue, 06 Mar 2018 17:25:38 +0000 |
parents | b11cff4b7f83 |
children | a5b8e2b91d8f |
comparison
equal
deleted
inserted
replaced
18:155126861c07 | 19:ae220e89cb3a |
---|---|
1 import random | 1 import random |
2 import csv | 2 import csv |
3 import re | 3 import re |
4 import requests | |
5 import time | |
6 from bs4 import BeautifulSoup | |
7 | |
4 import pdb | 8 import pdb |
5 | 9 |
6 class compatibility: | 10 class compatibility: |
7 def __init__(self): | 11 def __init__(self): |
8 self.rules = [] | 12 self.rules = [] |
29 text.append(line.strip()) | 33 text.append(line.strip()) |
30 return text | 34 return text |
31 | 35 |
32 def parseCompatRules(self,filename): | 36 def parseCompatRules(self,filename): |
33 comList = self.readFile(filename) | 37 comList = self.readFile(filename) |
34 # compat = {} | |
35 for com in comList: | 38 for com in comList: |
36 c = com.split(',') | 39 c = com.split(',') |
37 # print c | |
38 if len(c) == 4: | 40 if len(c) == 4: |
39 if len(c[1]) > 1: | 41 if len(c[1]) > 1: |
40 aspectList = [] | 42 aspectList = [] |
41 planetTuple = tuple(sorted((c[0].lower(),c[2].lower()))) | 43 planetTuple = tuple(sorted((c[0].lower(),c[2].lower()))) |
42 score = c[3] | 44 score = int(c[3]) |
43 for subset in c[1].split(' '): | 45 for subset in c[1].split(' '): |
44 aspectList.append(subset) | 46 aspectList.append(subset) |
45 elif len(c[1]) == 0 : | 47 elif len(c[1]) == 0 : |
46 if 'house' not in c[2]: | 48 if 'house' not in c[2]: |
47 print 'some error where rule is not house' | 49 print 'some error where rule is not house' |
50 if len(houseNo) != 1: | 52 if len(houseNo) != 1: |
51 print 'multiple house numbers found - ERROR' | 53 print 'multiple house numbers found - ERROR' |
52 aspectList = int(houseNo[0]) | 54 aspectList = int(houseNo[0]) |
53 score = int(c[3]) | 55 score = int(c[3]) |
54 self.addRule(planetTuple,aspectList,score) | 56 self.addRule(planetTuple,aspectList,score) |
55 # return compat | |
56 # pdb.set_trace() | |
57 | 57 |
58 def calcCompatibility(self, horiscope): | 58 def calcCompatibility(self, horiscope): |
59 score = 0 | 59 score = 0 |
60 for r in self.rules: | 60 for r in self.rules: |
61 if 'vertex' in r.planet: | 61 if 'vertex' in r.planet: |
76 if aspect is not None: | 76 if aspect is not None: |
77 if aspect in r.aspect: | 77 if aspect in r.aspect: |
78 score += r.score | 78 score += r.score |
79 return score | 79 return score |
80 | 80 |
81 | |
81 | 82 |
82 | 83 |
83 class compatibilityRule: | 84 class compatibilityRule: |
84 def __init__(self,uniqueId,planetTuple,aspectList,score): | 85 def __init__(self,uniqueId,planetTuple,aspectList,score): |
85 self.id = uniqueId | 86 self.id = uniqueId |
134 self.p_dob = self.p_dob[1:-1].split(',') | 135 self.p_dob = self.p_dob[1:-1].split(',') |
135 if type(self.tob) is str: | 136 if type(self.tob) is str: |
136 self.tob = self.tob[1:-1].split(',') | 137 self.tob = self.tob[1:-1].split(',') |
137 if type(self.p_tob) is str: | 138 if type(self.p_tob) is str: |
138 self.p_tob = self.p_tob[1:-1].split(',') | 139 self.p_tob = self.p_tob[1:-1].split(',') |
139 # pdb.set_trace() | 140 |
140 | |
141 # print dataDict['pDOB'] | |
142 | |
143 self.payload = {'send_calculation':'1', #Req | 141 self.payload = {'send_calculation':'1', #Req |
144 'muz_narozeni_den':self.dob[0], | 142 'muz_narozeni_den':self.dob[0], |
145 'muz_narozeni_mesic':self.dob[1], | 143 'muz_narozeni_mesic':self.dob[1], |
146 'muz_narozeni_rok':self.dob[2], | 144 'muz_narozeni_rok':self.dob[2], |
147 'muz_narozeni_hodina':self.tob[0], | 145 'muz_narozeni_hodina':self.tob[0], |
186 'zena_narozeni_timezone_dst_form':'auto', | 184 'zena_narozeni_timezone_dst_form':'auto', |
187 'switch_interpretations':'0', | 185 'switch_interpretations':'0', |
188 'house_system':'placidus', | 186 'house_system':'placidus', |
189 'uhel_orbis':'#tabs_redraw'} | 187 'uhel_orbis':'#tabs_redraw'} |
190 | 188 |
189 def requestURL(self): | |
190 self.resp = requests.get(self.url, params=self.payload) | |
191 time.sleep(5) | |
192 return self.resp | |
193 | |
194 def parsePage(self): | |
195 gotLocation = 0 | |
196 self.horiscope = planetPositions() | |
197 soup = BeautifulSoup(self.resp.content, 'lxml') | |
198 tcCell = soup.find_all('div', attrs={'class':'right-sedy-banner-svetlejsi'}) | |
199 for cell in tcCell: | |
200 if "Planets in partner's house" in cell.get_text(): | |
201 gotLocation = 1 | |
202 divList = cell.find_all('div') | |
203 for i in range(len(divList)): | |
204 planetName = divList[i].getText().lower().strip().replace(':','').split(' ')[0] | |
205 if planetName in planetPositions.planetNames: | |
206 if gotLocation and not '/' in planetName: | |
207 self.horiscope.planets[planetName].setHouse(divList[i+2].getText(),divList[i+4].getText()) | |
208 else: | |
209 self.horiscope.planets[planetName].setLocation(divList[i+2].getText(),divList[i+1].img.attrs['alt'],0) | |
210 self.horiscope.planets[planetName].setLocation(divList[i+4].getText(),divList[i+3].img.attrs['alt'],1) | |
211 return self.horiscope | |
212 | |
213 | |
191 | 214 |
192 class planetRelation: | 215 class planetRelation: |
193 noHouseList = ['asc','ic','dsc','mc','asc/mc','sun/moon'] | 216 noHouseList = ['asc','ic','dsc','mc','asc/mc','sun/moon'] |
217 zodiacAngle = {'aries':0,'taurus':30,'gemini':60,'cancer':90,'leo':120,'virgo':150,'libra':180,'scorpio':210,'sagittarius':240,'capricorn':270,'aquarius':300,'pisces':330} | |
218 | |
194 def __init__(self,planetName): | 219 def __init__(self,planetName): |
195 self.name = planetName | 220 self.name = planetName |
196 self.angleA = None | 221 self.angleA = None |
197 self.angleB = None | 222 self.angleB = None |
198 if planetName not in planetRelation.noHouseList: | 223 if planetName not in planetRelation.noHouseList: |
199 self.houseA = None | 224 self.houseA = None |
200 self.houseB = None | 225 self.houseB = None |
201 | 226 |
202 def setLocation(self,A,B): | 227 def setLocation(self,value,sign,isB): |
203 self.angleA = float('.'.join(A.encode('ascii','replace').split('?'))[:-1]) | 228 signVal = planetRelation.zodiacAngle[sign.lower()] |
204 self.angleB = float('.'.join(B.encode('ascii','replace').split('?'))[:-1]) | 229 signVal+= float('.'.join(value.encode('ascii','replace').split('?'))[:-1]) |
205 # print self.angleA,self.angleB | 230 # print self.name,sign.lower(),planetRelation.zodiacAngle[sign.lower()],value,signVal |
206 | 231 if not isB: |
232 self.angleA = signVal | |
233 else: | |
234 self.angleB = signVal | |
235 | |
207 def setHouse(self,A,B): | 236 def setHouse(self,A,B): |
208 self.houseA = int(A.encode('ascii','ignore')) | 237 self.houseA = int(A.encode('ascii','ignore')) |
209 self.houseB = int(B.encode('ascii','ignore')) | 238 self.houseB = int(B.encode('ascii','ignore')) |
210 # print self.houseA,self.houseB | |
211 | 239 |
212 def test_random(self): | 240 def test_random(self): |
213 self.angleA = random.random()*360 | 241 self.angleA = random.random()*360 |
214 self.angleB = random.random()*360 | 242 self.angleB = random.random()*360 |
215 | 243 |
216 | 244 |
217 class planetPositions: | 245 class planetPositions: |
218 aspectDict = {'conjunction':0,'semi-square':45,'sextile':60,'square':90,'trine':120,'opposite':180} | 246 aspectDict = {'conjunction':0,'semi-square':45,'sextile':60,'square':90,'trine':120,'opposite':180} |
247 # zodiacAngle = {'aries':0,'taurus':30,'gemini':60,'cancer':90,'leo':120,'virgo':150,'libra':180,'scorpio':210,'sagittarius':240,'capricorn':270,'aquarius':300,'pisces':330} | |
248 # Taken from https://en.wikipedia.org/wiki/Astrological_sign, with reference from https://en.wikipedia.org/wiki/Astrological_symbols#Signs_of_the_zodiac | |
219 planetNames = ['sun','moon','mercury','venus','mars','jupiter','saturn','uranus','neptune','pluto','node','lilith','chiron','asc','ic','dsc','mc','asc/mc','sun/moon'] | 249 planetNames = ['sun','moon','mercury','venus','mars','jupiter','saturn','uranus','neptune','pluto','node','lilith','chiron','asc','ic','dsc','mc','asc/mc','sun/moon'] |
220 def __init__(self): | 250 def __init__(self): |
221 self.planets = {} | 251 self.planets = {} |
222 for p in planetPositions.planetNames: | 252 for p in planetPositions.planetNames: |
223 self.planets[p] = planetRelation(p) | 253 self.planets[p] = planetRelation(p) |
226 def test_random(self): | 256 def test_random(self): |
227 for planet in self.planets: | 257 for planet in self.planets: |
228 self.planets[planet].test_random() | 258 self.planets[planet].test_random() |
229 | 259 |
230 def calcAngle(self,componentA,componentB): | 260 def calcAngle(self,componentA,componentB): |
231 self.angle = max(self.planets[componentA].angleA,self.planets[componentB].angleB) - min(self.planets[componentA].angleA,self.planets[componentB].angleB) | 261 self.angle = abs(self.planets[componentA].angleA - self.planets[componentB].angleB) |
232 self.angleRange = self.angle-10,self.angle+10 | 262 self.angleRange = self.angle-10,self.angle+10 |
233 | 263 |
234 def calcAspect(self,componentA,componentB): | 264 def calcAspect(self,componentA,componentB): |
235 self.calcAngle(componentA,componentB) | 265 self.calcAngle(componentA,componentB) |
266 # print componentA,componentB,self.angle,self.planets[componentA].angleA,self.planets[componentB].angleB | |
236 for aspect in planetPositions.aspectDict: | 267 for aspect in planetPositions.aspectDict: |
237 if self.angleRange[0] < planetPositions.aspectDict[aspect] and self.angleRange[1] > planetPositions.aspectDict[aspect]: | 268 if self.angleRange[0] < planetPositions.aspectDict[aspect] and self.angleRange[1] > planetPositions.aspectDict[aspect]: |
238 aspectDiff = abs(self.angle - planetPositions.aspectDict[aspect]) | 269 # print aspect#,componentA,componentB,self.angle |
270 aspectDiff = round(abs(self.angle - planetPositions.aspectDict[aspect]),2) | |
239 self.aspect[componentA,componentB] = (aspect,aspectDiff) | 271 self.aspect[componentA,componentB] = (aspect,aspectDiff) |
240 return aspect,aspectDiff | 272 return aspect,aspectDiff |
241 return None,None | 273 return None,None |
242 | 274 |
243 def isInHouse(self,component,houseNo): | 275 def isInHouse(self,component,houseNo): |