Did some refactor, add YouTube as search variant
This commit is contained in:
1
bot/results/__init__.py
Normal file
1
bot/results/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
pass
|
||||
1
bot/results/common/__init__.py
Normal file
1
bot/results/common/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
pass
|
||||
39
bot/results/common/search.py
Normal file
39
bot/results/common/search.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from aiogram.types import (
|
||||
InlineQueryResultDocument, InlineQueryResultCachedAudio,
|
||||
InlineKeyboardMarkup, InlineKeyboardButton
|
||||
)
|
||||
|
||||
from bot.modules.database.db import DBDict
|
||||
|
||||
from bot.modules.common.song import BaseSongItem
|
||||
from typing import TypeVar
|
||||
|
||||
|
||||
BaseSongT = TypeVar('BaseSongT', bound=BaseSongItem)
|
||||
|
||||
|
||||
async def get_common_search_result(
|
||||
audio: BaseSongT,
|
||||
db_table: DBDict,
|
||||
service_id: str
|
||||
) -> InlineQueryResultDocument | InlineQueryResultCachedAudio:
|
||||
return (
|
||||
InlineQueryResultDocument(
|
||||
id=f'{service_id}::' + audio.id,
|
||||
title=audio.name,
|
||||
description=audio.all_artists,
|
||||
thumb_url=audio.thumbnail,
|
||||
document_url=audio.preview_url or audio.thumbnail,
|
||||
mime_type='application/zip',
|
||||
reply_markup=InlineKeyboardMarkup(
|
||||
inline_keyboard=[
|
||||
[InlineKeyboardButton(text='Downloading...', callback_data='.')]
|
||||
]
|
||||
),
|
||||
caption=audio.full_name,
|
||||
) if audio.id not in list(db_table.keys()) else
|
||||
InlineQueryResultCachedAudio(
|
||||
id=f'{service_id}c::' + audio.id,
|
||||
audio_file_id=db_table[audio.id],
|
||||
)
|
||||
)
|
||||
4
bot/results/deezer/__init__.py
Normal file
4
bot/results/deezer/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from .search import get_deezer_search_results
|
||||
|
||||
|
||||
__all__ = ['get_deezer_search_results']
|
||||
21
bot/results/deezer/search.py
Normal file
21
bot/results/deezer/search.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from aiogram.types import (
|
||||
InlineQueryResultDocument, InlineQueryResultCachedAudio
|
||||
)
|
||||
|
||||
from bot.modules.deezer import deezer
|
||||
from bot.modules.database import db
|
||||
|
||||
from ..common.search import get_common_search_result
|
||||
|
||||
|
||||
async def get_deezer_search_results(query: str) -> list[
|
||||
InlineQueryResultDocument | InlineQueryResultCachedAudio
|
||||
]:
|
||||
return [
|
||||
await get_common_search_result(
|
||||
audio=audio,
|
||||
db_table=db.deezer,
|
||||
service_id='deez'
|
||||
)
|
||||
for audio in await deezer.songs.search(query, limit=50)
|
||||
]
|
||||
6
bot/results/spotify/__init__.py
Normal file
6
bot/results/spotify/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from .search import get_spotify_search_results
|
||||
|
||||
|
||||
__all__ = [
|
||||
'get_spotify_search_results'
|
||||
]
|
||||
21
bot/results/spotify/search.py
Normal file
21
bot/results/spotify/search.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from aiogram.types import (
|
||||
InlineQueryResultDocument, InlineQueryResultCachedAudio
|
||||
)
|
||||
|
||||
from bot.modules.spotify import spotify
|
||||
from bot.modules.database import db
|
||||
|
||||
from ..common.search import get_common_search_result
|
||||
|
||||
|
||||
async def get_spotify_search_results(query: str) -> list[
|
||||
InlineQueryResultDocument | InlineQueryResultCachedAudio
|
||||
]:
|
||||
return [
|
||||
await get_common_search_result(
|
||||
audio=audio,
|
||||
db_table=db.spotify,
|
||||
service_id='spot'
|
||||
)
|
||||
for audio in spotify.songs.search(query, limit=50)
|
||||
]
|
||||
6
bot/results/youtube/__init__.py
Normal file
6
bot/results/youtube/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from .search import get_youtube_search_results
|
||||
|
||||
|
||||
__all__ = [
|
||||
'get_youtube_search_results'
|
||||
]
|
||||
21
bot/results/youtube/search.py
Normal file
21
bot/results/youtube/search.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from aiogram.types import (
|
||||
InlineQueryResultDocument, InlineQueryResultCachedAudio
|
||||
)
|
||||
|
||||
from bot.modules.youtube import youtube
|
||||
from bot.modules.database import db
|
||||
|
||||
from ..common.search import get_common_search_result
|
||||
|
||||
|
||||
async def get_youtube_search_results(query: str) -> list[
|
||||
InlineQueryResultDocument | InlineQueryResultCachedAudio
|
||||
]:
|
||||
return [
|
||||
await get_common_search_result(
|
||||
audio=audio,
|
||||
db_table=db.youtube,
|
||||
service_id='yt'
|
||||
)
|
||||
for audio in youtube.songs.search(query, limit=40)
|
||||
]
|
||||
Reference in New Issue
Block a user