-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
215 lines (202 loc) · 7.46 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
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
import traceback, ffmpeg, re
import asyncio, datetime
from pytgcalls import PyTgCalls, StreamType, idle
from pytgcalls.types import Update
from pytgcalls.types.input_stream import AudioPiped, AudioVideoPiped
from pytgcalls.types.input_stream.quality import HighQualityAudio
from pytgcalls.types.input_stream.quality import HighQualityVideo
from pytgcalls.types.input_stream import InputStream
from pytgcalls.exceptions import AlreadyJoinedError
from client import *
from multiprocessing import Process
from funcs import *
vc = PyTgCalls(user)
#vc.start()
queue = []
async def is_admin(chat_id, user):
return user in [x.user.id async for x in bot.iter_chat_members(chat_id, filter="administrators") if x.can_manage_voice_chats]
async def yt_stream(query, only_audio=True):
if only_audio:
if re.search("youtu", query):
proc = await asyncio.create_subprocess_exec(
'yt-dlp',
'-g',
'-f',
'bestaudio/best',
f'{query}',
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
return stdout.decode().split('\n')[0]
else:
proc = await asyncio.create_subprocess_exec(
'yt-dlp',
'-g',
'-f',
# CHANGE THIS BASED ON WHAT YOU WANT
'best[height<=?720][width<=?1280]',
f'ytsearch1:{query}',
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
return stdout.decode().split('\n')[0]
if re.search("youtu", query):
proc = await asyncio.create_subprocess_exec(
'yt-dlp',
'-g',
'-f',
'best[height<=?720][width<=?1280]',
f'{query}',
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
return stdout.decode().split('\n')[0]
proc = await asyncio.create_subprocess_exec(
'yt-dlp',
'-g',
'-f',
# CHANGE THIS BASED ON WHAT YOU WANT
'best[height<=?720][width<=?1280]',
f'ytsearch1:{query}',
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
return stdout.decode().split('\n')[0]
def check_value(data, val):
if len(data) > 0:
if val in data and data[val] == 1:
is_playing = True
else:
is_playing = False
else:
is_playing = False
return is_playing
def transcode(filename: str, chat_id: int):
ffmpeg.input(filename).output(
f"input{chat_id}.raw",
format="s16le",
acodec="pcm_s16le",
ac=1,
ar="48k",
loglevel="error",
).overwrite_output().run()
os.remove(filename)
def download(idd, chat_id):
info_dict = ydl.extract_info(idd, download=False)
audio_file = ydl.prepare_filename(info_dict)
ydl.process_info(info_dict)
os.rename(audio_file, f"input{chat_id}.webm")
return info_dict
@bot.on_message(filters.command("vcs"))
async def joinvc(_, m):
if str(m.chat.id) not in AuthChats:
return
if await is_admin(m.chat.id, m.from_user.id) == False:
return
try:
await m.reply_text(f"{vc._call_holder._calls}\n\n{QUEUE}", quote=True)
except Exception as e:
print(traceback.print_exc())
await m.reply(e)
@bot.on_message(filters.command("stop"))
async def joinvc(_, m):
if str(m.chat.id) not in AuthChats:
return
if await is_admin(m.chat.id, m.from_user.id) == False:
return
await vc.leave_group_call(m.chat.id)
@bot.on_message(filters.command("skip"))
async def skipvc(_, m):
if str(m.chat.id) not in AuthChats:
return
if await is_admin(m.chat.id, m.from_user.id) == False:
return
mssg = await m.reply_text("Skipped current song!")
if not get_from_queue(m.chat.id):
return await vc.leave_group_call(m.chat.id)
song, from_user = get_from_queue(m.chat.id)
info_dict = ytdetails = await get_yt_dict(song)
title = info_dict["title"]
remote = await yt_stream(song)
xx = datetime.timedelta(seconds=info_dict["duration"])
if str(xx).startswith("0"):
duration = (str(xx)[2:])
else:
duration = str(xx)
await vc.change_stream(m.chat.id, AudioPiped(remote, HighQualityAudio()))
QUEUE[m.chat.id].pop(0)
await bot.send_photo(m.chat.id, f"https://i.ytimg.com/vi/{ytdetails['id']}/maxresdefault.jpg", caption=f"Playing `{title}`\nDuration: `{duration}`")
@bot.on_message(filters.command("yt"))
async def ytvc(_, m):
if str(m.chat.id) not in AuthChats:
return
if await is_admin(m.chat.id, m.from_user.id) == False:
return
text = m.text.split(" ", 1)
remote = await yt_stream(text[1], only_audio=False)
try:
await vc.join_group_call(m.chat.id, AudioVideoPiped(remote, HighQualityAudio(), HighQualityVideo()))
except AlreadyJoinedError:
_check = check_value(vc._call_holder._calls, m.chat.id)
if _check == False:
await vc.change_stream(m.chat.id, AudioVideoPiped(remote, HighQualityAudio(), HighQualityVideo()))
else:
return
await m.reply_text("accha")
@bot.on_message(filters.command("play"))
async def playvc(_, m):
text = m.text.split(" ", 1)
if str(m.chat.id) not in AuthChats:
return
if await is_admin(m.chat.id, m.from_user.id) == False:
return
_check = check_value(vc._call_holder._calls, m.chat.id)
print(_check)
if _check == False:
ytdetails = await get_yt_dict(text[1])
chat_id = m.chat.id
remote = await yt_stream(text[1])
title = ytdetails["title"]
# print(info_dict["thumbnails"])
# thumb = info_dict["thumbnails"][1]["url"]
xx = datetime.timedelta(seconds=ytdetails["duration"])
if str(xx).startswith("0"):
duration = (str(xx)[2:])
else:
duration = str(xx)
try:
await vc.join_group_call(
m.chat.id, AudioPiped(remote, HighQualityAudio())
)
except AlreadyJoinedError:
await vc.change_stream(m.chat.id, AudioPiped(remote, HighQualityAudio()))
await bot.send_photo(m.chat.id, f"https://i.ytimg.com/vi/{ytdetails['id']}/maxresdefault.jpg", caption=f"Playing: `{title}`\nDuration: `{duration}`")
elif _check == True:
print(m.chat.id, text[1], m.from_user.id)
add_to_queue(m.chat.id, text[1], m.from_user.id)
await m.reply("added to queue")
@vc.on_stream_end()
async def streamhandler(vc: PyTgCalls, update: Update):
if not get_from_queue(update.chat_id):
return await vc.leave_group_call(update.chat_id)
song, from_user = get_from_queue(update.chat_id)
ytdetails = await get_yt_dict(song)
remote = await yt_stream(song)
title = ytdetails["title"]
thumb = ytdetails["thumbnails"][1]["url"]
xx = datetime.timedelta(seconds=ytdetails["duration"])
if str(xx).startswith("0"):
duration = (str(xx)[2:])
else:
duration = str(xx)
msg = f"Playing {title} !"
await vc.change_stream(update.chat_id, AudioPiped(remote, HighQualityAudio()))
QUEUE[update.chat_id].pop(0)
await bot.send_photo(update.chat_id, f"https://i.ytimg.com/vi/{ytdetails['id']}/maxresdefault.jpg", caption=f"Playing: `{title}`\nDuration: `{duration}`")
bot.start()
vc.start()
idle()