annotate 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
rev   line source
DaveM@13 1 import random
DaveM@16 2 import csv
DaveM@16 3 import re
DaveM@19 4 import requests
DaveM@19 5 import time
DaveM@19 6 from bs4 import BeautifulSoup
DaveM@19 7
DaveM@16 8 import pdb
DaveM@16 9
DaveM@16 10 class compatibility:
DaveM@16 11 def __init__(self):
DaveM@16 12 self.rules = []
DaveM@16 13 self.uniqueID = 0
DaveM@16 14 print 'Warning, need to fix [Vertex, Ascendant House, None Location] issues'
DaveM@16 15
DaveM@16 16 def addRule(self,planetTuple,aspectList,score):
DaveM@16 17 rule = compatibilityRule(self.uniqueID,planetTuple,aspectList,score)
DaveM@16 18 self.rules.append(rule)
DaveM@16 19 self.uniqueID += 1
DaveM@16 20
DaveM@16 21 def findRule(self,planet):
DaveM@16 22 ruleList = [r for r in self.rules if r.planet == planet]
DaveM@16 23 print ruleList
DaveM@16 24 return ruleList
DaveM@16 25
DaveM@16 26 def readFile(self,filename):
DaveM@16 27 text = []
DaveM@16 28 with open(filename) as fp:
DaveM@16 29 line = fp.readline()
DaveM@16 30 text.append(line.strip())
DaveM@16 31 while line:
DaveM@16 32 line = fp.readline()
DaveM@16 33 text.append(line.strip())
DaveM@16 34 return text
DaveM@16 35
DaveM@16 36 def parseCompatRules(self,filename):
DaveM@16 37 comList = self.readFile(filename)
DaveM@16 38 for com in comList:
DaveM@16 39 c = com.split(',')
DaveM@16 40 if len(c) == 4:
DaveM@16 41 if len(c[1]) > 1:
DaveM@16 42 aspectList = []
DaveM@16 43 planetTuple = tuple(sorted((c[0].lower(),c[2].lower())))
DaveM@19 44 score = int(c[3])
DaveM@16 45 for subset in c[1].split(' '):
DaveM@16 46 aspectList.append(subset)
DaveM@16 47 elif len(c[1]) == 0 :
DaveM@16 48 if 'house' not in c[2]:
DaveM@16 49 print 'some error where rule is not house'
DaveM@16 50 planetTuple = (c[0].lower(),'house')
DaveM@16 51 houseNo = re.findall('\d+', c[2])
DaveM@16 52 if len(houseNo) != 1:
DaveM@16 53 print 'multiple house numbers found - ERROR'
DaveM@16 54 aspectList = int(houseNo[0])
DaveM@16 55 score = int(c[3])
DaveM@16 56 self.addRule(planetTuple,aspectList,score)
DaveM@16 57
DaveM@16 58 def calcCompatibility(self, horiscope):
DaveM@16 59 score = 0
DaveM@16 60 for r in self.rules:
DaveM@16 61 if 'vertex' in r.planet:
DaveM@16 62 # print 'ERROR - catch Vertex issue'
DaveM@16 63 pass
DaveM@16 64 elif r.planet[1] == 'house' and r.planet[0] == 'asc':
DaveM@16 65 # print 'ERROR - catch Ascendant House issue'
DaveM@16 66 pass
DaveM@16 67 elif horiscope.planets[r.planet[0]].angleA is None:
DaveM@16 68 # print 'Error - None Location'
DaveM@16 69 return None
DaveM@16 70 elif r.planet[1] == 'house':
DaveM@16 71 if horiscope.isInHouse(r.planet[0],r.aspect):
DaveM@16 72 score += r.score
DaveM@16 73 else:
DaveM@16 74 # print 'query standard synastry'
DaveM@16 75 aspect,variance = horiscope.calcAspect(r.planet[0],r.planet[1])
DaveM@16 76 if aspect is not None:
DaveM@16 77 if aspect in r.aspect:
DaveM@16 78 score += r.score
DaveM@16 79 return score
DaveM@16 80
DaveM@19 81
DaveM@16 82
DaveM@16 83
DaveM@16 84 class compatibilityRule:
DaveM@16 85 def __init__(self,uniqueId,planetTuple,aspectList,score):
DaveM@16 86 self.id = uniqueId
DaveM@16 87 self.planet = planetTuple
DaveM@16 88 self.aspect = aspectList
DaveM@16 89 self.score = score
DaveM@13 90
DaveM@13 91 class Person:
DaveM@13 92 url = 'https://horoscopes.astro-seek.com/calculate-love-compatibility/'
DaveM@13 93 def __init__(self,personDict):
DaveM@13 94 # self. = planetPositions()
DaveM@13 95 self.id = personDict['ID']
DaveM@13 96 self.dob = personDict['DOB']
DaveM@13 97 self.tob = personDict['TOB']
DaveM@13 98 self.cob = personDict['COB']
DaveM@13 99 self.p_dob = personDict['pDOB']
DaveM@13 100 self.p_tob = personDict['pTOB']
DaveM@13 101 self.p_cob = personDict['pCOB']
DaveM@13 102 self.horiscope = None
DaveM@13 103
DaveM@15 104 def identifyIssues(self):
DaveM@15 105 if self.id is None:
DaveM@16 106 self.issue = 'id'
DaveM@15 107 elif self.dob is None:
DaveM@16 108 self.issue = 'dob'
DaveM@15 109 elif self.tob is None:
DaveM@16 110 self.issue = 'tob'
DaveM@15 111 elif self.cob is None:
DaveM@16 112 self.issue = 'cob'
DaveM@15 113 elif self.p_dob is None:
DaveM@16 114 self.issue = 'p_dob'
DaveM@15 115 elif self.p_tob is None:
DaveM@16 116 self.issue = 'p_tob'
DaveM@15 117 elif self.p_cob is None:
DaveM@16 118 self.issue = 'p_cob'
DaveM@15 119 else:
DaveM@16 120 self.issue = None
DaveM@16 121 return self.issue
DaveM@15 122
DaveM@13 123 def makePayload(self):
DaveM@13 124 if type(self.cob) is str:
DaveM@13 125 cob_0 = float(self.cob.split(',')[0][1:])
DaveM@13 126 cob_1 = float(self.cob.split(',')[1])
DaveM@13 127 self.cob = (cob_0,cob_1)
DaveM@13 128 if type(self.p_cob) is str:
DaveM@13 129 pcob_0 = float(self.p_cob.split(',')[0][1:])
DaveM@13 130 pcob_1 = float(self.p_cob.split(',')[1])
DaveM@13 131 self.p_cob = (pcob_0,pcob_1)
DaveM@13 132 if type(self.dob) is str:
DaveM@13 133 self.dob = self.dob[1:-1].split(',')
DaveM@13 134 if type(self.p_dob) is str:
DaveM@13 135 self.p_dob = self.p_dob[1:-1].split(',')
DaveM@13 136 if type(self.tob) is str:
DaveM@13 137 self.tob = self.tob[1:-1].split(',')
DaveM@13 138 if type(self.p_tob) is str:
DaveM@13 139 self.p_tob = self.p_tob[1:-1].split(',')
DaveM@19 140
DaveM@13 141 self.payload = {'send_calculation':'1', #Req
DaveM@13 142 'muz_narozeni_den':self.dob[0],
DaveM@13 143 'muz_narozeni_mesic':self.dob[1],
DaveM@13 144 'muz_narozeni_rok':self.dob[2],
DaveM@13 145 'muz_narozeni_hodina':self.tob[0],
DaveM@13 146 'muz_narozeni_minuta':self.tob[1],
DaveM@13 147 'muz_narozeni_city':'',
DaveM@13 148 'muz_narozeni_mesto_hidden':'Manually+place%3A+%C2%B0%27N%2C+%C2%B0%27E',#auto
DaveM@13 149 'muz_narozeni_stat_hidden':'XX',
DaveM@13 150 'muz_narozeni_podstat_kratky_hidden':'',
DaveM@13 151 'muz_narozeni_podstat_hidden':'',
DaveM@13 152 'muz_narozeni_podstat2_kratky_hidden':'',
DaveM@13 153 'muz_narozeni_podstat3_kratky_hidden':'',
DaveM@13 154 'muz_narozeni_input_hidden':'',
DaveM@13 155 'muz_narozeni_sirka_stupne':str(abs(self.cob[0])).split('.')[0],
DaveM@13 156 'muz_narozeni_sirka_minuty':str(float('0.'+str(self.cob[0]).split('.')[1])*60).split('.')[0],
DaveM@13 157 'muz_narozeni_sirka_smer': '1' if self.cob[0]<0 else '0', #address N Dir (0':'N',1':'S)
DaveM@13 158 'muz_narozeni_delka_stupne':str(abs(self.cob[1])).split('.')[0], #address E - Main
DaveM@13 159 'muz_narozeni_delka_minuty':str(float('0.'+str(self.cob[1]).split('.')[1])*60).split('.')[0],
DaveM@13 160 'muz_narozeni_delka_smer': '1' if self.cob[1]<0 else '0', #address E Dir (0':'E',1':'W)
DaveM@13 161 'muz_narozeni_timezone_form':'auto',
DaveM@13 162 'muz_narozeni_timezone_dst_form':'auto',
DaveM@13 163 'send_calculation':'1',
DaveM@13 164 'zena_narozeni_den':self.p_dob[0],
DaveM@13 165 'zena_narozeni_mesic':self.p_dob[1],
DaveM@13 166 'zena_narozeni_rok':self.p_dob[2],
DaveM@13 167 'zena_narozeni_hodina':self.p_tob[0],
DaveM@13 168 'zena_narozeni_minuta':self.p_tob[1],
DaveM@13 169 'zena_narozeni_city':'',
DaveM@13 170 'zena_narozeni_mesto_hidden':'Manually+place%3A+%C2%B0%27N%2C+%C2%B0%27E',
DaveM@13 171 'zena_narozeni_stat_hidden':'XX',
DaveM@13 172 'zena_narozeni_podstat_kratky_hidden':'',
DaveM@13 173 'zena_narozeni_podstat_hidden':'',
DaveM@13 174 'zena_narozeni_podstat2_kratky_hidden':'',
DaveM@13 175 'zena_narozeni_podstat3_kratky_hidden':'',
DaveM@13 176 'zena_narozeni_input_hidden':'',
DaveM@13 177 'zena_narozeni_sirka_stupne':str(abs(self.p_cob[0])).split('.')[0],
DaveM@13 178 'zena_narozeni_sirka_minuty':str(float('0.'+str(self.p_cob[0]).split('.')[1])*60).split('.')[0],
DaveM@13 179 'zena_narozeni_sirka_smer': '1' if self.p_cob[0]<0 else '0',
DaveM@13 180 'zena_narozeni_delka_stupne':str(abs(self.p_cob[1])).split('.')[0],
DaveM@13 181 'zena_narozeni_delka_minuty':str(float('0.'+str(self.p_cob[1]).split('.')[1])*60).split('.')[0],
DaveM@13 182 'zena_narozeni_delka_smer': '1' if self.p_cob[1]<0 else '0',
DaveM@13 183 'zena_narozeni_timezone_form':'auto',
DaveM@13 184 'zena_narozeni_timezone_dst_form':'auto',
DaveM@13 185 'switch_interpretations':'0',
DaveM@13 186 'house_system':'placidus',
DaveM@13 187 'uhel_orbis':'#tabs_redraw'}
DaveM@13 188
DaveM@19 189 def requestURL(self):
DaveM@19 190 self.resp = requests.get(self.url, params=self.payload)
DaveM@19 191 time.sleep(5)
DaveM@19 192 return self.resp
DaveM@19 193
DaveM@19 194 def parsePage(self):
DaveM@19 195 gotLocation = 0
DaveM@19 196 self.horiscope = planetPositions()
DaveM@19 197 soup = BeautifulSoup(self.resp.content, 'lxml')
DaveM@19 198 tcCell = soup.find_all('div', attrs={'class':'right-sedy-banner-svetlejsi'})
DaveM@19 199 for cell in tcCell:
DaveM@19 200 if "Planets in partner's house" in cell.get_text():
DaveM@19 201 gotLocation = 1
DaveM@19 202 divList = cell.find_all('div')
DaveM@19 203 for i in range(len(divList)):
DaveM@19 204 planetName = divList[i].getText().lower().strip().replace(':','').split(' ')[0]
DaveM@19 205 if planetName in planetPositions.planetNames:
DaveM@19 206 if gotLocation and not '/' in planetName:
DaveM@19 207 self.horiscope.planets[planetName].setHouse(divList[i+2].getText(),divList[i+4].getText())
DaveM@19 208 else:
DaveM@19 209 self.horiscope.planets[planetName].setLocation(divList[i+2].getText(),divList[i+1].img.attrs['alt'],0)
DaveM@19 210 self.horiscope.planets[planetName].setLocation(divList[i+4].getText(),divList[i+3].img.attrs['alt'],1)
DaveM@19 211 return self.horiscope
DaveM@19 212
DaveM@19 213
DaveM@13 214
DaveM@13 215 class planetRelation:
DaveM@15 216 noHouseList = ['asc','ic','dsc','mc','asc/mc','sun/moon']
DaveM@19 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}
DaveM@19 218
DaveM@13 219 def __init__(self,planetName):
DaveM@13 220 self.name = planetName
DaveM@13 221 self.angleA = None
DaveM@13 222 self.angleB = None
DaveM@15 223 if planetName not in planetRelation.noHouseList:
DaveM@15 224 self.houseA = None
DaveM@15 225 self.houseB = None
DaveM@13 226
DaveM@19 227 def setLocation(self,value,sign,isB):
DaveM@19 228 signVal = planetRelation.zodiacAngle[sign.lower()]
DaveM@19 229 signVal+= float('.'.join(value.encode('ascii','replace').split('?'))[:-1])
DaveM@19 230 # print self.name,sign.lower(),planetRelation.zodiacAngle[sign.lower()],value,signVal
DaveM@19 231 if not isB:
DaveM@19 232 self.angleA = signVal
DaveM@19 233 else:
DaveM@19 234 self.angleB = signVal
DaveM@19 235
DaveM@15 236 def setHouse(self,A,B):
DaveM@15 237 self.houseA = int(A.encode('ascii','ignore'))
DaveM@15 238 self.houseB = int(B.encode('ascii','ignore'))
DaveM@13 239
DaveM@13 240 def test_random(self):
DaveM@13 241 self.angleA = random.random()*360
DaveM@13 242 self.angleB = random.random()*360
DaveM@13 243
DaveM@13 244
DaveM@13 245 class planetPositions:
DaveM@15 246 aspectDict = {'conjunction':0,'semi-square':45,'sextile':60,'square':90,'trine':120,'opposite':180}
DaveM@19 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}
DaveM@19 248 # Taken from https://en.wikipedia.org/wiki/Astrological_sign, with reference from https://en.wikipedia.org/wiki/Astrological_symbols#Signs_of_the_zodiac
DaveM@15 249 planetNames = ['sun','moon','mercury','venus','mars','jupiter','saturn','uranus','neptune','pluto','node','lilith','chiron','asc','ic','dsc','mc','asc/mc','sun/moon']
DaveM@13 250 def __init__(self):
DaveM@13 251 self.planets = {}
DaveM@15 252 for p in planetPositions.planetNames:
DaveM@15 253 self.planets[p] = planetRelation(p)
DaveM@13 254 self.aspect = {}
DaveM@13 255
DaveM@13 256 def test_random(self):
DaveM@13 257 for planet in self.planets:
DaveM@13 258 self.planets[planet].test_random()
DaveM@13 259
DaveM@13 260 def calcAngle(self,componentA,componentB):
DaveM@19 261 self.angle = abs(self.planets[componentA].angleA - self.planets[componentB].angleB)
DaveM@13 262 self.angleRange = self.angle-10,self.angle+10
DaveM@13 263
DaveM@13 264 def calcAspect(self,componentA,componentB):
DaveM@13 265 self.calcAngle(componentA,componentB)
DaveM@19 266 # print componentA,componentB,self.angle,self.planets[componentA].angleA,self.planets[componentB].angleB
DaveM@13 267 for aspect in planetPositions.aspectDict:
DaveM@13 268 if self.angleRange[0] < planetPositions.aspectDict[aspect] and self.angleRange[1] > planetPositions.aspectDict[aspect]:
DaveM@19 269 # print aspect#,componentA,componentB,self.angle
DaveM@19 270 aspectDiff = round(abs(self.angle - planetPositions.aspectDict[aspect]),2)
DaveM@16 271 self.aspect[componentA,componentB] = (aspect,aspectDiff)
DaveM@16 272 return aspect,aspectDiff
DaveM@16 273 return None,None
DaveM@13 274
DaveM@16 275 def isInHouse(self,component,houseNo):
DaveM@16 276 # print (self.planets[component].houseA,self.planets[component].houseB,houseNo)
DaveM@16 277 if (self.planets[component].houseA == houseNo) or (self.planets[component].houseB == houseNo):
DaveM@16 278 return True
DaveM@16 279 return False
DaveM@16 280
DaveM@15 281 def calcAllAspects(self):
DaveM@15 282 for p1 in planetPositions.planetNames:
DaveM@15 283 for p2 in planetPositions.planetNames:
DaveM@15 284 self.calcAspect(p1,p2)
DaveM@13 285
DaveM@15 286 def printPositions(self):
DaveM@15 287 for p in planetPositions.planetNames:
DaveM@15 288 if p in planetRelation.noHouseList:
DaveM@15 289 print p,self.planets[p].angleA,self.planets[p].angleB
DaveM@15 290 else:
DaveM@15 291 print p,self.planets[p].angleA,self.planets[p].angleB,self.planets[p].houseA,self.planets[p].houseB
DaveM@13 292
DaveM@13 293
DaveM@13 294
DaveM@13 295
DaveM@13 296