DaveM@13: import random DaveM@16: import csv DaveM@16: import re DaveM@19: import requests DaveM@19: import time DaveM@19: from bs4 import BeautifulSoup DaveM@19: DaveM@16: import pdb DaveM@16: DaveM@16: class compatibility: DaveM@16: def __init__(self): DaveM@16: self.rules = [] DaveM@16: self.uniqueID = 0 DaveM@23: print 'Warning, need to fix [Vertex, Ascendant House, South Node, IC/MC and None Location] issues' DaveM@16: DaveM@16: def addRule(self,planetTuple,aspectList,score): DaveM@16: rule = compatibilityRule(self.uniqueID,planetTuple,aspectList,score) DaveM@16: self.rules.append(rule) DaveM@16: self.uniqueID += 1 DaveM@16: DaveM@16: def findRule(self,planet): DaveM@16: ruleList = [r for r in self.rules if r.planet == planet] DaveM@16: print ruleList DaveM@16: return ruleList DaveM@16: DaveM@16: def readFile(self,filename): DaveM@16: text = [] DaveM@16: with open(filename) as fp: DaveM@16: line = fp.readline() DaveM@16: text.append(line.strip()) DaveM@16: while line: DaveM@16: line = fp.readline() DaveM@16: text.append(line.strip()) DaveM@16: return text DaveM@16: DaveM@16: def parseCompatRules(self,filename): DaveM@16: comList = self.readFile(filename) DaveM@16: for com in comList: DaveM@23: # print com DaveM@23: planetTuple = None DaveM@23: aspectList = None DaveM@23: score = None DaveM@16: c = com.split(',') DaveM@16: if len(c) == 4: DaveM@16: if len(c[1]) > 1: DaveM@16: aspectList = [] DaveM@23: planetTuple = tuple((c[0].lower(),c[2].lower())) DaveM@19: score = int(c[3]) DaveM@23: for subset in c[1].lower().split(' '): DaveM@16: aspectList.append(subset) DaveM@16: elif len(c[1]) == 0 : DaveM@16: if 'house' not in c[2]: DaveM@16: print 'some error where rule is not house' DaveM@16: planetTuple = (c[0].lower(),'house') DaveM@16: houseNo = re.findall('\d+', c[2]) DaveM@16: if len(houseNo) != 1: DaveM@16: print 'multiple house numbers found - ERROR' DaveM@16: aspectList = int(houseNo[0]) DaveM@16: score = int(c[3]) DaveM@23: self.addRule(planetTuple,aspectList,score) DaveM@16: DaveM@16: def calcCompatibility(self, horiscope): DaveM@16: score = 0 DaveM@23: for r in sorted(self.rules): DaveM@16: if 'vertex' in r.planet: DaveM@16: # print 'ERROR - catch Vertex issue' DaveM@16: pass DaveM@23: elif 'southnode' in r.planet: DaveM@23: # print 'ERROR - catch South Node issue' DaveM@23: pass DaveM@23: # elif 'ic/mc' in r.planet: DaveM@23: # # print 'ERROR - catch IC/MC issue' DaveM@23: # pass DaveM@16: elif r.planet[1] == 'house' and r.planet[0] == 'asc': DaveM@16: # print 'ERROR - catch Ascendant House issue' DaveM@16: pass DaveM@16: elif horiscope.planets[r.planet[0]].angleA is None: DaveM@16: # print 'Error - None Location' DaveM@16: return None DaveM@16: elif r.planet[1] == 'house': DaveM@16: if horiscope.isInHouse(r.planet[0],r.aspect): DaveM@16: score += r.score DaveM@23: print r.planet[0] +' '+ r.planet[1] +' '+ str(aspect)+' '+str(r.score) DaveM@23: # print r.planet,r.aspect,r.score,score DaveM@23: elif r.planet[0] == r.planet[1]: DaveM@16: aspect,variance = horiscope.calcAspect(r.planet[0],r.planet[1]) DaveM@16: if aspect is not None: DaveM@16: if aspect in r.aspect: DaveM@16: score += r.score DaveM@23: print r.planet[0]+' '+aspect +' '+ r.planet[1] +' '+ str(r.score) DaveM@23: # print r.planet,r.aspect,aspect,r.score,score DaveM@23: else: DaveM@23: # print 'query standard synastry' DaveM@23: for order in [0,1]: DaveM@23: # print order,not order DaveM@23: # print r.planet[order],r.planet[not order] DaveM@23: aspect,variance = horiscope.calcAspect(r.planet[order],r.planet[not order]) DaveM@23: if aspect is not None: DaveM@23: # print aspect DaveM@23: # currentR = r DaveM@23: # pdb.set_trace() DaveM@23: if aspect in r.aspect: DaveM@23: score += r.score DaveM@23: print r.planet[0] +' '+ aspect +' '+ r.planet[1] +' '+ str(r.score) DaveM@23: # print r.planet,r.aspect,aspect,r.score,score DaveM@23: # else: DaveM@23: # print r.planet,r.aspect,aspect,0,score DaveM@23: print score DaveM@16: return score DaveM@16: DaveM@19: DaveM@16: DaveM@16: DaveM@16: class compatibilityRule: DaveM@16: def __init__(self,uniqueId,planetTuple,aspectList,score): DaveM@16: self.id = uniqueId DaveM@16: self.planet = planetTuple DaveM@16: self.aspect = aspectList DaveM@16: self.score = score DaveM@13: DaveM@13: class Person: DaveM@13: url = 'https://horoscopes.astro-seek.com/calculate-love-compatibility/' DaveM@13: def __init__(self,personDict): DaveM@13: # self. = planetPositions() DaveM@13: self.id = personDict['ID'] DaveM@13: self.dob = personDict['DOB'] DaveM@13: self.tob = personDict['TOB'] DaveM@13: self.cob = personDict['COB'] DaveM@13: self.p_dob = personDict['pDOB'] DaveM@13: self.p_tob = personDict['pTOB'] DaveM@13: self.p_cob = personDict['pCOB'] DaveM@13: self.horiscope = None DaveM@22: self.resp = None DaveM@13: DaveM@15: def identifyIssues(self): DaveM@15: if self.id is None: DaveM@16: self.issue = 'id' DaveM@15: elif self.dob is None: DaveM@16: self.issue = 'dob' DaveM@15: elif self.tob is None: DaveM@16: self.issue = 'tob' DaveM@15: elif self.cob is None: DaveM@16: self.issue = 'cob' DaveM@15: elif self.p_dob is None: DaveM@16: self.issue = 'p_dob' DaveM@15: elif self.p_tob is None: DaveM@16: self.issue = 'p_tob' DaveM@15: elif self.p_cob is None: DaveM@16: self.issue = 'p_cob' DaveM@15: else: DaveM@16: self.issue = None DaveM@16: return self.issue DaveM@15: DaveM@13: def makePayload(self): DaveM@13: if type(self.cob) is str: DaveM@13: cob_0 = float(self.cob.split(',')[0][1:]) DaveM@13: cob_1 = float(self.cob.split(',')[1]) DaveM@13: self.cob = (cob_0,cob_1) DaveM@13: if type(self.p_cob) is str: DaveM@13: pcob_0 = float(self.p_cob.split(',')[0][1:]) DaveM@13: pcob_1 = float(self.p_cob.split(',')[1]) DaveM@13: self.p_cob = (pcob_0,pcob_1) DaveM@13: if type(self.dob) is str: DaveM@13: self.dob = self.dob[1:-1].split(',') DaveM@13: if type(self.p_dob) is str: DaveM@13: self.p_dob = self.p_dob[1:-1].split(',') DaveM@13: if type(self.tob) is str: DaveM@13: self.tob = self.tob[1:-1].split(',') DaveM@13: if type(self.p_tob) is str: DaveM@13: self.p_tob = self.p_tob[1:-1].split(',') DaveM@19: DaveM@13: self.payload = {'send_calculation':'1', #Req DaveM@13: 'muz_narozeni_den':self.dob[0], DaveM@13: 'muz_narozeni_mesic':self.dob[1], DaveM@13: 'muz_narozeni_rok':self.dob[2], DaveM@13: 'muz_narozeni_hodina':self.tob[0], DaveM@13: 'muz_narozeni_minuta':self.tob[1], DaveM@13: 'muz_narozeni_city':'', DaveM@13: 'muz_narozeni_mesto_hidden':'Manually+place%3A+%C2%B0%27N%2C+%C2%B0%27E',#auto DaveM@13: 'muz_narozeni_stat_hidden':'XX', DaveM@13: 'muz_narozeni_podstat_kratky_hidden':'', DaveM@13: 'muz_narozeni_podstat_hidden':'', DaveM@13: 'muz_narozeni_podstat2_kratky_hidden':'', DaveM@13: 'muz_narozeni_podstat3_kratky_hidden':'', DaveM@13: 'muz_narozeni_input_hidden':'', DaveM@13: 'muz_narozeni_sirka_stupne':str(abs(self.cob[0])).split('.')[0], DaveM@13: 'muz_narozeni_sirka_minuty':str(float('0.'+str(self.cob[0]).split('.')[1])*60).split('.')[0], DaveM@13: 'muz_narozeni_sirka_smer': '1' if self.cob[0]<0 else '0', #address N Dir (0':'N',1':'S) DaveM@13: 'muz_narozeni_delka_stupne':str(abs(self.cob[1])).split('.')[0], #address E - Main DaveM@13: 'muz_narozeni_delka_minuty':str(float('0.'+str(self.cob[1]).split('.')[1])*60).split('.')[0], DaveM@13: 'muz_narozeni_delka_smer': '1' if self.cob[1]<0 else '0', #address E Dir (0':'E',1':'W) DaveM@13: 'muz_narozeni_timezone_form':'auto', DaveM@13: 'muz_narozeni_timezone_dst_form':'auto', DaveM@13: 'send_calculation':'1', DaveM@13: 'zena_narozeni_den':self.p_dob[0], DaveM@13: 'zena_narozeni_mesic':self.p_dob[1], DaveM@13: 'zena_narozeni_rok':self.p_dob[2], DaveM@13: 'zena_narozeni_hodina':self.p_tob[0], DaveM@13: 'zena_narozeni_minuta':self.p_tob[1], DaveM@13: 'zena_narozeni_city':'', DaveM@13: 'zena_narozeni_mesto_hidden':'Manually+place%3A+%C2%B0%27N%2C+%C2%B0%27E', DaveM@13: 'zena_narozeni_stat_hidden':'XX', DaveM@13: 'zena_narozeni_podstat_kratky_hidden':'', DaveM@13: 'zena_narozeni_podstat_hidden':'', DaveM@13: 'zena_narozeni_podstat2_kratky_hidden':'', DaveM@13: 'zena_narozeni_podstat3_kratky_hidden':'', DaveM@13: 'zena_narozeni_input_hidden':'', DaveM@13: 'zena_narozeni_sirka_stupne':str(abs(self.p_cob[0])).split('.')[0], DaveM@13: 'zena_narozeni_sirka_minuty':str(float('0.'+str(self.p_cob[0]).split('.')[1])*60).split('.')[0], DaveM@13: 'zena_narozeni_sirka_smer': '1' if self.p_cob[0]<0 else '0', DaveM@13: 'zena_narozeni_delka_stupne':str(abs(self.p_cob[1])).split('.')[0], DaveM@13: 'zena_narozeni_delka_minuty':str(float('0.'+str(self.p_cob[1]).split('.')[1])*60).split('.')[0], DaveM@13: 'zena_narozeni_delka_smer': '1' if self.p_cob[1]<0 else '0', DaveM@13: 'zena_narozeni_timezone_form':'auto', DaveM@13: 'zena_narozeni_timezone_dst_form':'auto', DaveM@13: 'switch_interpretations':'0', DaveM@13: 'house_system':'placidus', DaveM@13: 'uhel_orbis':'#tabs_redraw'} DaveM@13: DaveM@19: def requestURL(self): DaveM@19: self.resp = requests.get(self.url, params=self.payload) DaveM@19: time.sleep(5) DaveM@19: return self.resp DaveM@19: DaveM@19: def parsePage(self): DaveM@22: self.horiscope = None DaveM@22: if('Please use valid date.' in self.resp.content): DaveM@22: self.issue = 'dob' DaveM@22: return self.horiscope DaveM@22: elif('Please use valid time.' in self.resp.content): DaveM@22: self.issue = 'tob' DaveM@22: return self.horiscope DaveM@19: gotLocation = 0 DaveM@19: self.horiscope = planetPositions() DaveM@19: soup = BeautifulSoup(self.resp.content, 'lxml') DaveM@19: tcCell = soup.find_all('div', attrs={'class':'right-sedy-banner-svetlejsi'}) DaveM@19: for cell in tcCell: DaveM@19: if "Planets in partner's house" in cell.get_text(): DaveM@19: gotLocation = 1 DaveM@19: divList = cell.find_all('div') DaveM@19: for i in range(len(divList)): DaveM@19: planetName = divList[i].getText().lower().strip().replace(':','').split(' ')[0] DaveM@19: if planetName in planetPositions.planetNames: DaveM@19: if gotLocation and not '/' in planetName: DaveM@19: self.horiscope.planets[planetName].setHouse(divList[i+2].getText(),divList[i+4].getText()) DaveM@19: else: DaveM@19: self.horiscope.planets[planetName].setLocation(divList[i+2].getText(),divList[i+1].img.attrs['alt'],0) DaveM@19: self.horiscope.planets[planetName].setLocation(divList[i+4].getText(),divList[i+3].img.attrs['alt'],1) DaveM@19: return self.horiscope DaveM@19: DaveM@19: DaveM@13: DaveM@13: class planetRelation: DaveM@15: noHouseList = ['asc','ic','dsc','mc','asc/mc','sun/moon'] DaveM@19: 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: DaveM@13: def __init__(self,planetName): DaveM@13: self.name = planetName DaveM@13: self.angleA = None DaveM@13: self.angleB = None DaveM@15: if planetName not in planetRelation.noHouseList: DaveM@15: self.houseA = None DaveM@15: self.houseB = None DaveM@13: DaveM@19: def setLocation(self,value,sign,isB): DaveM@19: signVal = planetRelation.zodiacAngle[sign.lower()] DaveM@19: signVal+= float('.'.join(value.encode('ascii','replace').split('?'))[:-1]) DaveM@19: # print self.name,sign.lower(),planetRelation.zodiacAngle[sign.lower()],value,signVal DaveM@19: if not isB: DaveM@19: self.angleA = signVal DaveM@19: else: DaveM@19: self.angleB = signVal DaveM@19: DaveM@15: def setHouse(self,A,B): DaveM@15: self.houseA = int(A.encode('ascii','ignore')) DaveM@15: self.houseB = int(B.encode('ascii','ignore')) DaveM@13: DaveM@13: def test_random(self): DaveM@13: self.angleA = random.random()*360 DaveM@13: self.angleB = random.random()*360 DaveM@13: DaveM@13: DaveM@13: class planetPositions: DaveM@23: aspectDict = {'conjunction':0,'semi-square':45,'sextile':60,'square':90,'trine':120,'opposition':180} DaveM@19: # 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: # 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: planetNames = ['sun','moon','mercury','venus','mars','jupiter','saturn','uranus','neptune','pluto','node','lilith','chiron','asc','ic','dsc','mc','asc/mc','sun/moon'] DaveM@13: def __init__(self): DaveM@13: self.planets = {} DaveM@15: for p in planetPositions.planetNames: DaveM@15: self.planets[p] = planetRelation(p) DaveM@13: self.aspect = {} DaveM@13: DaveM@13: def test_random(self): DaveM@13: for planet in self.planets: DaveM@13: self.planets[planet].test_random() DaveM@13: DaveM@13: def calcAngle(self,componentA,componentB): DaveM@19: self.angle = abs(self.planets[componentA].angleA - self.planets[componentB].angleB) DaveM@13: self.angleRange = self.angle-10,self.angle+10 DaveM@13: DaveM@13: def calcAspect(self,componentA,componentB): DaveM@13: self.calcAngle(componentA,componentB) DaveM@19: # print componentA,componentB,self.angle,self.planets[componentA].angleA,self.planets[componentB].angleB DaveM@13: for aspect in planetPositions.aspectDict: DaveM@13: if self.angleRange[0] < planetPositions.aspectDict[aspect] and self.angleRange[1] > planetPositions.aspectDict[aspect]: DaveM@19: # print aspect#,componentA,componentB,self.angle DaveM@19: aspectDiff = round(abs(self.angle - planetPositions.aspectDict[aspect]),2) DaveM@16: self.aspect[componentA,componentB] = (aspect,aspectDiff) DaveM@16: return aspect,aspectDiff DaveM@16: return None,None DaveM@13: DaveM@16: def isInHouse(self,component,houseNo): DaveM@16: # print (self.planets[component].houseA,self.planets[component].houseB,houseNo) DaveM@16: if (self.planets[component].houseA == houseNo) or (self.planets[component].houseB == houseNo): DaveM@16: return True DaveM@16: return False DaveM@22: DaveM@22: def makeAllAspectTreple(self): DaveM@22: self.aspectTreple = {} DaveM@22: for p1 in planetPositions.planetNames: DaveM@22: for p2 in planetPositions.planetNames: DaveM@22: aspect = self.calcAspect(p1,p2)[0] DaveM@22: if aspect is not None: DaveM@22: self.aspectTreple[(p1,p2,aspect)] = 1 DaveM@16: DaveM@15: def calcAllAspects(self): DaveM@15: for p1 in planetPositions.planetNames: DaveM@15: for p2 in planetPositions.planetNames: DaveM@15: self.calcAspect(p1,p2) DaveM@13: DaveM@15: def printPositions(self): DaveM@15: for p in planetPositions.planetNames: DaveM@15: if p in planetRelation.noHouseList: DaveM@15: print p,self.planets[p].angleA,self.planets[p].angleB DaveM@15: else: DaveM@15: print p,self.planets[p].angleA,self.planets[p].angleB,self.planets[p].houseA,self.planets[p].houseB DaveM@13: DaveM@13: DaveM@13: DaveM@13: DaveM@13: