-
Notifications
You must be signed in to change notification settings - Fork 61
/
pyhoroscope.py
103 lines (91 loc) · 3.99 KB
/
pyhoroscope.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from lxml import html
import requests
from datetime import datetime, timezone
####################################################################
# API
####################################################################
class Horoscope:
@staticmethod
def get_todays_horoscope(sunsign):
url = "http://www.ganeshaspeaks.com/horoscopes/daily-horoscope/" + sunsign
response = requests.get(url)
tree = html.fromstring(response.content)
date = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()"))
date = date.replace("']", "").replace("['", "")
date_utc = datetime.now(timezone.utc)
date_website = "-".join(date.split('-')[::-1])
date_local = str(date_utc.astimezone()).split(' ')[0]
if date_local < date_website :
url = "https://www.ganeshaspeaks.com/horoscopes/yesterday-horoscope/" + sunsign
response = requests.get(url)
tree = html.fromstring(response.content)
horoscope = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()"))
elif date_local > date_website :
url = "https://www.ganeshaspeaks.com/horoscopes/tomorrow-horoscope/" + sunsign
response = requests.get(url)
tree = html.fromstring(response.content)
horoscope = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()"))
else :
horoscope = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()"))
horoscope = horoscope.replace("\\n", "").replace(" ", "").replace("[\"", "").replace("\"]", "").replace("[\'", "").replace("\']", "")
dict = {
'date': date_local,
'horoscope': horoscope,
'sunsign': sunsign
}
return dict
@staticmethod
def get_weekly_horoscope(sunsign):
url = "http://www.ganeshaspeaks.com/horoscopes/weekly-horoscope/" + sunsign
response = requests.get(url)
tree = html.fromstring(response.content)
week = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()"))
week = week.replace("']", "").replace("['", "")
horoscope = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()"))
horoscope = horoscope.replace("\\n", "").replace(" ", "").replace("']", "").replace("['", "")
dict = {
'week': week,
'horoscope': horoscope,
'sunsign': sunsign
}
return dict
@staticmethod
def get_monthly_horoscope(sunsign):
url = "http://www.ganeshaspeaks.com/horoscopes/monthly-horoscope/" + sunsign
response = requests.get(url)
tree = html.fromstring(response.content)
month = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()"))
month = month.replace("']", "").replace("['", "")
horoscope = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()[1]"))
horoscope = horoscope.replace("\\n", "").replace(" ", "").replace("']", "").replace("['", "")
dict = {
'month': month,
'horoscope': horoscope,
'sunsign': sunsign
}
return dict
@staticmethod
def get_yearly_horoscope(sunsign):
url = "http://www.ganeshaspeaks.com/horoscopes/yearly-horoscope/" + sunsign
response = requests.get(url)
tree = html.fromstring(response.content)
year = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()"))
year = year.replace("']", "").replace("['", "")
horoscope = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()"))
horoscope = horoscope.replace("\\n", "").replace(" ", "").replace("']", "").replace("['", "")
dict = {
'year': year,
'horoscope': horoscope,
'sunsign': sunsign
}
return dict