Spaces:
Sleeping
Sleeping
File size: 7,796 Bytes
89a7a7a aef1161 89a7a7a aef1161 89a7a7a aef1161 89a7a7a aef1161 89a7a7a aef1161 89a7a7a aef1161 89a7a7a aef1161 aee3bb4 89a7a7a aef1161 89a7a7a aef1161 89a7a7a 1c016d9 aef1161 89a7a7a aef1161 89a7a7a aef1161 1c016d9 89a7a7a aef1161 89a7a7a aef1161 89a7a7a |
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 |
import json
import os
from urllib import parse
import yt_dlp
from pyrogram.types import InlineKeyboardButton as IKB
from pyrogram.types import InlineKeyboardMarkup as IKM
from pyrogram.types import Message
from Powers.bot_class import 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
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"
class GOJO_YTS:
"""
A class to fetch link of from youtube using the name provided
Creadit: Hellbot
"""
def __init__(self, search_terms: str, max_results=None):
self.search_terms = search_terms
self.max_results = max_results
self.videos = self._search()
def _search(self):
encoded_search = parse.quote_plus(self.search_terms)
BASE_URL = "https://youtube.com"
url = f"{BASE_URL}/results?search_query={encoded_search}"
response = requests.get(url).text
while "ytInitialData" not in response:
response = requests.get(url).text
results = self._parse_html(response)
if self.max_results is not None and len(results) > self.max_results:
return results[: self.max_results]
return results
def _parse_html(self, response):
results = []
start = response.index("ytInitialData") + len("ytInitialData") + 3
end = response.index("};", start) + 1
json_str = response[start:end]
data = json.loads(json_str)
videos = data["contents"]["twoColumnSearchResultsRenderer"]["primaryContents"][
"sectionListRenderer"
]["contents"][0]["itemSectionRenderer"]["contents"]
for video in videos:
res = {}
if "videoRenderer" in video.keys():
video_data = video.get("videoRenderer", {})
res["id"] = video_data.get("videoId", None)
res["thumbnails"] = [
thumb.get("url", None)
for thumb in video_data.get("thumbnail", {}).get("thumbnails", [{}])
]
res["title"] = (
video_data.get("title", {}).get("runs", [[{}]])[0].get("text", None)
)
res["long_desc"] = (
video_data.get("descriptionSnippet", {})
.get("runs", [{}])[0]
.get("text", None)
)
res["channel"] = (
video_data.get("longBylineText", {})
.get("runs", [[{}]])[0]
.get("text", None)
)
res["duration"] = video_data.get("lengthText", {}).get("simpleText", 0)
res["views"] = video_data.get("viewCountText", {}).get("simpleText", 0)
res["publish_time"] = video_data.get("publishedTimeText", {}).get(
"simpleText", 0
)
res["url_suffix"] = (
video_data.get("navigationEndpoint", {})
.get("commandMetadata", {})
.get("webCommandMetadata", {})
.get("url", None)
)
results.append(res)
return results
def to_dict(self, clear_cache=True):
result = self.videos
if clear_cache:
self.videos = ""
return result
def to_json(self, clear_cache=True):
result = json.dumps({"videos": self.videos})
if clear_cache:
self.videos = ""
return result
# Gets yt result of given query.
async def song_search(query, max_results=1):
try:
results = json.loads(GOJO_YTS(query, max_results=max_results).to_json())
except KeyError:
return
yt_dict = {}
nums = 1
for i in results["videos"]:
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": f"https://www.youtube.com{i['url_suffix']}",
"title": i['title'],
"views": i['views'],
"channel": i['channel'],
"duration": i['duration'],
"thumbnail": i['thumbnails'][0]
}
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)
if is_direct:
query = query
else:
dicti = await song_search(query, 1)
if not dicti:
await m.reply_text("File with duration less than or equals to 5 minutes is allowed only")
try:
query = dicti[1]['link']
except KeyError:
z = "KeyError"
return z
FILE = ydl.extract_info(query,download=video)
if int(FILE['duration']) > 600:
await m.reply_text("File with duration less than or equals to 5 minutes is allowed only")
return
f_name = FILE['title']
uploader = FILE['uploader']
up_url = FILE['uploader_url']
views = FILE['view_count']
url = query
if song:
f_down = ydl.prepare_filename(FILE)
f_path = f"{f_down}.mp3"
thumb = f"{f_down}.webp"
ydl.download([query])
elif video:
f_path = open(f"{FILE['id']}.mp4","rb")
cap = f"""
✘ Name: `{f_name}`
✘ Views: `{views}`
"""
kb = IKM(
[
[
IKB(f"✘ {uploader.capitalize()} ✘",url=f"{up_url}"),
IKB(f"✘ Youtube url ✘", url=f"{url}")
]
]
)
if video:
await m.reply_video(f_path,caption=cap,reply_markup=kb,duration=int(FILE['duration']))
os.remove(f"./{FILE['id']}.mp4")
return
elif song:
await m.reply_audio(f_path,caption=cap,reply_markup=kb,duration=int(FILE['duration']),thumb=thumb,title=f_name)
os.remove(f_path)
os.remove(thumb)
return
|