Move to pydantic settings paradigm

This commit is contained in:
Roman Krček
2024-10-13 19:47:06 +02:00
parent c68d3d8722
commit d236f88b64
4 changed files with 68 additions and 19 deletions

View File

@@ -5,4 +5,5 @@ pyrogram==2.0.106
tiktok_downloader==0.3.5
uvloop==0.19.0
tgcrypto==1.2.5
sentry-sdk==2.15.0
sentry-sdk==2.15.0
pydantic-settings==2.5.2

View File

@@ -1,4 +1,3 @@
import os
import uvloop
from pyrogram import Client, filters
@@ -7,20 +6,18 @@ from pyrogram.types import Message
from telegram_downloader_bot.logger import log
from telegram_downloader_bot.telemetry import init_telemetry
from telegram_downloader_bot import utils, security
API_ID = os.getenv("API_ID") # Your API ID from my.telegram.org
API_HASH = os.getenv("API_HASH") # Your API Hash from my.telegram.org
BOT_TOKEN = os.getenv("BOT_TOKEN") # Your bot token from BotFather
STORAGE = os.getenv("STORAGE") # Your bot token from BotFather
from telegram_downloader_bot.settings import settings
uvloop.install()
init_telemetry()
if settings.app_env == "PROD":
init_telemetry()
app = Client("downloader_bot",
api_id=API_ID,
api_hash=API_HASH,
bot_token=BOT_TOKEN,
workers=1)
api_id=settings.api_id,
api_hash=settings.api_hash,
bot_token=settings.bot_token,
workers=settings.workers)
@app.on_message(filters.command("start"))
@@ -59,7 +56,7 @@ async def message_handler(_, message: Message):
msg = f"Downloading video {i+1}/{len(urls)}..."
log.info(msg)
await message.reply_text(msg)
utils.download_tt_video(STORAGE, url)
utils.download_tt_video(settings.storage, url)
await message.reply_text("Done.")
@@ -70,9 +67,9 @@ async def media_handler(client, message: Message):
await message.reply_text("Downloading media...")
utils.handle_media_message_contents(STORAGE, client, message)
utils.handle_media_message_contents(settings.storage, client, message)
if __name__ == "__main__":
utils.make_fs(STORAGE)
utils.make_fs(settings.storage)
app.run()

View File

@@ -1,11 +1,9 @@
import os
from functools import wraps
from telegram_downloader_bot.logger import log
from telegram_downloader_bot.settings import settings
# Comma separated list of Telegram IDs that this bot will respond to
allowed_ids_raw = os.getenv("ALLOWED_IDS", "")
allowed_ids = allowed_ids_raw.split(",")
allowed_ids = settings.allowed_ids.split(",")
allowed_ids = [int(x) for x in allowed_ids]

View File

@@ -0,0 +1,53 @@
import os
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
"""
Settings class that defines configuration variables for the application.
Attributes:
----------
app_env : str
Specifies the environment in which the application is running.
Default is 'DEV'. Possible values could include 'DEV', 'PROD'
workers : int
Defines the number of workers to be used in the application.
Default is 1.
api_id : int
Represents the API ID from my.telegram.org
api_hash : str
The hash key corresponding to your API Hash from my.telegram.org
bot_token : str
The token from BotFather.
storage : os.path
Specifies the path where the application stores persistent data.
allowed_ids : str
A list or comma-separated string of IDs that are allowed access
to the bot or application.
Config:
-------
env_file : str
Specifies the environment file to load the environment variables from.
Default is ".env".
"""
app_env: str = "DEV"
workers: int = 1
api_id: int
api_hash: str
bot_token: str
storage: os.path
allowed_ids: str
class Config:
env_file = ".env"
settings = Settings()