-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.py
91 lines (75 loc) · 2.71 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
import ytthumb
import requests
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, InlineQueryResultPhoto
from youtubesearchpython import VideosSearch
BOT_TOKEN = os.environ.get("BOT_TOKEN")
API_ID = int(os.environ.get("API_ID"))
API_HASH = os.environ.get("API_HASH")
CHANNELS = os.environ.get("CHANNELS", "").split()
Bot = Client(
"YouTube-Search-Bot",
bot_token=BOT_TOKEN,
api_id=API_ID,
api_hash=API_HASH
)
@Bot.on_message(filters.private & filters.all)
async def text(bot, update):
text = "Search youtube videos using below buttons.\n\nMade by @FayasNoushad"
reply_markup = InlineKeyboardMarkup(
[
[InlineKeyboardButton(text="Search here", switch_inline_query_current_chat="")],
[InlineKeyboardButton(text="Search in another chat", switch_inline_query="")]
]
)
await update.reply_text(
text=text,
reply_markup=reply_markup,
disable_web_page_preview=True,
quote=True
)
@Bot.on_inline_query()
async def search(_, update):
# for more results
limit = 100 if CHANNELS else 50
results = VideosSearch(update.query, limit=limit).result()["result"]
answers = []
for result in results:
if CHANNELS:
channel_id = result["channel"]["id"]
if channel_id not in CHANNELS:
continue
title = result["title"]
views_short = result["viewCount"]["short"]
duration = result["duration"]
duration_text = result["accessibility"]["duration"]
views = result["viewCount"]["text"]
publishedtime = result["publishedTime"]
channel_name = result["channel"]["name"]
channel_link = result["channel"]["link"]
description = f"{views_short} | {duration}"
details = f"**Title:** {title}" + "\n" \
f"**Channel:** [{channel_name}]({channel_link})" + "\n" \
f"**Duration:** {duration_text}" + "\n" \
f"**Views:** {views}" + "\n" \
f"**Published Time:** {publishedtime}" + "\n" \
"\n" + "**Made by @FayasNoushad**"
thumbnail = ytthumb.thumbnail(result["id"])
reply_markup = InlineKeyboardMarkup(
[[InlineKeyboardButton(text="Watch Video 📹", url=result["link"])]]
)
try:
answers.append(
InlineQueryResultPhoto(
title=title,
description=description,
caption=details,
photo_url=thumbnail,
reply_markup=reply_markup
)
)
except:
pass
await update.answer(answers)
Bot.run()