forked from omindadelshan/fastsongdownloaderbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainbot.py
288 lines (268 loc) · 9.96 KB
/
mainbot.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Copyright (c) 2021 slbotzone <https://t.me/slbotzone>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
import os
import aiohttp
import asyncio
import json
import sys
import time
from youtubesearchpython import SearchVideos
from pyrogram import filters, Client
from sample_config import Config
from youtube_dl import YoutubeDL
from youtube_dl.utils import (
ContentTooShortError,
DownloadError,
ExtractorError,
GeoRestrictedError,
MaxDownloadsReached,
PostProcessingError,
UnavailableVideoError,
XAttrMetadataError,
)
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery, InlineQuery, InputTextMessageContent
Jebot = Client(
"Song Downloader",
api_id=Config.APP_ID,
api_hash=Config.API_HASH,
bot_token=Config.TG_BOT_TOKEN,
)
#For private messages
#Ignore commands
#No bots also allowed
@Jebot.on_message(filters.private & ~filters.bot & ~filters.command("help") & ~filters.command("start") & ~filters.command("s"))
async def song(client, message):
#ImJanindu #JEBotZ
cap = "@fastsongdownloderslbzbot"
url = message.text
rkp = await message.reply("🌟 Processing...")
search = SearchVideos(url, offset=1, mode="json", max_results=1)
test = search.result()
p = json.loads(test)
q = p.get("search_result")
try:
url = q[0]["link"]
except BaseException:
return await rkp.edit("🌟Failed to find that song.")
type = "audio"
if type == "audio":
opts = {
"format": "bestaudio",
"addmetadata": True,
"key": "FFmpegMetadata",
"writethumbnail": True,
"prefer_ffmpeg": True,
"geo_bypass": True,
"nocheckcertificate": True,
"postprocessors": [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "320",
}
],
"outtmpl": "%(id)s.mp3",
"quiet": True,
"logtostderr": False,
}
song = True
try:
await rkp.edit("📥 Downloading...")
with YoutubeDL(opts) as rip:
rip_data = rip.extract_info(url)
except DownloadError as DE:
await rkp.edit(f"`{str(DE)}`")
return
except ContentTooShortError:
await rkp.edit("`📥 The download content was too short.`")
return
except GeoRestrictedError:
await rkp.edit(
"`Video is not available from your geographic location due to geographic restrictions imposed by a website.`"
)
return
except MaxDownloadsReached:
await rkp.edit("`Max-downloads limit has been reached.`")
return
except PostProcessingError:
await rkp.edit("`There was an error during post processing.`")
return
except UnavailableVideoError:
await rkp.edit("`Media is not available in the requested format.`")
return
except XAttrMetadataError as XAME:
await rkp.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`")
return
except ExtractorError:
await rkp.edit("`There was an error during info extraction.`")
return
except Exception as e:
await rkp.edit(f"{str(type(e)): {str(e)}}")
return
time.time()
if song:
await rkp.edit(" 📤 Uploading...") #ImJanindu
lol = "./thumb.jpg"
lel = await message.reply_audio(
f"{rip_data['id']}.mp3",
duration=int(rip_data["duration"]),
title=str(rip_data["title"]),
performer=str(rip_data["📤uploader"]),
thumb=lol,
caption=cap) #JEBotZ
await rkp.delete()
os.system("rm -rf *.mp3")
os.system("rm -rf *.webp")
@Jebot.on_message(filters.command("song") & ~filters.edited & filters.group)
async def song(client, message):
cap = "@fastsongdownloderslbzbot"
url = message.text.split(None, 1)[1]
rkp = await message.reply("Processing...")
if not url:
await rkp.edit("**🤦What's the song you want?**\nUsage`/song <song name>`")
search = SearchVideos(url, offset=1, mode="json", max_results=1)
test = search.result()
p = json.loads(test)
q = p.get("search_result")
try:
url = q[0]["link"]
except BaseException:
return await rkp.edit("Failed to find that song.")
type = "audio"
if type == "audio":
opts = {
"format": "bestaudio",
"addmetadata": True,
"key": "FFmpegMetadata",
"writethumbnail": True,
"prefer_ffmpeg": True,
"geo_bypass": True,
"nocheckcertificate": True,
"postprocessors": [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "320",
}
],
"outtmpl": "%(id)s.mp3",
"quiet": True,
"logtostderr": False,
}
song = True
try:
await rkp.edit("Downloading...")
with YoutubeDL(opts) as rip:
rip_data = rip.extract_info(url)
except DownloadError as DE:
await rkp.edit(f"`{str(DE)}`")
return
except ContentTooShortError:
await rkp.edit("`The download content was too short.`")
return
except GeoRestrictedError:
await rkp.edit(
"`Video is not available from your geographic location due to geographic restrictions imposed by a website.`"
)
return
except MaxDownloadsReached:
await rkp.edit("`Max-downloads limit has been reached.`")
return
except PostProcessingError:
await rkp.edit("`There was an error during post processing.`")
return
except UnavailableVideoError:
await rkp.edit("`Media is not available in the requested format.`")
return
except XAttrMetadataError as XAME:
await rkp.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`")
return
except ExtractorError:
await rkp.edit("`There was an error during info extraction.`")
return
except Exception as e:
await rkp.edit(f"{str(type(e)): {str(e)}}")
return
time.time()
if song:
await rkp.edit("Uploading...") #ImJanindu
lol = "./thumb.jpg"
lel = await message.reply_audio(
f"{rip_data['id']}.mp3",
duration=int(rip_data["duration"]),
title=str(rip_data["title"]),
performer=str(rip_data["uploader"]),
thumb=lol,
caption=cap) #JEBotZ
await rkp.delete()
os.system("rm -rf *.mp3")
os.system("rm -rf *.webp")
@Jebot.on_message(filters.command("start"))
async def start(client, message):
if message.chat.type == 'private':
await Jebot.send_message(
chat_id=message.chat.id,
sticker("CAACAgIAAxkBAAEJp3Bg1CErSDjmDGLFaJZt7DW1x2H97QACqAEAAjDUnREPZpQTbub_3h4E")
text="""<b>👋 Hey There, I'm a Song Downloader Bot. A bot by 👨💻 @slbotzone.
Hit help button to find out more about how to use me</b>""",
reply_markup=InlineKeyboardMarkup(
[[
InlineKeyboardButton(
" 💫 Help 💫", callback_data="help"),
InlineKeyboardButton(
"🔥 guid for create this bot🔥 ", url="https://www.youtube.com/channel/UCvYfJcTr8RY72dIapzMqFQA?sub_confirmation=1")
]]
),
disable_web_page_preview=True,
parse_mode="html",
reply_to_message_id=message.message_id
)
else:
await Jebot.send_message(
chat_id=message.chat.id,
text="""<b> 👋Song Downloader Is Online.\n\n</b>""",
reply_markup=InlineKeyboardMarkup(
[[
InlineKeyboardButton(
"Help", callback_data="help")
]]
),
disable_web_page_preview=True,
parse_mode="html",
reply_to_message_id=message.message_id
)
@Jebot.on_message(filters.command("help"))
async def help(client, message):
if message.chat.type == 'private':
await Jebot.send_message(
chat_id=message.chat.id,
text="""<b>Send a song name to download song
@slbotzone</b>""",
reply_to_message_id=message.message_id
)
else:
await Jebot.send_message(
chat_id=message.chat.id,
text="<b>Song Downloader Help.\n\nSyntax: `/song guleba`</b>",
reply_to_message_id=message.message_id
)
@Jebot.on_callback_query()
async def button(Jebot, update):
cb_data = update.data
if "help" in cb_data:
await update.message.delete()
await help(Jebot, update.message)
print(
"""
Bot Started!
Join @slbotzone
"""
)
Jebot.run()