forked from TheHamkerCat/Telegram_VC_Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
416 lines (368 loc) · 13 KB
/
functions.py
File metadata and controls
416 lines (368 loc) · 13 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import asyncio
import functools
import os
import aiofiles
import ffmpeg
import youtube_dl
from aiohttp import ClientSession
from PIL import Image, ImageDraw, ImageFont
from pyrogram import Client
from pyrogram.types import Message
from pyrogram.raw.types import InputGroupCall
from pyrogram.raw.functions.channels import GetFullChannel
from pyrogram.raw.functions.phone import EditGroupCallTitle
from Python_ARQ import ARQ
from db import db
is_config = os.path.exists("config.py")
if is_config:
from config import *
else:
from sample_config import *
if HEROKU:
if is_config:
from config import SESSION_STRING
elif not is_config:
from sample_config import SESSION_STRING
app = Client(
SESSION_STRING if HEROKU else "tgvc", api_id=API_ID, api_hash=API_HASH
)
session = ClientSession()
arq = ARQ(ARQ_API, ARQ_API_KEY, session)
themes = ["darkred", "lightred", "green", "purple", "skyblue", "dark", "black"]
def get_theme(chat_id) -> str:
theme = "purple"
if chat_id not in db:
db[chat_id] = {}
if "theme" not in db[chat_id]:
db[chat_id]["theme"] = theme
theme = db[chat_id]["theme"]
return theme
def change_theme(name: str, chat_id):
if chat_id not in db:
db[chat_id] = {}
if "theme" not in db[chat_id]:
db[chat_id]["theme"] = "green"
db[chat_id]["theme"] = name
# Get default service from config
def get_default_service() -> str:
services = ["youtube", "deezer", "saavn"]
try:
config_service = DEFAULT_SERVICE.lower()
if config_service in services:
return config_service
else: # Invalid DEFAULT_SERVICE
return "youtube"
except NameError: # DEFAULT_SERVICE not defined
return "youtube"
async def pause_skip_watcher(message: Message, duration: int, chat_id: int):
try:
chat_id = message.chat.id
db[chat_id]["call"].set_is_mute(False)
if "skipped" not in db[chat_id]:
db[chat_id]["skipped"] = False
if "paused" not in db[chat_id]:
db[chat_id]["paused"] = False
if "stopped" not in db[chat_id]:
db[chat_id]["stopped"] = False
if "replayed" not in db[chat_id]:
db[chat_id]["replayed"] = False
restart_while = False
while True:
for _ in range(duration * 10):
if db[chat_id]["skipped"]:
db[chat_id]["skipped"] = False
return await message.delete()
if db[chat_id]["paused"]:
while db[chat_id]["paused"]:
await asyncio.sleep(0.1)
continue
if db[chat_id]["stopped"]:
restart_while=True
break
if db[chat_id]["replayed"]:
restart_while = True
db[chat_id]["replayed"] = False
break
if "queue_breaker" in db[chat_id] and db[chat_id]["queue_breaker"] != 0:
break
await asyncio.sleep(0.1)
if not restart_while:
break
restart_while = False
await asyncio.sleep(0.1)
db[chat_id]["skipped"] = False
except:
pass
async def change_vc_title(title: str, chat_id):
peer = await app.resolve_peer(chat_id)
chat = await app.send(GetFullChannel(channel=peer))
data = EditGroupCallTitle(call=chat.full_chat.call, title=title)
await app.send(data)
def transcode(filename: str, chat_id: str):
ffmpeg.input(filename).output(
f"input{chat_id}.raw",
format="s16le",
acodec="pcm_s16le",
ac=2,
ar="48k",
loglevel="error",
).overwrite_output().run()
os.remove(filename)
# Download song
async def download_and_transcode_song(url, chat_id):
song = f"{chat_id}.mp3"
async with session.get(url) as resp:
if resp.status == 200:
f = await aiofiles.open(song, mode="wb")
await f.write(await resp.read())
await f.close()
loop = asyncio.get_running_loop()
await loop.run_in_executor(
None, functools.partial(transcode, song, chat_id)
)
# Convert seconds to mm:ss
def convert_seconds(seconds: int):
seconds = seconds % (24 * 3600)
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%02d:%02d" % (minutes, seconds)
# Convert hh:mm:ss to seconds
def time_to_seconds(time):
stringt = str(time)
return sum(
int(x) * 60 ** i for i, x in enumerate(reversed(stringt.split(":")))
)
# Change image size
def changeImageSize(maxWidth: int, maxHeight: int, image):
widthRatio = maxWidth / image.size[0]
heightRatio = maxHeight / image.size[1]
newWidth = int(widthRatio * image.size[0])
newHeight = int(heightRatio * image.size[1])
newImage = image.resize((newWidth, newHeight))
return newImage
# Generate cover for youtube
async def generate_cover(
requested_by, title, views_or_artist, duration, thumbnail, chat_id
):
async with session.get(thumbnail) as resp:
if resp.status == 200:
f = await aiofiles.open(f"background{chat_id}.png", mode="wb")
await f.write(await resp.read())
await f.close()
background = f"./background{chat_id}.png"
final = f"final{chat_id}.png"
temp = f"temp{chat_id}.png"
image1 = Image.open(background)
image2 = Image.open(f"etc/foreground_{get_theme(chat_id)}.png")
image3 = changeImageSize(1280, 720, image1)
image4 = changeImageSize(1280, 720, image2)
image5 = image3.convert("RGBA")
image6 = image4.convert("RGBA")
Image.alpha_composite(image5, image6).save(temp)
img = Image.open(temp)
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("etc/font.otf", 32)
draw.text((190, 550), f"Title: {title}", (255, 255, 255), font=font)
draw.text((190, 590), f"Duration: {duration}", (255, 255, 255), font=font)
draw.text(
(190, 630),
f"Views/Artist: {views_or_artist}",
(255, 255, 255),
font=font,
)
draw.text(
(190, 670), f"Requested By: {requested_by}", (255, 255, 255), font=font
)
img.save(final)
os.remove(temp)
os.remove(background)
try:
await change_vc_title(title, chat_id)
except Exception:
await app.send_message(chat_id, text="[ERROR]: FAILED TO EDIT VC TITLE, MAKE ME ADMIN.")
pass
return final
# Deezer
async def deezer(requested_by, query, message: Message):
m = await message.reply_text(
f"__**Searching for {query} on Deezer.**__", quote=False
)
songs = await arq.deezer(query, 1)
if not songs.ok:
return await m.edit(songs.result)
songs = songs.result
title = songs[0].title
duration = convert_seconds(int(songs[0].duration))
thumbnail = songs[0].thumbnail
artist = songs[0].artist
db[chat_id]["currently"] = {"artist": artist, "song": title, "query": query}
url = songs[0].url
await m.edit("__**Downloading And Transcoding.**__")
cover, _ = await asyncio.gather(
generate_cover(
requested_by, title, artist, duration, thumbnail, message.chat.id
),
download_and_transcode_song(url, message.chat.id),
)
await m.delete()
caption = (
f"🏷 **Name:** [{title[:45]}]({url})\n⏳ **Duration:** {duration}\n"
+ f"🎧 **Requested By:** {message.from_user.mention}\n📡 **Platform:** Deezer"
)
m = await message.reply_photo(
photo=cover,
caption=caption,
)
os.remove(cover)
duration = int(songs[0]["duration"])
await pause_skip_watcher(m, duration, message.chat.id)
await m.delete()
async def get_lyric(query: str, artist, song):
if song and artist:
q = song + artist
elif song:
q = song
else:
q = artist
res = await arq.lyrics(q)
if res.result == "Couldn't find any lyrics for that song!":
res = await arq.lyrics(query)
return res.result
# saavn
async def saavn(requested_by, query, message):
m = await message.reply_text(
f"__**Searching for {query} on JioSaavn.**__", quote=False
)
songs = await arq.saavn(query)
if not songs.ok:
return await m.edit(songs.result)
songs = songs.result
sname = songs[0].song
slink = songs[0].media_url
ssingers = songs[0].singers
db[chat_id]["currently"] = {"artist": ssingers[0] if type(ssingers) == list else ssingers, "song": sname, "query": query}
sthumb = songs[0].image
sduration = songs[0].duration
sduration_converted = convert_seconds(int(sduration))
await m.edit("__**Downloading And Transcoding.**__")
cover, _ = await asyncio.gather(
generate_cover(
requested_by,
sname,
ssingers,
sduration_converted,
sthumb,
message.chat.id,
),
download_and_transcode_song(slink, message.chat.id),
)
await m.delete()
caption = (
f"🏷 **Name:** {sname[:45]}\n⏳ **Duration:** {sduration_converted}\n"
+ f"🎧 **Requested By:** {message.from_user.mention}\n📡 **Platform:** JioSaavn"
)
m = await message.reply_photo(
photo=cover,
caption=caption,
)
os.remove(cover)
duration = int(sduration)
await pause_skip_watcher(m, duration, message.chat.id)
await m.delete()
# Youtube
async def youtube(requested_by, query, message):
ydl_opts = {"format": "bestaudio", "quiet": True}
m = await message.reply_text(
f"__**Searching for {query} on YouTube.**__", quote=False
)
results = await arq.youtube(query)
if not results.ok:
return await m.edit(results.result)
results = results.result
link = f"https://youtube.com{results[0].url_suffix}"
title = results[0].title
db[chat_id]["currently"] = {"artist": None, "song": title, "query": query}
thumbnail = results[0].thumbnails[0]
duration = results[0].duration
views = results[0].views
if time_to_seconds(duration) >= 1800:
return await m.edit("__**Bruh! Only songs within 30 Mins.**__")
await m.edit("__**Processing Thumbnail.**__")
cover = await generate_cover(
requested_by, title, views, duration, thumbnail, message.chat.id
)
await m.edit("__**Downloading Music.**__")
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False)
audio_file = ydl.prepare_filename(info_dict)
ydl.process_info(info_dict)
await m.edit("__**Transcoding.**__")
song = f"audio{message.chat.id}.webm"
os.rename(audio_file, song)
loop = asyncio.get_running_loop()
await loop.run_in_executor(
None, functools.partial(transcode, song, message.chat.id)
)
await m.delete()
caption = (
f"🏷 **Name:** [{title[:45]}]({link})\n⏳ **Duration:** {duration}\n"
+ f"🎧 **Requested By:** {message.from_user.mention}\n📡 **Platform:** YouTube"
)
m = await message.reply_photo(
photo=cover,
caption=caption,
)
os.remove(cover)
duration = int(time_to_seconds(duration))
await pause_skip_watcher(m, duration, message.chat.id)
await m.delete()
# Telegram
async def telegram(_, __, message):
global db
chat_id = message.chat.id
if chat_id not in db:
db[chat_id] = {}
if not message.reply_to_message:
return await message.reply_text(
"__**Reply to an audio.**__", quote=False
)
if not message.reply_to_message.audio:
return await message.reply_text(
"__**Only Audio Files (Not Document) Are Supported.**__",
quote=False,
)
if int(message.reply_to_message.audio.file_size) >= 104857600:
return await message.reply_text(
"__**Bruh! Only songs within 100 MB.**__", quote=False
)
duration = message.reply_to_message.audio.duration
if not duration:
return await message.reply_text(
"__**Only Songs With Duration Are Supported.**__", quote=False
)
m = await message.reply_text("__**Downloading.**__", quote=False)
title = message.reply_to_message.audio.title
performer = message.reply_to_message.audio.performer
db[chat_id]["currently"] = {"artist": performer, "song": title, "query": None}
song = await message.reply_to_message.download()
await m.edit("__**Transcoding.**__")
try:
if message.reply_to_message.audio.title:
title = message.reply_to_message.audio.title
else:
title = message.reply_to_message.audio.performer
await change_vc_title(title, chat_id)
except Exception:
await app.send_message(chat_id, text="[ERROR]: FAILED TO EDIT VC TITLE, MAKE ME ADMIN.")
pass
loop = asyncio.get_running_loop()
await loop.run_in_executor(
None, functools.partial(transcode, song, chat_id)
)
await m.edit(f"**Playing** __**{message.reply_to_message.link}.**__")
await pause_skip_watcher(m, duration, message.chat.id)
try:
os.remove(song)
except:
pass