-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.py
125 lines (105 loc) · 4.67 KB
/
source.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import requests, time, re
from bs4 import BeautifulSoup as bs
from datetime import datetime, date, timedelta
from mailjet_rest import Client
def main():
with open("config.txt") as conf:
config = conf.read().splitlines()
keywords = [key.strip() for key in config[1].replace("Races:", "").split(",")]
emails = [key.strip() for key in config[2].replace("Emails:", "").split(",")]
try:
with open("notified") as ntf:
notified = ntf.read().splitlines()
except FileNotFoundError:
open("notified", "w").close()
success = False
while not success:
try:
content = requests.get("https://dubairacingclub.com/visit/calendar/racing-calendar")
success = True
except:
time.sleep(5)
soup = bs(content.content, "html.parser")
dates, titles, times = [], [], []
for k in soup.find_all("span", {"class": "date-display-single"}):
dates.append(k.text.strip().split(", ", 1))
for k in soup.find_all("td", {"class": "views-field views-field-title"}):
titles.append(k.text.strip())
for k in soup.find_all("td", {"class": "views-field views-field-field-event-post-time-value"}):
times.append(k.text.strip())
races = [list(l) for l in zip(dates, titles, times)]
races = [l for l in races if any([bool(re.search(k, l[1], re.I)) for k in keywords])]
MONTHS = {"January": "01", "February": "02", "March": "03", "April": "04", "May": "05", "June": "06", "July": "07", "August": "08", "September": "09", "October": "10", "November": "11", "December": "12"}
today = []
tomorrow = []
for race in races:
race[0][1] = race[0][1].replace(",", "").split()
race[0][1][1] = MONTHS[race[0][1][1]]
if len(race[0][1][0]) < 2:
race[0][1][0] = "0" + race[0][1][0]
race[0][1] = " ".join(race[0][1])
race[2] = race[2].lower()
if "pm" in race[2]:
race[2] = race[2].replace("pm", "").split(":")
race[2][0] = str(int(race[2][0]) + 12)
race[2] = ":".join(race[2])
elif "am" in race[2]:
race[2] = race[2].replace("am", "")
if len(race[2].split(":")[0]) < 2:
race[2] = race[2].split(":")
race[2][0] = "0" + race[2][0]
race[2] = ":".join(race[2])
race[0][1] = race[0][1] + " " + race[2]
fd = datetime.strptime(race[0][1], '%d %m %Y %H:%M')
raceDate = str(fd.date())
raceTimestamp = fd.timestamp()
if raceDate == str(date.today()) and raceTimestamp > time.time() and not race[0][1] in notified:
today.append([race[1], race[0][0], race[2], race[0][1]])
if raceDate == str(date.today() +timedelta(days=1)) and not race[0][1] in notified:
tomorrow.append([race[1], race[0][0], race[2], race[0][1]])
body = ""
body += "<strong>Today:</strong><br/>"
if today:
for race in today:
body += f"{race[0]} on {race[1]} at {race[2]}<br/>"
else:
body += "None<br/>"
body += "<br/><strong>Tomorrow:</strong><br/>"
if tomorrow:
for race in tomorrow:
body += f"{race[0]} on {race[1]} at {race[2]}<br/>"
else:
body += "None<br/>"
mailjet = Client(auth=('bfa36871ec035cbe8798ae723dd268d2', 'ef38eca3a518901be8379222827e5c64'), version='v3.1')
if today or tomorrow:
for email in emails:
data = {
'Messages': [
{
"From": {
"Email": "[email protected]",
"Name": "Races notifier"
},
"To": [
{
"Email": email,
}
],
"Subject": f"Races notifier {date.today()}",
"TextPart": "Races notifier",
"HTMLPart": "<h3>These are the races for today and tomorrow</h3><br/>" + body
}
]
}
result = mailjet.send.create(data=data)
time.sleep(5)
if result.status_code == 200:
with open("notified", "a") as ntf:
if today:
for race in today:
ntf.write(race[3] + "\n")
if tomorrow:
for race in tomorrow:
ntf.write(race[3] + "\n")
if __name__ == "__main__":
main()