mirror of
https://github.com/serega404/VodokanalBot.git
synced 2025-04-21 14:30:46 +03:00
Compare commits
2 Commits
180875a42d
...
2a968f3528
Author | SHA1 | Date | |
---|---|---|---|
2a968f3528 | |||
4ab92b65a0 |
76
main.py
Normal file
76
main.py
Normal file
@ -0,0 +1,76 @@
|
||||
import requests, json, os
|
||||
from bs4 import BeautifulSoup
|
||||
from datetime import datetime
|
||||
|
||||
# Config
|
||||
|
||||
URL = os.environ.get('VODOKANAL_URL', 'http://www.tgnvoda.ru/avarii.php')
|
||||
SEND_SILENT = os.environ.get('SEND_SILENT', False)
|
||||
TELEGRAM_TOKEN = os.environ.get('TELEGRAM_TOKEN', '')
|
||||
TELEGRAM_CHANNEL = os.environ.get('TELEGRAM_CHANNEL', '')
|
||||
|
||||
if TELEGRAM_TOKEN == '':
|
||||
print("Telegram token is not set")
|
||||
exit()
|
||||
|
||||
if TELEGRAM_CHANNEL == '':
|
||||
print("Telegram channel is not set")
|
||||
exit()
|
||||
|
||||
# Load database
|
||||
|
||||
db = None
|
||||
if (os.path.isfile('db.json')):
|
||||
with open('db.json', 'r', encoding='utf-8') as f:
|
||||
db = json.load(f)
|
||||
else:
|
||||
print("Database not loaded")
|
||||
|
||||
# Get data
|
||||
|
||||
req = requests.get(URL)
|
||||
|
||||
if (req.status_code != 200):
|
||||
print("Request error: " + str(req.status_code))
|
||||
exit()
|
||||
|
||||
soup = BeautifulSoup(req.content, "html.parser")
|
||||
|
||||
elements = []
|
||||
for tag in soup.find_all('font', size='2', face='VERDANA'):
|
||||
date = tag.select_one('font:nth-of-type(1)').b.text
|
||||
if not(date.split('.')[0] == str(datetime.today().day).zfill(2) and date.split('.')[1] == str(datetime.today().month).zfill(2)):
|
||||
continue
|
||||
elements.append(tag.select_one('font:nth-of-type(2)').text.replace('\n', ''))
|
||||
|
||||
if elements == []:
|
||||
print("No posts")
|
||||
exit()
|
||||
|
||||
print("The number of posts for this day:", len(elements))
|
||||
|
||||
# Send telegram message
|
||||
|
||||
def send_message(message):
|
||||
req = requests.get("https://api.telegram.org/bot" + TELEGRAM_TOKEN + "/sendMessage?chat_id=" + TELEGRAM_CHANNEL + "&disable_notification=" + str(SEND_SILENT) + "&text=" + message)
|
||||
if (req.status_code != 200):
|
||||
print("Telegram request error: " + str(req.status_code))
|
||||
exit()
|
||||
else:
|
||||
print("Telegram message sent, mess id: " + str(req.json()['result']['message_id']))
|
||||
|
||||
# Compare db and elements
|
||||
|
||||
if db is not None:
|
||||
diff = list(set(elements).symmetric_difference(set(db)))
|
||||
for i in diff:
|
||||
send_message(i)
|
||||
else:
|
||||
for element in elements:
|
||||
send_message(element)
|
||||
|
||||
# Save database
|
||||
|
||||
with open('db.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(elements, f, ensure_ascii=False)
|
||||
print("Database updated")
|
@ -24,7 +24,7 @@ echo "Date now: " . $dateNow . "\n";
|
||||
|
||||
foreach ($html->find('td[bgcolor="#ffffff"]') as $e) {
|
||||
$date = "";
|
||||
preg_match("/\\d{2}\\.\\d{2}\\.\\d{4}/", $e->plaintext, $date);
|
||||
preg_match("/(0[1-9]|1[0-9]|2[0-9]|3[01])[.](0[1-9]|1[0-2])[.](20[0-9][0-9]|[0-9][0-9])/", $e->plaintext, $date);
|
||||
|
||||
if (strcasecmp(reset($date), $dateNow) == 0) { // get current date posts
|
||||
// remove date from content
|
||||
@ -39,7 +39,7 @@ foreach ($html->find('td[bgcolor="#ffffff"]') as $e) {
|
||||
}
|
||||
|
||||
if ($all_elements == null) {
|
||||
echo "Posts not dound\n";
|
||||
echo "Posts not found\n";
|
||||
exit;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user