From eed5aaa0f76bc15b0efd9ef83ecf26c8f646b73e Mon Sep 17 00:00:00 2001 From: subinps <64341611+subinps@users.noreply.github.com> Date: Tue, 12 Oct 2021 17:14:28 +0530 Subject: [PATCH] support for both player and scheduler --- plugins/player.py | 59 ++++++++++++++++++++++++--------------- plugins/scheduler.py | 42 ++++++++++++++-------------- utils/utils.py | 66 ++++++++++++++++++-------------------------- 3 files changed, 84 insertions(+), 83 deletions(-) diff --git a/plugins/player.py b/plugins/player.py index 3cac62a1..e4d0622e 100644 --- a/plugins/player.py +++ b/plugins/player.py @@ -43,7 +43,8 @@ start_stream, stream_from_link, chat_filter, - c_play + c_play, + is_ytdl_supported ) from pyrogram.types import ( InlineKeyboardMarkup, @@ -76,6 +77,7 @@ async def add_to_playlist(_, message: Message): type="" yturl="" ysearch="" + url="" if message.command[0] == "fplay": if not (message.from_user is None and message.sender_chat or message.from_user.id in admins): k=await message.reply("This command is only for admins.") @@ -119,20 +121,25 @@ async def add_to_playlist(_, message: Message): except: has_audio_ = False LOGGER.error("Unable to get Audio properties within time.") - if not has_audio_: - await msg.edit("This is an invalid link, provide me a direct link or a youtube link.") - await delete_messages([message, msg]) - return - try: - dur=await get_duration(query) - except: - dur=0 - if dur == 0: - await msg.edit("This is a live stream, Use /stream command.") - await delete_messages([message, msg]) - return - type="direct" - url=query + if has_audio_: + try: + dur=await get_duration(query) + except: + dur=0 + if dur == 0: + await msg.edit("This is a live stream, Use /stream command.") + await delete_messages([message, msg]) + return + type="direct" + url=query + else: + if is_ytdl_supported(query): + type="ytdl_s" + url=query + else: + await msg.edit("This is an invalid link, provide me a direct link or a youtube link.") + await delete_messages([message, msg]) + return else: type="query" ysearch=query @@ -165,7 +172,7 @@ async def add_to_playlist(_, message: Message): Config.playlist.append(data) await add_to_db_playlist(data) await msg.edit("Media added to playlist") - elif type=="youtube" or type=="query": + elif type in ["youtube", "query", "ytdl_s"]: if type=="youtube": await msg.edit("⚡️ **Fetching Video From YouTube...**") url=yturl @@ -183,6 +190,8 @@ async def add_to_playlist(_, message: Message): LOGGER.error(str(e), exc_info=True) await delete_messages([message, msg]) return + elif type == "ytdl_s": + url=url else: return ydl_opts = { @@ -201,11 +210,18 @@ async def add_to_playlist(_, message: Message): LOGGER.error(str(e)) await delete_messages([message, msg]) return - title = info["title"] - if info['duration'] is None: - await msg.edit("This is a live stream, Use /stream command.") - await delete_messages([message, msg]) - return + if type == "ytdl_s": + title = "Music" + try: + title = info['title'] + except: + pass + else: + title = info["title"] + if info['duration'] is None: + await msg.edit("This is a live stream, Use /stream command.") + await delete_messages([message, msg]) + return data={1:title, 2:url, 3:"youtube", 4:user, 5:f"{nyav}_{user_id}"} if message.command[0] == "fplay": pla = [data] + Config.playlist @@ -359,7 +375,6 @@ async def channel_play_list(client, m: Message): - @Client.on_message(filters.command(["yplay", f"yplay@{Config.BOT_USERNAME}"]) & admin_filter & chat_filter) async def yt_play_list(client, m: Message): with suppress(MessageIdInvalid, MessageNotModified): diff --git a/plugins/scheduler.py b/plugins/scheduler.py index ccf754c1..e360ee22 100644 --- a/plugins/scheduler.py +++ b/plugins/scheduler.py @@ -37,7 +37,8 @@ sync_to_db, is_audio, chat_filter, - scheduler + scheduler, + is_ytdl_supported ) from pyrogram.types import ( @@ -95,27 +96,15 @@ async def schedule_vc(bot, message): type="youtube" yturl=query elif query.startswith("http"): - """if Config.IS_VIDEO: - try: - width, height = get_height_and_width(query) - except: - width, height = None, None - LOGGER.error("Unable to get video properties within time.") - if not width or \ - not height: + has_audio_ = await is_audio(query) + if not has_audio_: + if is_ytdl_supported(query): + type="ytdl_s" + url=query + else: await msg.edit("This is an invalid link, provide me a direct link or a youtube link.") await delete_messages([message, msg]) - return """ - #else: - try: - has_audio_ = await is_audio(query) - except: - has_audio_ = False - LOGGER.error("Unable to get Audio properties within time.") - if not has_audio_: - await msg.edit("This is an invalid link, provide me a direct link or a youtube link.") - await delete_messages([message, msg]) - return + return type="direct" url=query else: @@ -145,7 +134,7 @@ async def schedule_vc(bot, message): sid=f"{message.chat.id}_{msg.message_id}" Config.SCHEDULED_STREAM[sid] = data await sync_to_db() - elif type=="youtube" or type=="query": + elif type in ["youtube", "query", "ytdl_s"]: if type=="youtube": await msg.edit("⚡️ **Fetching Video From YouTube...**") url=yturl @@ -163,6 +152,8 @@ async def schedule_vc(bot, message): LOGGER.error(str(e), exc_info=True) await delete_messages([message, msg]) return + elif type == "ytdl_s": + url=url else: return ydl_opts = { @@ -181,7 +172,14 @@ async def schedule_vc(bot, message): LOGGER.error(str(e)) await delete_messages([message, msg]) return - title = info["title"] + if type == "ytdl_s": + title = "Music" + try: + title=info['title'] + except: + pass + else: + title = info["title"] data={'1':title, '2':url, '3':"youtube", '4':user, '5':f"{nyav}_{user_id}"} sid=f"{message.chat.id}_{msg.message_id}" Config.SCHEDULED_STREAM[sid] = data diff --git a/utils/utils.py b/utils/utils.py index 9c5e114c..4f36ba3c 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -21,7 +21,7 @@ from apscheduler.jobstores.base import ConflictingIdError from pyrogram.raw.functions.channels import GetFullChannel from pytgcalls import StreamType - from yt_dlp import YoutubeDL + import yt_dlp from pyrogram import filters from pymongo import MongoClient from datetime import datetime @@ -36,7 +36,6 @@ import asyncio import json import random - import re import time import sys import os @@ -109,6 +108,7 @@ async def play(): if not file: file = await dl.pyro_dl(song[2]) Config.GET_FILE[song[5]] = file + await sleep(3) while not os.path.exists(file): file=Config.GET_FILE.get(song[5]) await sleep(1) @@ -705,7 +705,7 @@ async def restart_playout(): def is_ytdl_supported(input_url: str) -> bool: - shei = YoutubeDL.extractor.gen_extractors() + shei = yt_dlp.extractor.gen_extractors() return any(int_extraactor.suitable(input_url) and int_extraactor.IE_NAME != "generic" for int_extraactor in shei) @@ -777,44 +777,32 @@ async def stream_from_link(link): return True, None - async def get_link(file): - def_ydl_opts = {'quiet': True, 'prefer_insecure': False, "geo-bypass": True} - with YoutubeDL(def_ydl_opts) as ydl: - try: - ydl_info = ydl.extract_info(file, download=False) - except Exception as e: - LOGGER.error(f"Errors occured while getting link from youtube video {e}", exc_info=True) - if Config.playlist or Config.STREAM_LINK: - return await skip() - else: - LOGGER.error("This stream is not supported , leaving VC.") - return False - url=None - for each in ydl_info['formats']: - if each['width'] == 640 \ - and each['acodec'] != 'none' \ - and each['vcodec'] != 'none': - url=each['url'] - break #prefer 640x360 - elif each['width'] \ - and each['width'] <= 1280 \ - and each['acodec'] != 'none' \ - and each['vcodec'] != 'none': - url=each['url'] - continue # any other format less than 1280 - else: - continue - if url: - return url + ytdl_cmd = [ "yt-dlp", "--geo-bypass", "-g", "-f", "best[height<=?720][width<=?1280]/best", file] + process = await asyncio.create_subprocess_exec( + *ytdl_cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE + ) + output, err = await process.communicate() + if not output: + LOGGER.error(str(err.decode())) + if Config.playlist or Config.STREAM_LINK: + return await skip() else: - LOGGER.error(f"Errors occured while getting link from youtube video - No Video Formats Found") - if Config.playlist or Config.STREAM_LINK: - return await skip() - else: - LOGGER.error("This stream is not supported , leaving VC.") - return False - + LOGGER.error("This stream is not supported , leaving VC.") + await leave_call() + return False + stream = output.decode().strip() + link = (stream.split("\n"))[-1] + if link: + return link + else: + LOGGER.error("Unable to get sufficient info from link") + if Config.playlist or Config.STREAM_LINK: + return await skip() + else: + LOGGER.error("This stream is not supported , leaving VC.") + await leave_call() + return False async def download(song, msg=None):