yt-dlp-bot/app_bot/bot/core/config/schema.py

85 lines
2 KiB
Python
Raw Normal View History

2022-11-03 01:56:19 +08:00
from pydantic import (
StrictBool,
StrictInt,
StrictStr,
constr,
validator,
)
from yt_shared.enums import DownMediaType
2022-02-04 06:21:27 +08:00
from yt_shared.schemas.base import RealBaseModel
2022-06-11 04:35:48 +08:00
_LANG_CODE_LEN = 2
2022-10-14 03:55:18 +08:00
_LANG_CODE_REGEX = rf'^[a-z]{{{_LANG_CODE_LEN}}}$'
2022-06-11 04:35:48 +08:00
2022-02-04 06:21:27 +08:00
2023-04-17 04:32:47 +08:00
class AnonymousUserSchema(RealBaseModel):
2022-11-03 01:56:19 +08:00
id: StrictInt
@property
2023-04-17 04:32:47 +08:00
def is_anonymous_user(self) -> bool:
2022-11-03 01:56:19 +08:00
return True
2022-11-05 00:45:33 +08:00
class VideoCaptionSchema(RealBaseModel):
include_title: StrictBool
include_filename: StrictBool
include_link: StrictBool
2023-03-11 06:13:16 +08:00
include_size: StrictBool
2022-11-05 00:45:33 +08:00
2023-04-17 04:32:47 +08:00
class UploadSchema(RealBaseModel):
upload_video_file: StrictBool
2022-11-03 01:56:19 +08:00
upload_video_max_file_size: StrictInt
forward_to_group: StrictBool
forward_group_id: StrictInt | None
silent: StrictBool
2022-11-05 00:45:33 +08:00
video_caption: VideoCaptionSchema
2022-11-03 01:56:19 +08:00
2023-04-17 04:32:47 +08:00
class UserSchema(AnonymousUserSchema):
send_startup_message: StrictBool
download_media_type: DownMediaType
save_to_storage: StrictBool
2023-03-29 03:26:16 +08:00
use_url_regex_match: StrictBool
2023-04-17 04:32:47 +08:00
upload: UploadSchema
2022-11-03 01:56:19 +08:00
@property
2023-04-17 04:32:47 +08:00
def is_anonymous_user(self) -> bool:
2022-11-03 01:56:19 +08:00
return False
2023-04-17 04:32:47 +08:00
def _change_type(values: list[int]) -> list[AnonymousUserSchema]:
return [AnonymousUserSchema(id=id_) for id_ in values]
2022-11-03 01:56:19 +08:00
class ApiSchema(RealBaseModel):
upload_video_file: StrictBool
2022-11-03 01:56:19 +08:00
upload_video_max_file_size: StrictInt
2023-04-17 04:32:47 +08:00
upload_to_chat_ids: list[AnonymousUserSchema]
2022-11-03 01:56:19 +08:00
silent: StrictBool
2022-11-05 00:45:33 +08:00
video_caption: VideoCaptionSchema
2022-11-03 01:56:19 +08:00
2022-11-06 04:20:27 +08:00
_transform_chat_ids = validator('upload_to_chat_ids', pre=True)(_change_type)
2022-11-03 01:56:19 +08:00
2022-02-04 06:21:27 +08:00
class TelegramSchema(RealBaseModel):
2022-06-11 04:35:48 +08:00
api_id: StrictInt
api_hash: StrictStr
2022-02-04 06:21:27 +08:00
token: StrictStr
2022-06-11 04:35:48 +08:00
lang_code: constr(regex=_LANG_CODE_REGEX, to_lower=True)
2022-11-05 00:45:33 +08:00
max_upload_tasks: StrictInt
2023-03-29 03:26:16 +08:00
url_validation_regexes: list[str]
2022-11-03 01:56:19 +08:00
allowed_users: list[UserSchema]
api: ApiSchema
2022-02-04 06:21:27 +08:00
class YtdlpSchema(RealBaseModel):
version_check_enabled: StrictBool
version_check_interval: StrictInt
notify_users_on_new_version: StrictBool
2022-02-04 06:21:27 +08:00
class ConfigSchema(RealBaseModel):
telegram: TelegramSchema
ytdlp: YtdlpSchema