taslim19 commited on
Commit
75f8d4a
·
1 Parent(s): 4ebaa1c

fix(youtube): implement search and fix callback

Browse files
DragMusic/platforms/Youtube.py CHANGED
@@ -67,6 +67,25 @@ class YouTubeAPI:
67
  info = await self.get_info(link)
68
  return info.get("thumbnail", "")
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  async def get_best_audio_url(self, link: str):
71
  """Gets the URL of the best quality audio stream."""
72
  info = await self.get_info(link)
 
67
  info = await self.get_info(link)
68
  return info.get("thumbnail", "")
69
 
70
+ async def search(self, query: str):
71
+ """Searches for a video and returns its details."""
72
+ search_opts = self.opts.copy()
73
+ try:
74
+ with yt_dlp.YoutubeDL(search_opts) as ydl:
75
+ info = ydl.extract_info(f"ytsearch:{query}", download=False)['entries'][0]
76
+
77
+ details = {
78
+ "title": info.get("title", "Unknown Title"),
79
+ "link": info.get("webpage_url", ""),
80
+ "vidid": info.get("id", ""),
81
+ "duration_min": info.get("duration_string", "0:00"),
82
+ "thumb": info.get("thumbnail", ""),
83
+ }
84
+ track_id = info.get("id", "")
85
+ return details, track_id
86
+ except Exception:
87
+ return None, None
88
+
89
  async def get_best_audio_url(self, link: str):
90
  """Gets the URL of the best quality audio stream."""
91
  info = await self.get_info(link)
