# █▀▀ ▄▀█ █▀▄▀█ █▀█ █▀▄ █▀
# █▀░ █▀█ █░▀░█ █▄█ █▄▀ ▄█
# https://t.me/famods
# 🔒 Licensed under the GNU AGPLv3
# 🌐 https://www.gnu.org/licenses/agpl-3.0.html
# ---------------------------------------------------------------------------------
# Name: YTsearch
# Description: Поиск в Youtube
# meta developer: @FAmods
# meta banner: https://github.com/FajoX1/FAmods/blob/main/assets/banners/ytsearch.png?raw=true
# requires: youtube-search-python
# ---------------------------------------------------------------------------------
import time
import asyncio
import logging
from youtubesearchpython import VideosSearch, ChannelsSearch
from .. import loader, utils
logger = logging.getLogger(__name__)
@loader.tds
class YTsearch(loader.Module):
"""Поиск в Youtube"""
strings = {
"name": "YTsearch",
"no_q": "❌ Должно быть {}{} [запрос]",
"no_result": "😕 Ничего не нашёл по этому запросу",
"searching": "🔄 Поиск в youtube.com...",
"searched": """
🔎 Результаты поиска {}
🔎 Запрос: {}{}
{} результатов за {} сек
""",
}
def __init__(self):
self.config = loader.ModuleConfig(
loader.ConfigValue(
"limit_channels",
10,
lambda: "Максимальное количество видео в результате.",
),
loader.ConfigValue(
"limit_video",
10,
lambda: "Максимальное количество видео в результате.",
),
loader.ConfigValue(
"lang",
"ru",
lambda: "Язык поиска.",
),
)
async def client_ready(self, client, db):
self.db = db
self._client = client
@loader.command()
async def ytvsearch(self, message):
"""Поиск видео в Youtube"""
q = utils.get_args_raw(message)
if not q:
return await utils.answer(message, self.strings["no_q"].format(self.get_prefix(), "ytvsearch"))
await utils.answer(message, self.strings['searching'])
count_s = 0
start_time = time.time()
searched_result = ""
for v in VideosSearch(query=q, limit=self.config['limit_video'], language=self.config['lang']).result()['result']:
searched_result += f"""
{v['title']} от {v['channel']['name']}
{v['viewCount']['text'].replace('views', 'просмотров')} ({v['duration']}) {v['publishedTime']}"""
count_s += 1
end_time = time.time()
execution_time = end_time - start_time
if count_s == 0:
return await utils.answer(message, self.strings['no_result'])
return await utils.answer(message, self.strings['searched'].format("видео", q, searched_result, count_s, execution_time))
@loader.command()
async def ytcsearch(self, message):
"""Поиск каналов в Youtube"""
q = utils.get_args_raw(message)
if not q:
return await utils.answer(message, self.strings["no_q"].format(self.get_prefix(), "ytcsearch"))
await utils.answer(message, self.strings['searching'])
count_s = 0
start_time = time.time()
searched_result = ""
for c in ChannelsSearch(query=q, limit=self.config['limit_channels'], language=self.config['lang']).result()['result']:
searched_result += "\n" if count_s == 0 else ""
searched_result += f"""
{c['title']} ({c['subscribers']})"""
count_s += 1
end_time = time.time()
execution_time = end_time - start_time
if count_s == 0:
return await utils.answer(message, self.strings['no_result'])
return await utils.answer(message, self.strings['searched'].format("каналов", q, searched_result, count_s, execution_time))