-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·84 lines (67 loc) · 2.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import asyncio
import contextlib
import logging
import telegram
from dotenv import load_dotenv
from pydantic import Field
from pydantic_settings import BaseSettings
from telegram import Bot
from telegram.constants import ParseMode
from passabot.authenticators import AuthData, Authenticator, Credentials
from passabot.scraper_api import ApiScraper
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("passabot.log"),
logging.StreamHandler(),
],
)
logger = logging.getLogger(__name__)
class Secrets(BaseSettings):
SPID_SESSION_ID: str = Field(default=...)
CSRF_TOKEN: str = Field(default=...)
SPID_USERNAME: str = Field(default=...)
SPID_PASSWORD: str = Field(default=...)
TARGET_PROVINCE: str = Field(default=...)
TELEGRAM_BOT_TOKEN: str = Field(default=...)
TELEGRAM_DATA_CHAT_ID: str = Field(default=...)
TELEGRAM_CONTROL_CHAT_ID: str = Field(default=...)
def get_credentials(self) -> Credentials:
return Credentials(
username=self.SPID_USERNAME,
password=self.SPID_PASSWORD,
)
def get_auth_data(self) -> AuthData:
return AuthData(
csrf_token=self.CSRF_TOKEN,
session_id=self.SPID_SESSION_ID,
)
async def handle_error(bot: Bot, chat_id: str, e: Exception) -> None:
message = f"An error occurred:\n\n<code>{e}</code>"
while True:
try:
await bot.send_message(chat_id=chat_id, text=message, parse_mode=ParseMode.HTML) # pyright: ignore[reportCallIssue]
break
except telegram.error.BadRequest:
logger.exception("An error occurred while sending the error message")
await asyncio.sleep(5)
async def main() -> None:
load_dotenv()
secrets = Secrets()
authenticator = Authenticator(secrets.get_credentials())
scraper = ApiScraper(authenticator, secrets.TARGET_PROVINCE)
await scraper.login()
async with Bot(secrets.TELEGRAM_BOT_TOKEN) as bot:
await bot.send_message(chat_id=secrets.TELEGRAM_CONTROL_CHAT_ID, text="Bot started") # pyright: ignore[reportCallIssue]
try:
await asyncio.create_task(
scraper.check_availability(bot, secrets.TELEGRAM_DATA_CHAT_ID, secrets.TELEGRAM_CONTROL_CHAT_ID),
)
except Exception as e:
await handle_error(bot, secrets.TELEGRAM_CONTROL_CHAT_ID, e)
raise
if __name__ == "__main__":
with contextlib.suppress(KeyboardInterrupt):
asyncio.run(main())