DragMusic/plugins/play/play.py CHANGED
@@ -157,35 +157,23 @@ async def play_commnd(
157
  return await mystic.delete()
158
  return
159
  elif url:
160
- if await YouTube.exists(url):
161
- if "playlist" in url:
162
- try:
163
- details = await YouTube.playlist(
164
- url,
165
- config.PLAYLIST_FETCH_LIMIT,
166
- message.from_user.id,
167
- )
168
- except:
169
- return await mystic.edit_text(_["play_3"])
170
- streamtype = "playlist"
171
- plist_type = "yt"
172
- if "&" in url:
173
- plist_id = (url.split("=")[1]).split("&")[0]
174
- else:
175
- plist_id = url.split("=")[1]
176
- img = config.PLAYLIST_IMG_URL
177
- cap = _["play_9"]
178
- else:
179
- try:
180
- details, track_id = await YouTube.track(url)
181
- except:
182
- return await mystic.edit_text(_["play_3"])
183
- streamtype = "youtube"
184
- img = details["thumb"]
185
- cap = _["play_10"].format(
186
- details["title"],
187
- details["duration_min"],
188
  )
 
 
 
 
 
 
 
 
 
 
189
  elif await Spotify.valid(url):
190
  spotify = True
191
  if not config.SPOTIFY_CLIENT_ID and not config.SPOTIFY_CLIENT_SECRET:
@@ -249,7 +237,7 @@ async def play_commnd(
249
  cap = _["play_12"].format(app.mention, message.from_user.mention)
250
  img = url
251
  else:
252
- return await mystic.edit_text(_["play_3"])
253
  elif await Resso.valid(url):
254
  try:
255
  details, track_id = await Resso.track(url)
@@ -260,104 +248,48 @@ async def play_commnd(
260
  cap = _["play_10"].format(details["title"], details["duration_min"])
261
  elif await SoundCloud.valid(url):
262
  try:
263
- details, track_path = await SoundCloud.download(url)
264
  except:
265
  return await mystic.edit_text(_["play_3"])
266
- duration_sec = details["duration_sec"]
267
- if duration_sec > config.DURATION_LIMIT:
268
- return await mystic.edit_text(
269
- _["play_6"].format(
270
- config.DURATION_LIMIT_MIN,
271
- app.mention,
272
- )
273
- )
274
- try:
275
- await stream(
276
- _,
277
- mystic,
278
- user_id,
279
- details,
280
- chat_id,
281
- user_name,
282
- message.chat.id,
283
- streamtype="soundcloud",
284
- forceplay=fplay,
285
- )
286
- except Exception as e:
287
- print(f"Error: {e}")
288
- ex_type = type(e).__name__
289
- err = e if ex_type == "AssistantErr" else _["general_2"].format(ex_type)
290
- return await mystic.edit_text(err)
291
- return await mystic.delete()
292
  else:
293
  try:
294
- await Aviax.stream_call(url)
295
- except NoActiveGroupCall:
296
- await mystic.edit_text(_["black_9"])
297
- return await app.send_message(
298
- chat_id=config.LOG_GROUP_ID,
299
- text=_["play_17"],
300
- )
301
- except Exception as e:
302
- print(f"Error: {e}")
303
- return await mystic.edit_text(_["general_2"].format(type(e).__name__))
304
- await mystic.edit_text(_["str_2"])
305
- try:
306
- await stream(
307
- _,
308
- mystic,
309
- message.from_user.id,
310
- url,
311
- chat_id,
312
- message.from_user.first_name,
313
- message.chat.id,
314
- video=video,
315
- streamtype="index",
316
- forceplay=fplay,
317
  )
318
  except Exception as e:
319
- print(f"Error: {e}")
320
- ex_type = type(e).__name__
321
- err = e if ex_type == "AssistantErr" else _["general_2"].format(ex_type)
322
- return await mystic.edit_text(err)
323
- return await play_logs(message, streamtype="M3u8 or Index Link")
324
  else:
325
  if len(message.command) < 2:
326
- buttons = botplaylist_markup(_)
327
- return await mystic.edit_text(
328
- _["play_18"],
329
- reply_markup=InlineKeyboardMarkup(buttons),
330
- )
331
- slider = True
332
  query = message.text.split(None, 1)[1]
333
- if "-v" in query:
334
- query = query.replace("-v", "")
335
  try:
336
- details, track_id = await YouTube.track(query)
 
 
337
  except:
338
  return await mystic.edit_text(_["play_3"])
339
  streamtype = "youtube"
340
- if str(playmode) == "Direct":
341
- if not plist_type:
342
- if details["duration_min"]:
343
- duration_sec = time_to_seconds(details["duration_min"])
344
- if duration_sec > config.DURATION_LIMIT:
345
- return await mystic.edit_text(
346
- _["play_6"].format(config.DURATION_LIMIT_MIN, app.mention)
347
- )
348
- else:
349
- buttons = livestream_markup(
350
- _,
351
- track_id,
352
- user_id,
353
- "v" if video else "a",
354
- "c" if channel else "g",
355
- "f" if fplay else "d",
356
- )
357
- return await mystic.edit_text(
358
- _["play_13"],
359
- reply_markup=InlineKeyboardMarkup(buttons),
360
- )
361
  try:
362
  await stream(
363
  _,
@@ -368,8 +300,9 @@ async def play_commnd(
368
  user_name,
369
  message.chat.id,
370
  video=video,
371
- streamtype=streamtype,
372
- spotify=spotify,
 
373
  forceplay=fplay,
374
  )
375
  except Exception as e:
@@ -377,65 +310,44 @@ async def play_commnd(
377
  ex_type = type(e).__name__
378
  err = e if ex_type == "AssistantErr" else _["general_2"].format(ex_type)
379
  return await mystic.edit_text(err)
380
- await mystic.delete()
381
- return await play_logs(message, streamtype=streamtype)
382
  else:
383
- if plist_type:
384
- ran_hash = "".join(
385
- random.choices(string.ascii_uppercase + string.digits, k=10)
 
 
 
 
 
 
 
386
  )
387
- lyrical[ran_hash] = plist_id
388
- buttons = playlist_markup(
 
 
 
 
 
 
 
 
389
  _,
390
- ran_hash,
391
- message.from_user.id,
392
- plist_type,
393
- "c" if channel else "g",
394
- "f" if fplay else "d",
395
  )
396
  await mystic.delete()
397
- await message.reply_photo(
 
398
  photo=img,
399
  caption=cap,
400
  reply_markup=InlineKeyboardMarkup(buttons),
401
  )
402
- return await play_logs(message, streamtype=f"Playlist : {plist_type}")
403
- else:
404
- if slider:
405
- buttons = slider_markup(
406
- _,
407
- track_id,
408
- message.from_user.id,
409
- query,
410
- 0,
411
- "c" if channel else "g",
412
- "f" if fplay else "d",
413
- )
414
- await mystic.delete()
415
- await message.reply_photo(
416
- photo=details["thumb"],
417
- caption=_["play_10"].format(
418
- details["title"].title(),
419
- details["duration_min"],
420
- ),
421
- reply_markup=InlineKeyboardMarkup(buttons),
422
- )
423
- return await play_logs(message, streamtype=f"Searched on Youtube")
424
- else:
425
- buttons = track_markup(
426
- _,
427
- track_id,
428
- message.from_user.id,
429
- "c" if channel else "g",
430
- "f" if fplay else "d",
431
- )
432
- await mystic.delete()
433
- await message.reply_photo(
434
- photo=img,
435
- caption=cap,
436
- reply_markup=InlineKeyboardMarkup(buttons),
437
- )
438
- return await play_logs(message, streamtype=f"URL Searched Inline")
439
 
440
 
441
  @app.on_callback_query(filters.regex("MusicStream") & ~BANNED_USERS)
@@ -443,27 +355,32 @@ async def play_commnd(
443
  async def play_music(client, CallbackQuery, _):
444
  callback_data = CallbackQuery.data.strip()
445
  callback_request = callback_data.split(None, 1)[1]
446
- vidid, user_id, mode, cplay, fplay = callback_request.split("|")
 
 
 
 
 
 
447
  if CallbackQuery.from_user.id != int(user_id):
448
  try:
449
  return await CallbackQuery.answer(_["playcb_1"], show_alert=True)
450
  except:
451
  return
452
  try:
453
- chat_id, channel = await get_channeplayCB(_, cplay, CallbackQuery)
454
  except:
455
- return
 
456
  user_name = CallbackQuery.from_user.first_name
 
457
  try:
458
- await CallbackQuery.message.delete()
459
  await CallbackQuery.answer()
460
  except:
461
  pass
462
- mystic = await CallbackQuery.message.reply_text(
463
- _["play_2"].format(channel) if channel else _["play_1"]
464
- )
465
  try:
466
- details, track_id = await YouTube.track(vidid, True)
467
  except:
468
  return await mystic.edit_text(_["play_3"])
469
  if details["duration_min"]:
@@ -475,18 +392,19 @@ async def play_music(client, CallbackQuery, _):
475
  else:
476
  buttons = livestream_markup(
477
  _,
478
- track_id,
479
- CallbackQuery.from_user.id,
480
- mode,
481
- "c" if cplay == "c" else "g",
482
- "f" if fplay else "d",
483
  )
484
  return await mystic.edit_text(
485
  _["play_13"],
486
  reply_markup=InlineKeyboardMarkup(buttons),
487
  )
488
- video = True if mode == "v" else None
489
  ffplay = True if fplay == "f" else None
 
490
  try:
491
  await stream(
492
  _,
@@ -551,4 +469,4 @@ async def play_playlists_command(client, CallbackQuery, _):
551
  _["play_2"].format(channel) if channel else _["play_1"]
552
  )
553
  videoid = lyrical.get(videoid)
554
- video = Tr
 
157
  return await mystic.delete()
158
  return
159
  elif url:
160
+ if "youtube.com/playlist" in url:
161
+ try:
162
+ details = await YouTube.playlist(
163
+ url,
164
+ config.PLAYLIST_FETCH_LIMIT,
165
+ message.from_user.id,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  )
167
+ except:
168
+ return await mystic.edit_text(_["play_3"])
169
+ streamtype = "playlist"
170
+ plist_type = "yt"
171
+ if "&" in url:
172
+ plist_id = (url.split("=")[1]).split("&")[0]
173
+ else:
174
+ plist_id = url.split("=")[1]
175
+ img = config.PLAYLIST_IMG_URL
176
+ cap = _["play_9"]
177
  elif await Spotify.valid(url):
178
  spotify = True
179
  if not config.SPOTIFY_CLIENT_ID and not config.SPOTIFY_CLIENT_SECRET:
 
237
  cap = _["play_12"].format(app.mention, message.from_user.mention)
238
  img = url
239
  else:
240
+ return await mystic.edit_text(_["play_15"])
241
  elif await Resso.valid(url):
242
  try:
243
  details, track_id = await Resso.track(url)
 
248
  cap = _["play_10"].format(details["title"], details["duration_min"])
249
  elif await SoundCloud.valid(url):
250
  try:
251
+ details, track_id = await SoundCloud.track(url)
252
  except:
253
  return await mystic.edit_text(_["play_3"])
254
+ streamtype = "youtube"
255
+ img = details["thumb"]
256
+ cap = _["play_10"].format(details["title"], details["duration_min"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  else:
258
  try:
259
+ title, duration_min, duration_sec, thumbnail, vidid = await YouTube.details(url)
260
+ details = {
261
+ "title": title,
262
+ "link": url,
263
+ "vidid": vidid,
264
+ "duration_min": duration_min,
265
+ "thumb": thumbnail,
266
+ }
267
+ streamtype = "youtube"
268
+ img = details["thumb"]
269
+ cap = _["play_10"].format(
270
+ details["title"],
271
+ details["duration_min"],
 
 
 
 
 
 
 
 
 
 
272
  )
273
  except Exception as e:
274
+ print(e)
275
+ return await mystic.edit_text(_["play_3"])
 
 
 
276
  else:
277
  if len(message.command) < 2:
278
+ return await mystic.edit_text(_["play_16"])
279
+ await mystic.edit_text(_["play_18"])
 
 
 
 
280
  query = message.text.split(None, 1)[1]
 
 
281
  try:
282
+ details, track_id = await YouTube.search(query)
283
+ if details is None:
284
+ return await mystic.edit_text(_["play_3"])
285
  except:
286
  return await mystic.edit_text(_["play_3"])
287
  streamtype = "youtube"
288
+ img = details["thumb"]
289
+ cap = _["play_10"].format(details["title"], details["duration_min"])
290
+ if streamtype == "playlist":
291
+ await mystic.edit_text(_["play_19"])
292
+ await play_logs(message, streamtype="Playlist")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  try:
294
  await stream(
295
  _,
 
300
  user_name,
301
  message.chat.id,
302
  video=video,
303
+ streamtype="playlist",
304
+ playlist_id=plist_id,
305
+ playlist_type=plist_type,
306
  forceplay=fplay,
307
  )
308
  except Exception as e:
 
310
  ex_type = type(e).__name__
311
  err = e if ex_type == "AssistantErr" else _["general_2"].format(ex_type)
312
  return await mystic.edit_text(err)
313
+ return await mystic.delete()
 
314
  else:
315
+ if slider:
316
+ buttons = slider_markup(
317
+ _,
318
+ track_id,
319
+ user_id,
320
+ query,
321
+ 0,
322
+ "play",
323
+ video,
324
+ fplay,
325
  )
326
+ await mystic.delete()
327
+ await app.send_photo(
328
+ chat_id=message.chat.id,
329
+ photo=img,
330
+ caption=_["play_20"].format(app.mention),
331
+ reply_markup=InlineKeyboardMarkup(buttons),
332
+ )
333
+ return
334
+ else:
335
+ buttons = track_markup(
336
  _,
337
+ track_id,
338
+ user_id,
339
+ chat_id,
340
+ video,
341
+ fplay,
342
  )
343
  await mystic.delete()
344
+ await app.send_photo(
345
+ chat_id=message.chat.id,
346
  photo=img,
347
  caption=cap,
348
  reply_markup=InlineKeyboardMarkup(buttons),
349
  )
350
+ return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
351
 
352
 
353
  @app.on_callback_query(filters.regex("MusicStream") & ~BANNED_USERS)
 
355
  async def play_music(client, CallbackQuery, _):
356
  callback_data = CallbackQuery.data.strip()
357
  callback_request = callback_data.split(None, 1)[1]
358
+ (
359
+ videoid,
360
+ user_id,
361
+ what,
362
+ chattype,
363
+ fplay,
364
+ ) = callback_request.split("|")
365
  if CallbackQuery.from_user.id != int(user_id):
366
  try:
367
  return await CallbackQuery.answer(_["playcb_1"], show_alert=True)
368
  except:
369
  return
370
  try:
371
+ chat_id, channel = await get_channeplayCB(_, what, ch)
372
  except:
373
+ return await CallbackQuery.edit_message_text(_["play_14"])
374
+ video = True if what == "v" else None
375
  user_name = CallbackQuery.from_user.first_name
376
+ await CallbackQuery.message.delete()
377
  try:
 
378
  await CallbackQuery.answer()
379
  except:
380
  pass
381
+ mystic = await app.send_message(chat_id, _["play_2"].format(channel))
 
 
382
  try:
383
+ details, track_id = await YouTube.track(videoid)
384
  except:
385
  return await mystic.edit_text(_["play_3"])
386
  if details["duration_min"]:
 
392
  else:
393
  buttons = livestream_markup(
394
  _,
395
+ videoid,
396
+ user_id,
397
+ "play",
398
+ chat_id,
399
+ video,
400
  )
401
  return await mystic.edit_text(
402
  _["play_13"],
403
  reply_markup=InlineKeyboardMarkup(buttons),
404
  )
405
+ video = True if what == "v" else None
406
  ffplay = True if fplay == "f" else None
407
+ await play_logs(CallbackQuery, streamtype="Video" if video else "Audio")
408
  try:
409
  await stream(
410
  _,
 
469
  _["play_2"].format(channel) if channel else _["play_1"]
470
  )
471
  videoid = lyrical.get(videoid)
472
+ video = True if mode == "v" else None