annotate V4/synastry.py @ 22:a5b8e2b91d8f

fixing None Planets bug and reformatting output of fullResults
author DaveM
date Thu, 08 Mar 2018 20:36:44 +0000
parents ae220e89cb3a
children 11d4e438045e
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@22 103 self.resp = None
DaveM@13 104
DaveM@15 105 def identifyIssues(self):
DaveM@15 106 if self.id is None:
DaveM@16 107 self.issue = 'id'
DaveM@15 108 elif self.dob is None:
DaveM@16 109 self.issue = 'dob'
DaveM@15 110 elif self.tob is None:
DaveM@16 111 self.issue = 'tob'
DaveM@15 112 elif self.cob is None:
DaveM@16 113 self.issue = 'cob'
DaveM@15 114 elif self.p_dob is None:
DaveM@16 115 self.issue = 'p_dob'
DaveM@15 116 elif self.p_tob is None:
DaveM@16 117 self.issue = 'p_tob'
DaveM@15 118 elif self.p_cob is None:
DaveM@16 119 self.issue = 'p_cob'
DaveM@15 120 else:
DaveM@16 121 self.issue = None
DaveM@16 122 return self.issue
DaveM@15 123
DaveM@13 124 def makePayload(self):
DaveM@13 125 if type(self.cob) is str:
DaveM@13 126 cob_0 = float(self.cob.split(',')[0][1:])
DaveM@13 127 cob_1 = float(self.cob.split(',')[1])
DaveM@13 128 self.cob = (cob_0,cob_1)
DaveM@13 129 if type(self.p_cob) is str:
DaveM@13 130 pcob_0 = float(self.p_cob.split(',')[0][1:])
DaveM@13 131 pcob_1 = float(self.p_cob.split(',')[1])
DaveM@13 132 self.p_cob = (pcob_0,pcob_1)
DaveM@13 133 if type(self.dob) is str:
DaveM@13 134 self.dob = self.dob[1:-1].split(',')
DaveM@13 135 if type(self.p_dob) is str:
DaveM@13 136 self.p_dob = self.p_dob[1:-1].split(',')
DaveM@13 137 if type(self.tob) is str:
DaveM@13 138 self.tob = self.tob[1:-1].split(',')
DaveM@13 139 if type(self.p_tob) is str:
DaveM@13 140 self.p_tob = self.p_tob[1:-1].split(',')
DaveM@19 141
DaveM@13 142 self.payload = {'send_calculation':'1', #Req
DaveM@13 143 'muz_narozeni_den':self.dob[0],
DaveM@13 144 'muz_narozeni_mesic':self.dob[1],
DaveM@13 145 'muz_narozeni_rok':self.dob[2],
DaveM@13 146 'muz_narozeni_hodina':self.tob[0],
DaveM@13 147 'muz_narozeni_minuta':self.tob[1],
DaveM@13 148 'muz_narozeni_city':'',
DaveM@13 149 'muz_narozeni_mesto_hidden':'Manually+place%3A+%C2%B0%27N%2C+%C2%B0%27E',#auto
DaveM@13 150 'muz_narozeni_stat_hidden':'XX',
DaveM@13 151 'muz_narozeni_podstat_kratky_hidden':'',
DaveM@13 152 'muz_narozeni_podstat_hidden':'',
DaveM@13 153 'muz_narozeni_podstat2_kratky_hidden':'',
DaveM@13 154 'muz_narozeni_podstat3_kratky_hidden':'',
DaveM@13 155 'muz_narozeni_input_hidden':'',
DaveM@13 156 'muz_narozeni_sirka_stupne':str(abs(self.cob[0])).split('.')[0],
DaveM@13 157 'muz_narozeni_sirka_minuty':str(float('0.'+str(self.cob[0]).split('.')[1])*60).split('.')[0],
DaveM@13 158 'muz_narozeni_sirka_smer': '1' if self.cob[0]<0 else '0', #address N Dir (0':'N',1':'S)
DaveM@13 159 'muz_narozeni_delka_stupne':str(abs(self.cob[1])).split('.')[0], #address E - Main
DaveM@13 160 'muz_narozeni_delka_minuty':str(float('0.'+str(self.cob[1]).split('.')[1])*60).split('.')[0],
DaveM@13 161 'muz_narozeni_delka_smer': '1' if self.cob[1]<0 else '0', #address E Dir (0':'E',1':'W)
DaveM@13 162 'muz_narozeni_timezone_form':'auto',
DaveM@13 163 'muz_narozeni_timezone_dst_form':'auto',
DaveM@13 164 'send_calculation':'1',
DaveM@13 165 'zena_narozeni_den':self.p_dob[0],
DaveM@13 166 'zena_narozeni_mesic':self.p_dob[1],
DaveM@13 167 'zena_narozeni_rok':self.p_dob[2],
DaveM@13 168 'zena_narozeni_hodina':self.p_tob[0],
DaveM@13 169 'zena_narozeni_minuta':self.p_tob[1],
DaveM@13 170 'zena_narozeni_city':'',
DaveM@13 171 'zena_narozeni_mesto_hidden':'Manually+place%3A+%C2%B0%27N%2C+%C2%B0%27E',
DaveM@13 172 'zena_narozeni_stat_hidden':'XX',
DaveM@13 173 'zena_narozeni_podstat_kratky_hidden':'',
DaveM@13 174 'zena_narozeni_podstat_hidden':'',
DaveM@13 175 'zena_narozeni_podstat2_kratky_hidden':'',
DaveM@13 176 'zena_narozeni_podstat3_kratky_hidden':'',
DaveM@13 177 'zena_narozeni_input_hidden':'',
DaveM@13 178 'zena_narozeni_sirka_stupne':str(abs(self.p_cob[0])).split('.')[0],
DaveM@13 179 'zena_narozeni_sirka_minuty':str(float('0.'+str(self.p_cob[0]).split('.')[1])*60).split('.')[0],
DaveM@13 180 'zena_narozeni_sirka_smer': '1' if self.p_cob[0]<0 else '0',
DaveM@13 181 'zena_narozeni_delka_stupne':str(abs(self.p_cob[1])).split('.')[0],
DaveM@13 182 'zena_narozeni_delka_minuty':str(float('0.'+str(self.p_cob[1]).split('.')[1])*60).split('.')[0],
DaveM@13 183 'zena_narozeni_delka_smer': '1' if self.p_cob[1]<0 else '0',
DaveM@13 184 'zena_narozeni_timezone_form':'auto',
DaveM@13 185 'zena_narozeni_timezone_dst_form':'auto',
DaveM@13 186 'switch_interpretations':'0',
DaveM@13 187 'house_system':'placidus',
DaveM@13 188 'uhel_orbis':'#tabs_redraw'}
DaveM@13 189
DaveM@19 190 def requestURL(self):
DaveM@19 191 self.resp = requests.get(self.url, params=self.payload)
DaveM@19 192 time.sleep(5)
DaveM@19 193 return self.resp
DaveM@19 194
DaveM@19 195 def parsePage(self):
DaveM@22 196 self.horiscope = None
DaveM@22 197 if('Please use valid date.' in self.resp.content):
DaveM@22 198 self.issue = 'dob'
DaveM@22 199 return self.horiscope
DaveM@22 200 elif('Please use valid time.' in self.resp.content):
DaveM@22 201 self.issue = 'tob'
DaveM@22 202 return self.horiscope
DaveM@19 203 gotLocation = 0
DaveM@19 204 self.horiscope = planetPositions()
DaveM@19 205 soup = BeautifulSoup(self.resp.content, 'lxml')
DaveM@19 206 tcCell = soup.find_all('div', attrs={'class':'right-sedy-banner-svetlejsi'})
DaveM@19 207 for cell in tcCell:
DaveM@19 208 if "Planets in partner's house" in cell.get_text():
DaveM@19 209 gotLocation = 1
DaveM@19 210 divList = cell.find_all('div')
DaveM@19 211 for i in range(len(divList)):
DaveM@19 212 planetName = divList[i].getText().lower().strip().replace(':','').split(' ')[0]
DaveM@19 213 if planetName in planetPositions.planetNames:
DaveM@19 214 if gotLocation and not '/' in planetName:
DaveM@19 215 self.horiscope.planets[planetName].setHouse(divList[i+2].getText(),divList[i+4].getText())
DaveM@19 216 else:
DaveM@19 217 self.horiscope.planets[planetName].setLocation(divList[i+2].getText(),divList[i+1].img.attrs['alt'],0)
DaveM@19 218 self.horiscope.planets[planetName].setLocation(divList[i+4].getText(),divList[i+3].img.attrs['alt'],1)
DaveM@19 219 return self.horiscope
DaveM@19 220
DaveM@19 221
DaveM@13 222
DaveM@13 223 class planetRelation:
DaveM@15 224 noHouseList = ['asc','ic','dsc','mc','asc/mc','sun/moon']
DaveM@19 225 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 226
DaveM@13 227 def __init__(self,planetName):
DaveM@13 228 self.name = planetName
DaveM@13 229 self.angleA = None
DaveM@13 230 self.angleB = None
DaveM@15 231 if planetName not in planetRelation.noHouseList:
DaveM@15 232 self.houseA = None
DaveM@15 233 self.houseB = None
DaveM@13 234
DaveM@19 235 def setLocation(self,value,sign,isB):
DaveM@19 236 signVal = planetRelation.zodiacAngle[sign.lower()]
DaveM@19 237 signVal+= float('.'.join(value.encode('ascii','replace').split('?'))[:-1])
DaveM@19 238 # print self.name,sign.lower(),planetRelation.zodiacAngle[sign.lower()],value,signVal
DaveM@19 239 if not isB:
DaveM@19 240 self.angleA = signVal
DaveM@19 241 else:
DaveM@19 242 self.angleB = signVal
DaveM@19 243
DaveM@15 244 def setHouse(self,A,B):
DaveM@15 245 self.houseA = int(A.encode('ascii','ignore'))
DaveM@15 246 self.houseB = int(B.encode('ascii','ignore'))
DaveM@13 247
DaveM@13 248 def test_random(self):
DaveM@13 249 self.angleA = random.random()*360
DaveM@13 250 self.angleB = random.random()*360
DaveM@13 251
DaveM@13 252
DaveM@13 253 class planetPositions:
DaveM@15 254 aspectDict = {'conjunction':0,'semi-square':45,'sextile':60,'square':90,'trine':120,'opposite':180}
DaveM@19 255 # 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 256 # 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 257 planetNames = ['sun','moon','mercury','venus','mars','jupiter','saturn','uranus','neptune','pluto','node','lilith','chiron','asc','ic','dsc','mc','asc/mc','sun/moon']
DaveM@13 258 def __init__(self):
DaveM@13 259 self.planets = {}
DaveM@15 260 for p in planetPositions.planetNames:
DaveM@15 261 self.planets[p] = planetRelation(p)
DaveM@13 262 self.aspect = {}
DaveM@13 263
DaveM@13 264 def test_random(self):
DaveM@13 265 for planet in self.planets:
DaveM@13 266 self.planets[planet].test_random()
DaveM@13 267
DaveM@13 268 def calcAngle(self,componentA,componentB):
DaveM@19 269 self.angle = abs(self.planets[componentA].angleA - self.planets[componentB].angleB)
DaveM@13 270 self.angleRange = self.angle-10,self.angle+10
DaveM@13 271
DaveM@13 272 def calcAspect(self,componentA,componentB):
DaveM@13 273 self.calcAngle(componentA,componentB)
DaveM@19 274 # print componentA,componentB,self.angle,self.planets[componentA].angleA,self.planets[componentB].angleB
DaveM@13 275 for aspect in planetPositions.aspectDict:
DaveM@13 276 if self.angleRange[0] < planetPositions.aspectDict[aspect] and self.angleRange[1] > planetPositions.aspectDict[aspect]:
DaveM@19 277 # print aspect#,componentA,componentB,self.angle
DaveM@19 278 aspectDiff = round(abs(self.angle - planetPositions.aspectDict[aspect]),2)
DaveM@16 279 self.aspect[componentA,componentB] = (aspect,aspectDiff)
DaveM@16 280 return aspect,aspectDiff
DaveM@16 281 return None,None
DaveM@13 282
DaveM@16 283 def isInHouse(self,component,houseNo):
DaveM@16 284 # print (self.planets[component].houseA,self.planets[component].houseB,houseNo)
DaveM@16 285 if (self.planets[component].houseA == houseNo) or (self.planets[component].houseB == houseNo):
DaveM@16 286 return True
DaveM@16 287 return False
DaveM@22 288
DaveM@22 289 def makeAllAspectTreple(self):
DaveM@22 290 self.aspectTreple = {}
DaveM@22 291 for p1 in planetPositions.planetNames:
DaveM@22 292 for p2 in planetPositions.planetNames:
DaveM@22 293 aspect = self.calcAspect(p1,p2)[0]
DaveM@22 294 if aspect is not None:
DaveM@22 295 self.aspectTreple[(p1,p2,aspect)] = 1
DaveM@16 296
DaveM@15 297 def calcAllAspects(self):
DaveM@15 298 for p1 in planetPositions.planetNames:
DaveM@15 299 for p2 in planetPositions.planetNames:
DaveM@15 300 self.calcAspect(p1,p2)
DaveM@13 301
DaveM@15 302 def printPositions(self):
DaveM@15 303 for p in planetPositions.planetNames:
DaveM@15 304 if p in planetRelation.noHouseList:
DaveM@15 305 print p,self.planets[p].angleA,self.planets[p].angleB
DaveM@15 306 else:
DaveM@15 307 print p,self.planets[p].angleA,self.planets[p].angleB,self.planets[p].houseA,self.planets[p].houseB
DaveM@13 308
DaveM@13 309
DaveM@13 310
DaveM@13 311
DaveM@13 312