-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.py
executable file
·132 lines (106 loc) · 3.31 KB
/
check.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
126
127
128
129
130
131
132
#!/usr/bin/env python
import hashlib
import os
import re
import time
import requests
from dateutil import parser
from lxml import html
from slack_webhook import Slack
import json
import logging
logging.basicConfig(level=logging.INFO)
CACHE_FILE = os.getenv('CACHE_FILE', '/tmp/cache.json')
SLACK_WEBHOOK_URL = os.getenv('SLACK_WEBHOOK_URL')
SLACK_CHANNEL = os.getenv('SLACK_CHANNEL', '#sandbox')
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0',
'Referer': 'https://nijz.si/nalezljive-bolezni/koronavirus/spremljanje-okuzb-s-sars-cov-2-covid-19/'
}
if SLACK_WEBHOOK_URL:
slack = Slack(url=SLACK_WEBHOOK_URL)
else:
slack = None
print("Starting bot: Cache file {}, chanel {}, webhook {}".format(CACHE_FILE, SLACK_CHANNEL, SLACK_WEBHOOK_URL))
def parse_page():
resp = requests.get(
'https://nijz.si/nalezljive-bolezni/koronavirus/spremljanje-okuzb-s-sars-cov-2-covid-19/', headers=headers)
resp.raise_for_status()
tree = html.fromstring(resp.content)
date = parser.parse(tree.xpath("//div[@class='date-modified']/text()")[0].lstrip('Zadnje posodobljeno: '))
links = tree.xpath(
"//p/a[contains(@href, '/wp-content/uploads/')]/@href")
filtered_links = list(filter(lambda url: url.endswith('.xlsx') and 'DNEVNI' in url.upper(), links))
return (date, filtered_links)
def file_hash(url):
resp = requests.get(url, headers=headers)
resp.raise_for_status()
m = hashlib.sha256()
m.update(resp.content)
return m.hexdigest()
def notify_new(url, extra=None):
text = ""
blocks = [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Changes on NIJZ webpage detected"
}
}
]
if isinstance(url, str):
text = "New file: {}".format(url)
if isinstance(url, set):
text = "New files: {}".format(', '.join(url))
for u in url:
blocks.append(
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": u
}
}
)
logging.info("Notification: {}".format(text))
if slack:
slack.post(
username="NIJZ bot",
channel=SLACK_CHANNEL,
blocks=blocks
)
else:
print("No slack")
def load_cache():
try:
with open(CACHE_FILE, 'r') as f:
return set(json.load(f))
except FileNotFoundError:
return set()
except json.decoder.JSONDecodeError:
os.remove(CACHE_FILE)
return set()
def persist_cache(data):
with open(CACHE_FILE, 'w') as f:
json.dump(list(data), f)
def loop():
logging.info("Staring NIJZ checker")
last_hash = load_cache()
while True:
logging.info('Parsing NIZJ page')
date, links = parse_page()
new_hash = set()
new_files = set()
for link in links:
new_hash.add(file_hash(link))
new_files.add(link)
if new_hash != last_hash:
notify_new(new_files)
else:
logging.info('Nothing new')
last_hash = new_hash
persist_cache(last_hash)
time.sleep(300)
if __name__ == '__main__':
loop()