From f26bc7429af5300a64111d84e38f887e3ca65045 Mon Sep 17 00:00:00 2001 From: hhh Date: Wed, 3 Jul 2024 20:49:10 +0300 Subject: [PATCH] Update deps versions, minor spotify fix, removed db sync, suppress deprecationwarning --- .gitignore | 2 +- bot/handlers/initialize/initializer.py | 3 - bot/handlers/on_chosen/deezer.py | 2 - bot/handlers/on_chosen/recode_cached.py | 1 - bot/handlers/on_chosen/soundcloud.py | 2 - bot/handlers/on_chosen/spotify.py | 2 - bot/handlers/on_chosen/youtube.py | 2 - bot/modules/database/__init__.py | 3 +- bot/modules/database/db.py | 16 ---- bot/modules/database/db_model.py | 44 +--------- bot/modules/database/meta.py | 109 ------------------------ bot/modules/database/pull_db.py | 60 ------------- bot/modules/deezer/util.py | 14 +-- bot/modules/spotify/spotify.py | 4 +- config.toml.example | 3 +- pyproject.toml | 6 +- 16 files changed, 19 insertions(+), 254 deletions(-) delete mode 100644 bot/modules/database/meta.py delete mode 100644 bot/modules/database/pull_db.py diff --git a/.gitignore b/.gitignore index 963c028..aa5520a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ config.toml */__pycache__/ .cache -db +db.db diff --git a/bot/handlers/initialize/initializer.py b/bot/handlers/initialize/initializer.py index 7985fd2..629ce9e 100644 --- a/bot/handlers/initialize/initializer.py +++ b/bot/handlers/initialize/initializer.py @@ -8,6 +8,3 @@ router = Router() @router.startup() async def startup(bot: Bot): print(f'[green]Started as[/] @{(await bot.me()).username}') - - from bot.modules.database import pull - await pull() diff --git a/bot/handlers/on_chosen/deezer.py b/bot/handlers/on_chosen/deezer.py index 07bc6c4..a31821a 100644 --- a/bot/handlers/on_chosen/deezer.py +++ b/bot/handlers/on_chosen/deezer.py @@ -36,5 +36,3 @@ async def on_new_chosen(chosen_result: ChosenInlineResult, bot: Bot): media=InputMediaAudio(media=audio.audio.file_id), reply_markup=None ) - - await db.occasionally_write() diff --git a/bot/handlers/on_chosen/recode_cached.py b/bot/handlers/on_chosen/recode_cached.py index ff3f23f..d693a2e 100644 --- a/bot/handlers/on_chosen/recode_cached.py +++ b/bot/handlers/on_chosen/recode_cached.py @@ -98,4 +98,3 @@ async def on_cached_chosen(chosen_result: ChosenInlineResult, bot: Bot, else: db.youtube[song_id] = audio.audio.file_id db.recoded[song_id] = True - await db.occasionally_write() diff --git a/bot/handlers/on_chosen/soundcloud.py b/bot/handlers/on_chosen/soundcloud.py index 973acc6..fc66b68 100644 --- a/bot/handlers/on_chosen/soundcloud.py +++ b/bot/handlers/on_chosen/soundcloud.py @@ -35,5 +35,3 @@ async def on_new_chosen(chosen_result: ChosenInlineResult, bot: Bot): media=InputMediaAudio(media=audio.audio.file_id), reply_markup=None ) - - await db.occasionally_write() diff --git a/bot/handlers/on_chosen/spotify.py b/bot/handlers/on_chosen/spotify.py index 607d2b8..1155579 100644 --- a/bot/handlers/on_chosen/spotify.py +++ b/bot/handlers/on_chosen/spotify.py @@ -156,5 +156,3 @@ async def on_new_chosen(chosen_result: ChosenInlineResult, bot: Bot, db.recoded[yt_song.id] = audio.message_id if settings['exact_spotify_search'].value == 'yes': db.recoded[song.id] = audio.message_id - - await db.occasionally_write() diff --git a/bot/handlers/on_chosen/youtube.py b/bot/handlers/on_chosen/youtube.py index 0d22a28..444bdaa 100644 --- a/bot/handlers/on_chosen/youtube.py +++ b/bot/handlers/on_chosen/youtube.py @@ -79,5 +79,3 @@ async def on_new_chosen(chosen_result: ChosenInlineResult, bot: Bot, ) else: db.recoded[song.id] = audio.message_id - - await db.occasionally_write() diff --git a/bot/modules/database/__init__.py b/bot/modules/database/__init__.py index 2b5227c..f54bf8c 100644 --- a/bot/modules/database/__init__.py +++ b/bot/modules/database/__init__.py @@ -1,7 +1,6 @@ from .db import Db -from .pull_db import pull db = Db() -__all__ = ['db', 'pull'] +__all__ = ['db'] diff --git a/bot/modules/database/db.py b/bot/modules/database/db.py index 5d67f09..0484ddc 100644 --- a/bot/modules/database/db.py +++ b/bot/modules/database/db.py @@ -1,13 +1,4 @@ from .db_model import DBDict -import os.path -from bot.utils.config import config - -from random import randint - -DB = os.path.join(config.local.db_path, 'db') - -if not os.path.isfile(DB): - open('sync', 'w') class Db(object): @@ -22,10 +13,3 @@ class Db(object): self.youtube = DBDict('youtube') self.soundcloud = DBDict('soundcloud') self.recoded = DBDict('recoded') - - async def write(self): - await self.config.write() - - async def occasionally_write(self, chance: int = 5): - if randint(1, chance) == 1: - await self.write() diff --git a/bot/modules/database/db_model.py b/bot/modules/database/db_model.py index fe76ed3..87133dc 100644 --- a/bot/modules/database/db_model.py +++ b/bot/modules/database/db_model.py @@ -1,49 +1,7 @@ from sqlitedict import SqliteDict from bot.utils.config import config -from aiogram.types import FSInputFile, InputMediaDocument -from aiogram import exceptions -from pydantic import ValidationError -import time -from .meta import DBMeta -import os.path - -DB = os.path.join(config.local.db_path, 'db') -DB_CHAT = config.telegram.db_chat class DBDict(SqliteDict): def __init__(self, tablename: str): - super().__init__(DB, tablename=tablename, autocommit=True) - - async def write(self): - from bot.common import bot - - try: - DBMeta().update_time = time.time_ns() - - await bot.edit_message_media( - media=InputMediaDocument(media=FSInputFile(DB)), - chat_id=DB_CHAT, - message_id=DBMeta().message_id - ) - await bot.edit_message_caption( - caption=str(DBMeta()), - chat_id=DB_CHAT, - message_id=DBMeta().message_id - ) - - except (ValidationError, exceptions.TelegramBadRequest): - DBMeta().update_time = time.time_ns() - - self['db_message_id'] = ( - await bot.send_document( - chat_id=DB_CHAT, document=FSInputFile(DB), - disable_notification=True - ) - ).message_id - - DBMeta().message_id = self['db_message_id'] - await bot.edit_message_caption( - caption=str(DBMeta()), - chat_id=DB_CHAT, message_id=self.get('db_message_id') - ) + super().__init__(config.local.db_path, tablename=tablename, autocommit=True) diff --git a/bot/modules/database/meta.py b/bot/modules/database/meta.py deleted file mode 100644 index bec5001..0000000 --- a/bot/modules/database/meta.py +++ /dev/null @@ -1,109 +0,0 @@ -import os.path -from bot.utils.config import config -from aiogram.exceptions import TelegramBadRequest -import asyncio - -from typing import Coroutine - -loop = asyncio.get_event_loop() - - -DBMETA = os.path.join(config.local.db_path, 'dbmeta') -APP_ID = config.local.app_id -DB_CHAT = config.telegram.db_chat - - -def meta_property(prop_name): - def getter(self): - return self[prop_name] - - def setter(self, value): - self[prop_name] = value - - return property(getter, setter) - - -class DBMeta: - app_id = meta_property('app_id') - message_id = meta_property('message_id') - update_time = meta_property('update_time') - - def __init__(self): - if not os.path.isfile(DBMETA): - open(DBMETA, 'w').write(f'{APP_ID}|None|0') - - def __getitem__(self, item): - try: - return open(DBMETA).read().split('|')[{ - "app_id": 0, - "message_id": 1, - "update_time": 2 - }.get(item)] - except TypeError: - return None - - def __setitem__(self, key, value): - meta = open(DBMETA).read().split('|') - meta[{ - "app_id": 0, - "message_id": 1, - "update_time": 2 - }[key]] = value - open(DBMETA, 'w').write('|'.join(str(x) for x in meta)) - - def __str__(self): - return open(DBMETA).read() - - -def cloud_meta_property(self, prop_name): - async def getter(): - return await self.get(prop_name) - - return getter() - - -class CloudMeta: - def __init__(self): - def prop_generator(name) -> Coroutine: - return cloud_meta_property(self, name) - - self.app_id = prop_generator('app_id') - self.message_id = prop_generator('message_id') - self.update_time = prop_generator('update_time') - - @staticmethod - async def get(item): - from bot.common import bot - - try: - if not DBMeta().update_time or not bot.cloudmeta_message_text: - raise AttributeError - - except AttributeError: - try: - message = await bot.forward_message( - DB_CHAT, DB_CHAT, - DBMeta().message_id - ) - - bot.cloudmeta_message_text = message.caption - - await message.delete() - - except TelegramBadRequest: - print('Cannot get CloudMeta - writing DBDict') - from .db_model import DBDict - await DBDict('config').write() - message = await bot.forward_message( - DB_CHAT, DB_CHAT, - DBMeta().message_id - ) - bot.cloudmeta_message_text = message.caption - await message.delete() - - cloudmeta = bot.cloudmeta_message_text.split('|') - return cloudmeta[{ - "app_id": 0, - "message_id": 1, - "update_time": 2 - }.get(item)] diff --git a/bot/modules/database/pull_db.py b/bot/modules/database/pull_db.py deleted file mode 100644 index 1694a65..0000000 --- a/bot/modules/database/pull_db.py +++ /dev/null @@ -1,60 +0,0 @@ -import os -from .meta import DBMeta, CloudMeta -from bot.utils.config import config -from sqlitedict import SqliteDict - -DB = os.path.join(config.local.db_path, 'db') -DB_CHAT = config.telegram.db_chat - - -async def pull(): - from bot.common import bot - - if DBMeta().message_id == 'None': - from . import db - print('No dbmeta file') - if msg_id := db.config.get('db_message_id'): - print('Found message id in in-db config') - DBMeta().message_id = msg_id - await db.write() - - if not os.path.isfile('sync'): - try: - if not bot.cloudmeta_message_text: - print('Cloudmeta initialized incorrectly') - raise AttributeError - else: - return - except AttributeError: - if int(DBMeta().update_time) >= int(await CloudMeta().update_time): - print('DB is up-to-date') - return - else: - print('Database file is new. Trying to download cloud data') - os.remove('sync') - - print('DB is not up-to-date') - - message = await bot.forward_message(DB_CHAT, DB_CHAT, DBMeta().message_id) - - await message.delete() - - await bot.download( - message.document, - destination=DB + 'b' - ) - - from . import db - for table in db.__dict__.keys(): - new_table = SqliteDict(DB + 'b', tablename=table) - for key in new_table.keys(): - if key is not None: - try: - getattr(db, table)[key] = new_table[key] - except Exception as e: - assert e - new_table.close() - - await db.write() - - print('Synced') diff --git a/bot/modules/deezer/util.py b/bot/modules/deezer/util.py index d0c50f7..14417dc 100644 --- a/bot/modules/deezer/util.py +++ b/bot/modules/deezer/util.py @@ -1,5 +1,5 @@ # https://pypi.org/project/music-helper/ - +import warnings import re import hashlib @@ -64,11 +64,13 @@ class ChunkDecrypter: @classmethod def from_track_id(cls, track_id: str): - cipher = Cipher( - algorithms.Blowfish(get_blowfish_key(track_id)), - modes.CBC(bytes([i for i in range(8)])), - default_backend() - ) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore") + cipher = Cipher( + algorithms.Blowfish(get_blowfish_key(track_id)), + modes.CBC(bytes([i for i in range(8)])), + default_backend() + ) return cls( cipher=cipher diff --git a/bot/modules/spotify/spotify.py b/bot/modules/spotify/spotify.py index dc66a9e..bde36cf 100644 --- a/bot/modules/spotify/spotify.py +++ b/bot/modules/spotify/spotify.py @@ -10,7 +10,9 @@ class Spotify(object): client_credentials_manager=SpotifyClientCredentials( client_id=client_id, client_secret=client_secret - ) + ), + backoff_factor=0.1, + retries=10 ) self.songs = Songs(self.spotify) diff --git a/config.toml.example b/config.toml.example index f6c85be..446ae79 100644 --- a/config.toml.example +++ b/config.toml.example @@ -1,11 +1,10 @@ [telegram] bot_token = '' -db_chat = 0 files_chat = 0 admin_id = 0 [local] -db_path = 'db/' +db_path = 'db.db' app_id = 'ANY-MUSIC-BOT' [tokens.spotify] diff --git a/pyproject.toml b/pyproject.toml index c28798c..8ecd9b4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,6 +6,8 @@ authors = ["BarsTiger"] readme = "README.md" [tool.poetry.dependencies] +wheel = "^0.43.0" +setuptools = "^70.2.0" python = "^3.11" aiogram = "^3.1.1" rich = "^13.6.0" @@ -19,8 +21,8 @@ pydub = "^0.25.1" aiohttp = "^3.8.6" nest-asyncio = "^1.5.8" icecream = "^2.1.3" -m3u8 = "^3.6.0" -cryptography = "^41.0.7" +m3u8 = "^5.1.0" +cryptography = "^42.0.8" [build-system]