mirror of
https://github.com/serega404/VodokanalBot.git
synced 2026-07-01 17:39:17 +03:00
Compare commits
2 Commits
23ab8113cf
...
03a4ad5d8e
| Author | SHA1 | Date | |
|---|---|---|---|
| 03a4ad5d8e | |||
| 6f2b27f00e |
@@ -11,6 +11,13 @@ env:
|
||||
jobs:
|
||||
build-and-push-image:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
image:
|
||||
- suffix: ''
|
||||
start_file: start_telegram.py
|
||||
- suffix: '-meshcore-ha'
|
||||
start_file: start_meshcore_ha.py
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
@@ -30,12 +37,14 @@ jobs:
|
||||
id: meta
|
||||
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
|
||||
with:
|
||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}${{ matrix.image.suffix }}
|
||||
|
||||
- name: Build and push Docker image
|
||||
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
|
||||
with:
|
||||
context: .
|
||||
build-args: |
|
||||
START_FILE=${{ matrix.image.start_file }}
|
||||
push: true
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
@@ -0,0 +1,2 @@
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
+4
-2
@@ -1,8 +1,9 @@
|
||||
FROM python:3.11.2-alpine3.17
|
||||
FROM python:3.12-alpine
|
||||
|
||||
LABEL Maintainer="serega404"
|
||||
|
||||
WORKDIR /app
|
||||
ARG START_FILE=start_telegram.py
|
||||
|
||||
COPY requirements.txt requirements.txt
|
||||
RUN pip3 install -r requirements.txt
|
||||
@@ -11,7 +12,8 @@ RUN pip3 install -r requirements.txt
|
||||
COPY crontab /tmp/crontab
|
||||
RUN cat /tmp/crontab > /etc/crontabs/root
|
||||
|
||||
COPY main.py main.py
|
||||
COPY ${START_FILE} start.py
|
||||
COPY parser.py parser.py
|
||||
|
||||
# run crond as main process of container
|
||||
CMD ["crond", "-f", "-l", "2"]
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
[](https://github.com/serega404/VodokanalBot)
|
||||
|
||||
## Запуск в Docker
|
||||
## Запуск TG интеграции в Docker
|
||||
|
||||
``` Docker
|
||||
docker volume create vodokanal_bot_data
|
||||
@@ -15,7 +15,7 @@ docker run -d --name VodokanalBot \
|
||||
ghcr.io/serega404/vodokanalbot:main
|
||||
```
|
||||
|
||||
## Запуск в Docker Compose
|
||||
### Запуск в Docker Compose
|
||||
|
||||
Укажи `TELEGRAM_TOKEN` и `TELEGRAM_CHANNEL` в [`docker-compose.yml`](./docker-compose.yml), затем запусти:
|
||||
|
||||
@@ -23,6 +23,51 @@ docker run -d --name VodokanalBot \
|
||||
docker compose up -d --build
|
||||
```
|
||||
|
||||
## Запуск с MeshCore + Home Assistant
|
||||
|
||||
Вариант для MeshCore отправляет новые сообщения в webhook Home Assistant, а Home Assistant пересылает их в канал MeshCore.
|
||||
|
||||
1. Добавь в Home Assistant автоматизацию [`Webhook to MeshCore channel`](https://github.com/serega404/HA-MeshCore#webhook-to-meshcore-channel).
|
||||
2. Замени `CHANGE_ME_RANDOM_WEBHOOK_ID` в автоматизации на случайную строку.
|
||||
|
||||
``` 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 HOME_ASSISTANT_WEBHOOK_ID='CHANGE_ME_RANDOM_WEBHOOK_ID' \
|
||||
-e HOME_ASSISTANT_URL='http://homeassistant.local:8123' \
|
||||
-e HOME_ASSISTANT_WEBHOOK_CHANNEL='0' \
|
||||
ghcr.io/serega404/vodokanalbot-meshcore-ha:main
|
||||
```
|
||||
|
||||
### Запуск в Docker Compose
|
||||
|
||||
Укажи `HOME_ASSISTANT_WEBHOOK_ID` и `HOME_ASSISTANT_WEBHOOK_CHANNEL` в [`docker-compose.ha.yml`](./docker-compose.ha.yml), затем запусти:
|
||||
|
||||
``` Docker
|
||||
docker compose up -d --build -f docker-compose.ha.yml
|
||||
```
|
||||
|
||||
## Интеграции
|
||||
|
||||
Общая логика парсинга, работы с `data/db.json` и поиска новых сообщений вынесена в [`parser.py`](./parser.py).
|
||||
|
||||
Для новой интеграции достаточно создать свой адаптер отправки и передать его в `publish_new_posts`:
|
||||
|
||||
``` Python
|
||||
from parser import create_session, publish_new_posts
|
||||
|
||||
session = create_session()
|
||||
|
||||
publish_new_posts(
|
||||
send_message=lambda message: print(message),
|
||||
session=session,
|
||||
url="http://www.tgnvoda.ru/avarii.php",
|
||||
)
|
||||
```
|
||||
|
||||
## Библиотеки
|
||||
|
||||
* [Requests](https://requests.readthedocs.io/en/latest/)
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
@reboot cd /app && python3 /app/main.py
|
||||
*/10 * * * * cd /app && python3 /app/main.py
|
||||
@reboot cd /app && python3 /app/start.py
|
||||
*/10 * * * * cd /app && python3 /app/start.py
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
services:
|
||||
vodokanalbot-ha:
|
||||
image: ghcr.io/serega404/vodokanalbot-meshcore-ha:main
|
||||
build:
|
||||
context: .
|
||||
args:
|
||||
START_FILE: start_meshcore_ha.py
|
||||
container_name: VodokanalBotMeshCoreHA
|
||||
restart: always
|
||||
environment:
|
||||
TZ: Europe/Moscow
|
||||
HOME_ASSISTANT_URL: https://your-home-assistant-url
|
||||
HOME_ASSISTANT_WEBHOOK_ID: CHANGE_ME_RANDOM_WEBHOOK_ID
|
||||
HOME_ASSISTANT_WEBHOOK_CHANNEL: "0"
|
||||
# VODOKANAL_URL: http://www.tgnvoda.ru/avarii.php
|
||||
# PROXY_URL: socks5h://user:password@proxy-host:1080
|
||||
# PROXY_URL: http://user:password@proxy-host:3128
|
||||
volumes:
|
||||
- vodokanal_bot_data:/app/data
|
||||
|
||||
volumes:
|
||||
vodokanal_bot_data:
|
||||
@@ -1,12 +1,17 @@
|
||||
services:
|
||||
vodokanalbot:
|
||||
image: ghcr.io/serega404/vodokanalbot:main
|
||||
build:
|
||||
context: .
|
||||
args:
|
||||
START_FILE: start_telegram.py
|
||||
container_name: VodokanalBot
|
||||
restart: always
|
||||
environment:
|
||||
TZ: Europe/Moscow
|
||||
TELEGRAM_TOKEN: TOKEN
|
||||
TELEGRAM_CHANNEL: CHAT_ID
|
||||
# VODOKANAL_URL: http://www.tgnvoda.ru/avarii.php
|
||||
# PROXY_URL: socks5h://user:password@proxy-host:1080
|
||||
# PROXY_URL: http://user:password@proxy-host:3128
|
||||
volumes:
|
||||
|
||||
@@ -1,101 +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', '')
|
||||
PROXY_URL = os.environ.get('PROXY_URL', '')
|
||||
|
||||
if TELEGRAM_TOKEN == '':
|
||||
print("Telegram token is not set")
|
||||
exit()
|
||||
|
||||
if TELEGRAM_CHANNEL == '':
|
||||
print("Telegram channel is not set")
|
||||
exit()
|
||||
|
||||
# Configure HTTP client
|
||||
|
||||
session = requests.Session()
|
||||
|
||||
if PROXY_URL != '':
|
||||
session.proxies.update({
|
||||
'http': PROXY_URL,
|
||||
'https': PROXY_URL,
|
||||
})
|
||||
|
||||
# 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 = session.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 = session.get(
|
||||
"https://api.telegram.org/bot" + TELEGRAM_TOKEN + "/sendMessage",
|
||||
params={
|
||||
'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 not 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")
|
||||
@@ -0,0 +1,109 @@
|
||||
import json
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
|
||||
DEFAULT_DB_PATH = "data/db.json"
|
||||
DEFAULT_VODOKANAL_URL = "http://www.tgnvoda.ru/avarii.php"
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Post:
|
||||
date: str
|
||||
text: str
|
||||
|
||||
@property
|
||||
def key(self):
|
||||
return self.date + "$" + self.text
|
||||
|
||||
|
||||
def create_session(proxy_url=""):
|
||||
session = requests.Session()
|
||||
|
||||
if proxy_url:
|
||||
session.proxies.update({
|
||||
"http": proxy_url,
|
||||
"https": proxy_url,
|
||||
})
|
||||
|
||||
return session
|
||||
|
||||
def load_database(path=DEFAULT_DB_PATH):
|
||||
if not os.path.isfile(path):
|
||||
print("Database not loaded")
|
||||
return None
|
||||
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
def save_database(posts, path=DEFAULT_DB_PATH):
|
||||
directory = os.path.dirname(path)
|
||||
if directory and not os.path.exists(directory):
|
||||
os.makedirs(directory)
|
||||
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
json.dump([post.key for post in posts], f, ensure_ascii=False)
|
||||
print("Database updated")
|
||||
|
||||
|
||||
def fetch_posts(session, url, today=None):
|
||||
req = session.get(url)
|
||||
|
||||
if req.status_code != 200:
|
||||
raise RuntimeError("Request error: " + str(req.status_code))
|
||||
|
||||
return parse_posts(req.content, today=today)
|
||||
|
||||
|
||||
def parse_posts(content, today=None):
|
||||
today = today or datetime.today()
|
||||
soup = BeautifulSoup(content, "html.parser")
|
||||
|
||||
posts = []
|
||||
for tag in soup.find_all("font", size="2", face="VERDANA"):
|
||||
date = tag.select_one("font:nth-of-type(1)").b.text
|
||||
if not is_today(date, today):
|
||||
continue
|
||||
|
||||
text = tag.select_one("font:nth-of-type(2)").text.replace("\n", "")
|
||||
posts.append(Post(date=date, text=text))
|
||||
|
||||
return posts
|
||||
|
||||
|
||||
def is_today(date, today):
|
||||
day, month = date.split(".")[:2]
|
||||
return day == str(today.day).zfill(2) and month == str(today.month).zfill(2)
|
||||
|
||||
|
||||
def get_new_posts(posts, database):
|
||||
if database is None:
|
||||
return posts
|
||||
|
||||
database_keys = set(database)
|
||||
return [post for post in posts if post.key not in database_keys]
|
||||
|
||||
|
||||
def publish_new_posts(send_message, session, db_path=DEFAULT_DB_PATH):
|
||||
database = load_database(db_path)
|
||||
posts = fetch_posts(session, DEFAULT_VODOKANAL_URL)
|
||||
|
||||
if not posts:
|
||||
print("No posts")
|
||||
return
|
||||
|
||||
print("The number of posts for this day:", len(posts))
|
||||
|
||||
new_posts = get_new_posts(posts, database)
|
||||
if not new_posts:
|
||||
print("No new posts")
|
||||
return
|
||||
|
||||
for post in new_posts:
|
||||
send_message(post.text)
|
||||
|
||||
save_database(posts, db_path)
|
||||
@@ -0,0 +1,56 @@
|
||||
import os
|
||||
|
||||
from parser import create_session, publish_new_posts
|
||||
|
||||
|
||||
PROXY_URL = os.environ.get('PROXY_URL', '')
|
||||
HOME_ASSISTANT_URL = os.environ.get('HOME_ASSISTANT_URL', '')
|
||||
HOME_ASSISTANT_WEBHOOK_ID = os.environ.get('HOME_ASSISTANT_WEBHOOK_ID', '')
|
||||
HOME_ASSISTANT_WEBHOOK_CHANNEL = os.environ.get('HOME_ASSISTANT_WEBHOOK_CHANNEL', '0')
|
||||
|
||||
|
||||
def create_webhook_url():
|
||||
return (
|
||||
HOME_ASSISTANT_URL.rstrip('/')
|
||||
+ "/api/webhook/"
|
||||
+ HOME_ASSISTANT_WEBHOOK_ID
|
||||
)
|
||||
|
||||
|
||||
def send_webhook_message(session, message):
|
||||
req = session.get(
|
||||
create_webhook_url(),
|
||||
params={
|
||||
'channel': HOME_ASSISTANT_WEBHOOK_CHANNEL,
|
||||
'message': message,
|
||||
},
|
||||
)
|
||||
if not 200 <= req.status_code < 300:
|
||||
print("Home Assistant webhook request error: " + str(req.status_code))
|
||||
exit()
|
||||
else:
|
||||
print("Home Assistant webhook message sent")
|
||||
|
||||
|
||||
def main():
|
||||
if HOME_ASSISTANT_URL == '':
|
||||
print("Home Assistant URL is not set")
|
||||
exit()
|
||||
|
||||
if HOME_ASSISTANT_WEBHOOK_ID == '':
|
||||
print("Home Assistant webhook id is not set")
|
||||
exit()
|
||||
|
||||
session = create_session(PROXY_URL)
|
||||
try:
|
||||
publish_new_posts(
|
||||
send_message=lambda message: send_webhook_message(session, message),
|
||||
session=session,
|
||||
)
|
||||
except RuntimeError as error:
|
||||
print(error)
|
||||
exit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,49 @@
|
||||
import os
|
||||
|
||||
from parser import create_session, publish_new_posts
|
||||
|
||||
|
||||
SEND_SILENT = os.environ.get('SEND_SILENT', False)
|
||||
TELEGRAM_TOKEN = os.environ.get('TELEGRAM_TOKEN', '')
|
||||
TELEGRAM_CHANNEL = os.environ.get('TELEGRAM_CHANNEL', '')
|
||||
PROXY_URL = os.environ.get('PROXY_URL', '')
|
||||
|
||||
|
||||
def send_telegram_message(session, message):
|
||||
req = session.get(
|
||||
"https://api.telegram.org/bot" + TELEGRAM_TOKEN + "/sendMessage",
|
||||
params={
|
||||
'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']))
|
||||
|
||||
|
||||
def main():
|
||||
if TELEGRAM_TOKEN == '':
|
||||
print("Telegram token is not set")
|
||||
exit()
|
||||
|
||||
if TELEGRAM_CHANNEL == '':
|
||||
print("Telegram channel is not set")
|
||||
exit()
|
||||
|
||||
session = create_session(PROXY_URL)
|
||||
try:
|
||||
publish_new_posts(
|
||||
send_message=lambda message: send_telegram_message(session, message),
|
||||
session=session,
|
||||
)
|
||||
except RuntimeError as error:
|
||||
print(error)
|
||||
exit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user