mirror of
https://github.com/serega404/VodokanalBot.git
synced 2025-04-21 22:40:46 +03:00
Compare commits
No commits in common. "2a968f35285f8aa6ad367d1d061f86f4a8e62215" and "180875a42d65df90e17c98c964008d9b78a9a442" have entirely different histories.
2a968f3528
...
180875a42d
76
main.py
76
main.py
@ -1,76 +0,0 @@
|
|||||||
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) {
|
foreach ($html->find('td[bgcolor="#ffffff"]') as $e) {
|
||||||
$date = "";
|
$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);
|
preg_match("/\\d{2}\\.\\d{2}\\.\\d{4}/", $e->plaintext, $date);
|
||||||
|
|
||||||
if (strcasecmp(reset($date), $dateNow) == 0) { // get current date posts
|
if (strcasecmp(reset($date), $dateNow) == 0) { // get current date posts
|
||||||
// remove date from content
|
// remove date from content
|
||||||
@ -39,7 +39,7 @@ foreach ($html->find('td[bgcolor="#ffffff"]') as $e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($all_elements == null) {
|
if ($all_elements == null) {
|
||||||
echo "Posts not found\n";
|
echo "Posts not dound\n";
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user