-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
83 lines (74 loc) · 2.64 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
81
82
83
from bs4 import BeautifulSoup
import requests
import sys
import re
from jinja2 import Template
from hash_tree import *
from notify import *
URL = 'https://www.kvv.de/fahrplan/verkehrsmeldungen.html'
MAIL_PATTERN = '[^@]+@[^@]+.\w+'
FILTER_CHARS = '[a-zA-Z0-9_\-\ ,]'
SEP_REGEX = '[\|&]'
ARG_PATTERN = '^(' + MAIL_PATTERN + ')\{(' + FILTER_CHARS + '+)(?:' + SEP_REGEX + '(' + FILTER_CHARS + '+))*\}$'
def run():
if not arguments_valid():
return
html_page = requests.get(URL)
soup = BeautifulSoup(html_page.content, 'html.parser')
results = soup.find_all('div', class_='ix_kvv_ticker_list')
hash_tree = HashTree(results)
diffs = hash_tree.find_diffs()
if len(diffs) < 1:
print('[INFO] nothing new')
return
hash_tree.update()
table = transform_diffs_to_table(diffs)
for arg in sys.argv[1:]:
email_adr = re.match('^(' + MAIL_PATTERN + ')\{.*\}$', arg).group(1)
filters = re.match('.*\{(.*)\}', arg).group(1)
table = filter_table(table, filters)
if len(table) < 1:
continue
response_page = build_response_page(table)
send_email('Aktuelle Verkehrsmeldungen', 'KVV Ticker', response_page, email_adr)
def transform_diffs_to_table(diffs):
table = []
for diff in diffs:
dict_ = {
'date': diff.find('span', class_='date').text,
'subject': diff.find('h3', class_='ticker_subject').text,
'content': diff.find('p').text,
'details_link': 'https://www.kvv.de' + diff.find('a', class_='internal-link')['href']
}
table.append(dict_)
return table
def filter_table(table, filters):
def contains_all(string, substring_list):
for substring in substring_list:
if not substring in string:
return False
return True
filtered_table = []
for row in table:
joined_row = row['subject'] + row['content']
for and_filters in filters.split('|'):
if contains_all(joined_row, and_filters.split('&')):
filtered_table.append(row)
break
return filtered_table
def build_response_page(table):
with open('template.html') as f:
template = Template(f.read())
return template.render(table=table)
def arguments_valid():
if len(sys.argv) < 2:
print('[ERROR] arguments are missing')
return False
for arg in sys.argv[1:]:
matches = re.match(ARG_PATTERN, arg)
if not matches:
print('[ERROR] incorrect argument pattern, use mail@domain.com{string1|string2&string3}')
return False
return True
if __name__ == '__main__':
run()