forked from TR-TECH-GUIDE/YouTube-Search-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
83 lines (74 loc) · 2.58 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
# Made with python3
# (C) @TR-TECH-GUIDE
# Copyright permission under MIT License
# All rights reserved by TR-TECH-GUIDE
# License -> https://github.com/TR-TECH-GUIDE/YouTube-Search-Bot/blob/main/LICENSE
import os
import ytthumb
import requests
from requests.utils import requote_uri
from pyrogram import Client, filters
from pyrogram.types import *
Bot = Client(
"YouTube-Search-Bot",
bot_token = os.environ["BOT_TOKEN"],
api_id = int(os.environ["API_ID"]),
api_hash = os.environ["API_HASH"]
)
@Bot.on_message(filters.command(['search']))
async def text(bot, update):
text = "Search youtube videos using below buttons.\n\nMade by @SLBotsOfficial"
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(bot, update):
results = requests.get(
"https://youtube.api.fayas.me/videos/?query=" + requote_uri(update.query)
).json()["result"][:50]
answers = []
for result in results:
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 @TR-TECH-GUIDE**"
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()