-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (64 loc) · 2.82 KB
/
main.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
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
from datetime import datetime
import requests
import time
import os
# Environment Variables: Use your own secret keys
from util.dev import print_dict
chat_id = os.environ['CHAT_ID']
api_key = os.environ['API_KEY']
biathlon_infos = "https://www.sport.de/biathlon/kalender/"
biathlon_ereignisse = {}
def get_data_from_website(url, dataset):
# opening connection, grabbing page content and closing connection
uClient = uReq(url)
page_content = uClient.read()
uClient.close()
# html parser
page_soup = soup(page_content, "html.parser")
# get all events
container = (page_soup.find_all("tr"))
for entry in container:
if entry.find("td", {"class": "hs-date-breaker-col"}):
date = entry.find("td", {"class": "hs-date-breaker-col"}).text
else:
gender = 'Männer' if 'male' in (entry['class']) else 'Frauen'
mytime = entry.find("td", {"class": "calendar-date"}).text
try:
country = entry.find("td", {"class": "country-flag"}).img['alt']
except AttributeError:
country = "-"
region = entry.find("td", {"class": "calendar-competition-name"}).text
event = entry.find("td", {"class": "calendar-event"}).text
# add new node
if date not in dataset:
dataset[date] = [
{'Uhrzeit': mytime, 'Land': country, 'Region': region, 'Event': event, 'Geschlecht': gender}]
# if node already exists
else:
dataset[date].append(
{'Uhrzeit': mytime, 'Land': country, 'Region': region, 'Event': event, 'Geschlecht': gender})
return dataset
def send_notification_telegram(dataset, chat, api):
print("Das Programm ist aktiv...")
while True:
date = datetime.now().strftime('%d.%m.%Y')
now = str(datetime.now().strftime('%H:%M')) + "h"
if date in dataset:
events = dataset[date]
for event in events:
if event['Uhrzeit'] == now:
print("Event jetzt gefunden!")
message = "Jetzt " + event['Event'] + " der " + event['Geschlecht'] + " in " + event[
'Region'] + " (" \
+ event['Land'] + ")"
raw_url = 'https://api.telegram.org/bot' + api + '/' \
'sendMessage?chat_id=' + str(
chat) + '&text="{}"'.format(message)
requests.get(raw_url)
time.sleep(300)
# get data
biathlon_ereignisse = get_data_from_website(biathlon_infos, biathlon_ereignisse)
# send notification
# send_notification_telegram(biathlon_ereignisse, chat_id, api_key)