implemented weather job

This commit is contained in:
bvn13 2024-07-19 00:45:23 +03:00
parent 08fa257545
commit bcc43c3ff5
10 changed files with 131 additions and 12 deletions

View File

@ -50,5 +50,8 @@
}
}
]
},
"weather": {
"shkolniy": -1001699667908
}
}

View File

@ -23,5 +23,8 @@
}
}
]
},
"weather": {
"shkolniy": -1002184916546
}
}

View File

@ -1,3 +1,5 @@
aiohttp==3.9.5
telethon==1.36.0
cryptg==0.4.0
cryptg==0.4.0
scheduler==0.8.7
requests-html==0.10.0

View File

@ -1,7 +1,10 @@
import asyncio
from settings import config
import logging
import re
from queue import Queue, Empty
from telethon import TelegramClient, events
from bot.message import WeatherTask
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
@ -20,6 +23,7 @@ class Bot():
self.__owner_id = owner_id
self.__reload()
self.__bot = TelegramClient(session_file, api_id, api_hash)
self.__weather_queue = Queue()
@self.__bot.on(events.NewMessage(incoming=True, pattern='!whoami'))
async def onMessageWhoami(event):
@ -63,10 +67,31 @@ class Bot():
logger.info("Received message in chat {0}, id {1}: {2}".format(chat_id, message.id, message.text if not message == None else "<No Text>"))
await self.__process_message(event, chat_id, message)
def start(self) -> None:
self.__bot.start()
self.__bot.loop.run_until_complete(self.__identity())
self.__bot.run_until_disconnected()
async def start(self) -> None:
await self.__bot.start()
await self.__identity()
self.__bot.loop.create_task(self.__task_loop())
await self.__bot.run_until_disconnected()
def send_weather(self, task: WeatherTask) -> None:
self.__weather_queue.put(task)
async def __task_loop(self) -> None:
while True:
logger.debug("Looking for weather jobs")
weather_task = None
try:
weather_task = self.__weather_queue.get_nowait()
except Empty:
pass
if weather_task is not None:
await self.__send_weather(weather_task.get_key(), weather_task.get_message())
await asyncio.sleep(1)
async def __send_weather(self, key: str, message: str) -> None:
dest_chat_id = self.__config.get_weather_destination_chat_id(key)
if dest_chat_id is not None:
await self.__bot.send_message(dest_chat_id, message, parse_mode='html')
def __reload(self) -> None:
self.__config = config.Config(filename=self.__config_file)

12
src/bot/message.py Normal file
View File

@ -0,0 +1,12 @@
class WeatherTask:
def __init__(self, key: str, message: str) -> None:
self.__key = key
self.__message = message
def get_key(self) -> str:
return self.__key
def get_message(self) -> str:
return self.__message

11
src/jobs/__init__.py Normal file
View File

@ -0,0 +1,11 @@
import sys
import os
# Get the current script's directory
current_dir = os.path.dirname(os.path.abspath(__file__))
# Get the parent directory by going one level up
parent_dir = os.path.dirname(current_dir)
# Add the parent directory to sys.path
sys.path.append(parent_dir)

14
src/jobs/weather.py Normal file
View File

@ -0,0 +1,14 @@
from requests_html import HTMLSession
class Weather:
def __init__(self) -> None:
pass
def get_shkolniy(self):
url = "https://a-weather.ru/place/ru-shkolnij-12/"
session = HTMLSession()
response = session.get(url)
return "\n".join([
response.html.find('.weather_about', first=True).text,
response.html.find('.last_update', first=True).text
])

View File

@ -1,11 +1,19 @@
import os
from bot.bot import Bot
import asyncio
import datetime as dt
import logging
import os
from scheduler import Scheduler
import time
import threading
from bot.bot import Bot
from bot.message import WeatherTask
from jobs.weather import Weather
logging.basicConfig(format="%(asctime)s | %(name)s | %(levelname)s | %(message)s")
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
if __name__ == "__main__":
session_file = os.environ['SESSION_FILE']
api_id = os.environ['TG_APP_ID']
@ -17,4 +25,37 @@ if __name__ == "__main__":
api_hash=api_hash,
config_file=config_file,
owner_id=owner_id)
bot.start()
def start_bot():
asyncio.set_event_loop(asyncio.new_event_loop())
loop = asyncio.get_event_loop()
# Your asyncio code here
loop.run_until_complete(bot.start())
loop.close()
def shkolniy_weather_job():
logger.info("Shkolniy weather job")
weather = Weather()
shkolniy_weather = str(weather.get_shkolniy())
if shkolniy_weather is None:
logger.warn("Unable to get weather")
rows = shkolniy_weather.split('\n')
n = len(rows)-1
rows[0] = "<b>{0}</b>".format(rows[0])
rows[n] = "<i>{0}</i>".format(rows[n])
shkolniy_weather = '\n\n'.join(rows)
bot.send_weather(WeatherTask('shkolniy', "{0}".format(shkolniy_weather)))
def scheduled():
schedule = Scheduler()
# schedule.cyclic(dt.timedelta(seconds=10), shkolniy_weather_job)
schedule.daily(dt.time(hour=0, minute=24), shkolniy_weather_job)
while True:
schedule.exec_jobs()
time.sleep(1)
thread = threading.Thread(target=start_bot)
thread.start()
thread2 = threading.Thread(target=scheduled)
thread2.start()

View File

@ -8,6 +8,10 @@ class Config():
self.__config = {}
self.__read()
def read_as_text(self) -> str:
with open(self.__filename, 'r') as f:
return ''.join(f.readlines())
def get_on_message_for_chat_id(self, chat_id: int) -> Optional[dict]:
chat_id_str = str(chat_id)
if 'onMessage' in self.__config:
@ -16,9 +20,13 @@ class Config():
return on_message[chat_id_str]
return None
def read_as_text(self) -> str:
with open(self.__filename, 'r') as f:
return ''.join(f.readlines())
def get_weather_destination_chat_id(self, key: str) -> Optional[int]:
key_str = str(key)
if 'weather' in self.__config:
weather = self.__config['weather']
if key_str in weather:
return weather[key_str]
return None
def __read(self) -> None:
with open(self.__filename, 'r') as f:

View File

@ -1 +1 @@
0.1.3
0.2.0