Spaces:
Running
Running
File size: 6,005 Bytes
d720c41 3c05ff4 d720c41 3c05ff4 d720c41 3c05ff4 d720c41 d7e1221 3c05ff4 d7e1221 d720c41 d7e1221 d720c41 d7e1221 d720c41 d7e1221 d720c41 3c05ff4 d720c41 3c05ff4 d720c41 3c05ff4 d7e1221 3c05ff4 d7e1221 af89290 d720c41 3c05ff4 d7e1221 d720c41 d7e1221 d720c41 3c05ff4 d720c41 3c05ff4 d720c41 3c05ff4 d720c41 3c05ff4 d720c41 3c05ff4 d720c41 3c05ff4 d7e1221 3c05ff4 d720c41 3c05ff4 d720c41 3c05ff4 d720c41 3c05ff4 d720c41 3c05ff4 d7e1221 d720c41 3c05ff4 |
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 |
import json
import os
from traceback import format_exc
from urllib import parse
from pyrogram.types import InlineKeyboardButton as IKB
from pyrogram.types import InlineKeyboardMarkup as IKM
from pyrogram.types import Message
# import yt_dlp
from pytube import YouTube
from youtubesearchpython.__future__ import Video, VideosSearch
from Powers.bot_class import LOGGER, MESSAGE_DUMP, Gojo
from Powers.utils.http_helper import *
async def get_file_size(file: Message):
if file.photo:
size = file.photo.file_size/1024
elif file.document:
size = file.document.file_size/1024
elif file.video:
size = file.video.file_size/1024
elif file.audio:
size = file.audio.file_size/1024
elif file.sticker:
size = file.sticker.file_size/1024
elif file.animation:
size = file.animation.file_size/1024
elif file.voice:
size = file.voice.file_size/1024
elif file.video_note:
size = file.video_note.file_size/1024
if size <= 1024:
return f"{round(size)} kb"
elif size > 1024:
size = size/1024
if size <= 1024:
return f"{round(size)} mb"
elif size > 1024:
size = size/1024
return f"{round(size)} gb"
def get_duration_in_sec(dur: str):
duration = dur.split(":")
if len(duration) == 2:
dur = (int(duration[0]) * 60) + int(duration[1])
else:
dur = int(duration[0])
return dur
# Gets yt result of given query.
async def song_search(query, is_direct, max_results=1):
yt_dict = {}
try:
if is_direct:
vid = Video.getInfo(query)
query = vid["title"]
else:
query = query
videos = VideosSearch(query,max_results)
results = await videos.next()
except Exception as e:
print(e)
nums = 1
for i in results["result"]:
durr = i['duration'].split(":")
if len(durr) == 3:
hour_to_sec = int(durr[0])*60*60
minutes_to_sec = int(durr[1])*60
total = hour_to_sec + minutes_to_sec + int(durr[2])
if len(durr) == 2:
minutes_to_sec = int(durr[0])*60
total = minutes_to_sec + int(durr[1])
if not (total > 600):
dict_form = {
"link": i["link"],
"title": i["title"],
"views": i["viewCount"]["short"],
"channel": i["channel"]["link"],
"duration": i["accessibility"]['duration'],
"DURATION": i["duration"],
"thumbnail": i["richThumbnail"]["url"],
"published": i["publishedTime"],
"uploader": i ["channel"]["name"]
}
yt_dict.update({nums: dict_form})
nums += 1
else:
pass
return yt_dict
"""song_opts = {
"format": "bestaudio",
"addmetadata": True,
"key": "FFmpegMetadata",
"writethumbnail": True,
"prefer_ffmpeg": True,
"geo_bypass": True,
"nocheckcertificate": True,
"postprocessors": [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "480",
}
],
"outtmpl": "%(id)s.mp3",
"quiet": True,
"logtostderr": False,
}
video_opts = {
"format": "best",
"addmetadata": True,
"key": "FFmpegMetadata",
"prefer_ffmpeg": True,
"geo_bypass": True,
"nocheckcertificate": True,
"postprocessors": [
{
"key": "FFmpegVideoConvertor",
"preferedformat": "mp4",
}
],
"outtmpl": "%(id)s.mp4",
"logtostderr": False,
"quiet": True,
}"""
async def youtube_downloader(c:Gojo,m:Message,query:str,is_direct:bool,type_:str):
if type_ == "a":
# opts = song_opts
video = False
song = True
elif type_ == "v":
# opts = video_opts
video = True
song = False
# ydl = yt_dlp.YoutubeDL(opts)
dicti = await song_search(query, is_direct,1)
if not dicti and type(dicti) != str:
await m.reply_text("File with duration less than or equals to 5 minutes is allowed only")
elif type(dicti) == str:
await m.reply_text(dicti)
return
try:
query = dicti[1]['link']
except KeyError:
return
yt = YouTube(query)
dicti = dicti[1]
f_name = dicti["title"]
views = dicti["views"]
up_url = dicti["channel"]
uploader = dicti["uploader"]
dura = dicti["duration"]
thumb = dicti["thumbnail"]
vid_dur = get_duration_in_sec(dicti["DURATION"])
published_on = dicti["published"]
thumb_ = await c.send_photo(-1001586309125,thumb)
# FILE = ydl.extract_info(query,download=video)
url = query
thumb = await thumb_.download()
await thumb_.delete()
cap = f"""
⤷ Name: `{f_name}`
⤷ Duration: `{dura}`
⤷ Views: `{views}`
⤷ Published: `{published_on}`
"""
kb = IKM(
[
[
IKB(f"✘ {uploader.capitalize()} ✘",url=f"{up_url}")
],
[
IKB(f"✘ Youtube url ✘", url=f"{url}")
]
]
)
if song:
audio_stream= yt.streams.filter(only_audio=True).first()
f_path = audio_stream.download("/youtube_downloads")
file_path = f"/youtube_downloads/{f_name.strip()}.mp3"
os.rename(f_path,file_path)
await m.reply_audio(file_path,caption=cap,reply_markup=kb,duration=vid_dur,thumb=thumb,title=f_name)
os.remove(f_path)
os.remove(file_path)
os.remove(thumb)
return
elif video:
video_stream = yt.streams.get_highest_resolution()
video_stream.download("/youtube_downloads",f"{f_name}.mp4")
file_path = f"/youtube_downloads/{f_name}.mp4"
await m.reply_video(file_path,caption=cap,reply_markup=kb,duration=vid_dur,thumb=thumb)
os.remove(file_path)
os.remove(thumb)
return |