mirror of
https://github.com/serega404/VodokanalBot.git
synced 2025-07-03 21:53:36 +03:00
Compare commits
8 Commits
180875a42d
...
main
Author | SHA1 | Date | |
---|---|---|---|
edb51bb696
|
|||
758c090824
|
|||
132a9d81ee
|
|||
80af7ec775
|
|||
6d65395b89
|
|||
5388901294
|
|||
2a968f3528
|
|||
4ab92b65a0
|
41
.github/workflows/github-push-docker.yml
vendored
Normal file
41
.github/workflows/github-push-docker.yml
vendored
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
name: Create and publish a Docker image
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: ['main']
|
||||||
|
|
||||||
|
env:
|
||||||
|
REGISTRY: ghcr.io
|
||||||
|
IMAGE_NAME: ${{ github.repository }}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-and-push-image:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
packages: write
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Log in to the Container registry
|
||||||
|
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
|
||||||
|
with:
|
||||||
|
registry: ${{ env.REGISTRY }}
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Extract metadata (tags, labels) for Docker
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
|
||||||
|
with:
|
||||||
|
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||||
|
|
||||||
|
- name: Build and push Docker image
|
||||||
|
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
17
Dockerfile
Normal file
17
Dockerfile
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
FROM python:3.11.2-alpine3.17
|
||||||
|
|
||||||
|
LABEL Maintainer="serega404"
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY requirements.txt requirements.txt
|
||||||
|
RUN pip3 install -r requirements.txt
|
||||||
|
|
||||||
|
# Setting up crontab
|
||||||
|
COPY crontab /tmp/crontab
|
||||||
|
RUN cat /tmp/crontab > /etc/crontabs/root
|
||||||
|
|
||||||
|
COPY main.py main.py
|
||||||
|
|
||||||
|
# run crond as main process of container
|
||||||
|
CMD ["crond", "-f", "-l", "2"]
|
24
README.md
24
README.md
@ -1 +1,25 @@
|
|||||||
# VodokanalParser
|
# VodokanalParser
|
||||||
|
|
||||||
|
[](https://github.com/serega404/VodokanalBot)
|
||||||
|
|
||||||
|
### Запуск в Docker
|
||||||
|
|
||||||
|
``` Docker
|
||||||
|
docker volume create vodokanal_bot_data
|
||||||
|
docker run -d --name VodokanalBot \
|
||||||
|
--restart=always \
|
||||||
|
-v vodokanal_bot_data:/app/data \
|
||||||
|
-e TZ='Europe/Moscow' \
|
||||||
|
-e TELEGRAM_TOKEN='TOKEN' \
|
||||||
|
-e TELEGRAM_CHANNEL='CHAT_ID' \
|
||||||
|
ghcr.io/serega404/vodokanalbot:main
|
||||||
|
```
|
||||||
|
|
||||||
|
### Библиотеки
|
||||||
|
|
||||||
|
* [Requests](https://requests.readthedocs.io/en/latest/)
|
||||||
|
* [BeautifulSoup](https://www.crummy.com/software/BeautifulSoup/)
|
||||||
|
|
||||||
|
### Лицензия
|
||||||
|
|
||||||
|
Распространяется под MIT License. Смотри файл [`LICENSE`](./LICENSE) для того что бы узнать подробности.
|
||||||
|
2
crontab
Normal file
2
crontab
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
@reboot cd /app && python3 /app/main.py
|
||||||
|
*/10 * * * * cd /app && python3 /app/main.py
|
83
main.py
Normal file
83
main.py
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
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('data/db.json')):
|
||||||
|
with open('data/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(date + "$" + 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 = set(elements) - set(db)
|
||||||
|
if diff == []:
|
||||||
|
print("No new posts")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
for i in diff:
|
||||||
|
send_message(i.split("$",1)[1])
|
||||||
|
else:
|
||||||
|
for element in elements:
|
||||||
|
send_message(element.split("$",1)[1])
|
||||||
|
|
||||||
|
# Save database
|
||||||
|
|
||||||
|
if not os.path.exists("data"):
|
||||||
|
os.makedirs("data")
|
||||||
|
|
||||||
|
with open('data/db.json', 'w', encoding='utf-8') as f:
|
||||||
|
json.dump(elements, f, ensure_ascii=False)
|
||||||
|
print("Database updated")
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
requests
|
||||||
|
beautifulsoup4
|
@ -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("/\\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
|
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 dound\n";
|
echo "Posts not found\n";
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user