-
Notifications
You must be signed in to change notification settings - Fork 1
/
да.py
174 lines (163 loc) · 5.36 KB
/
да.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
""" Userbot module containing various scrapers. """
import os
import time
import asyncio
import shutil
from bs4 import BeautifulSoup
import re
from html import unescape
from requests import get
from youtube_dl import YoutubeDL
from youtube_dl.utils import (
DownloadError,
ContentTooShortError,
ExtractorError,
GeoRestrictedError,
MaxDownloadsReached,
PostProcessingError,
UnavailableVideoError,
XAttrMetadataError,
)
from asyncio import sleep
from userbot import (
CMD_HELP,
BOTLOG,
BOTLOG_CHATID,
YOUTUBE_API_KEY,
CHROME_DRIVER,
GOOGLE_CHROME_BIN,
)
from userbot.events import register
from telethon.tl.types import DocumentAttributeAudio
from uniborg.util import progress, humanbytes, time_formatter
@register(outgoing=True, pattern=r".rip (audio|video) (.*)")
async def download_video(v_url):
"""For .rip command, download media from YouTube and many other sites."""
url = v_url.pattern_match.group(2)
type = v_url.pattern_match.group(1).lower()
await v_url.edit("`Preparing to download...`")
if type == "audio":
opts = {
"format": "bestaudio",
"addmetadata": True,
"key": "FFmpegMetadata",
"writethumbnail": True,
"prefer_ffmpeg": True,
"geo_bypass": True,
"nocheckcertificate": True,
"postprocessors": [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "320",
}
],
"outtmpl": "%(id)s.mp3",
"quiet": True,
"logtostderr": False,
}
video = False
song = True
elif type == "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,
}
song = False
video = True
try:
await v_url.edit("`Fetching data, please wait..`")
with YoutubeDL(opts) as rip:
rip_data = rip.extract_info(url)
except DownloadError as DE:
await v_url.edit(f"`{str(DE)}`")
return
except ContentTooShortError:
await v_url.edit("`The download content was too short.`")
return
except GeoRestrictedError:
await v_url.edit(
"`Video is not available from your geographic location due to geographic restrictions imposed by a website.`"
)
return
except MaxDownloadsReached:
await v_url.edit("`Max-downloads limit has been reached.`")
return
except PostProcessingError:
await v_url.edit("`There was an error during post processing.`")
return
except UnavailableVideoError:
await v_url.edit("`Media is not available in the requested format.`")
return
except XAttrMetadataError as XAME:
await v_url.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`")
return
except ExtractorError:
await v_url.edit("`There was an error during info extraction.`")
return
except Exception as e:
await v_url.edit(f"{str(type(e)): {str(e)}}")
return
c_time = time.time()
if song:
await v_url.edit(
f"`Preparing to upload song:`\
\n**{rip_data['title']}**\
\nby *{rip_data['uploader']}*"
)
await v_url.client.send_file(
v_url.chat_id,
f"{rip_data['id']}.mp3",
supports_streaming=True,
attributes=[
DocumentAttributeAudio(
duration=int(rip_data["duration"]),
title=str(rip_data["title"]),
performer=str(rip_data["uploader"]),
)
],
progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
progress(d, t, v_url, c_time, "Uploading..", f"{rip_data['title']}.mp3")
),
)
os.remove(f"{rip_data['id']}.mp3")
await v_url.delete()
elif video:
await v_url.edit(
f"`Preparing to upload video:`\
\n**{rip_data['title']}**\
\nby *{rip_data['uploader']}*"
)
await v_url.client.send_file(
v_url.chat_id,
f"{rip_data['id']}.mp4",
supports_streaming=True,
caption=rip_data["title"],
progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
progress(d, t, v_url, c_time, "Uploading..", f"{rip_data['title']}.mp4")
),
)
os.remove(f"{rip_data['id']}.mp4")
await v_url.delete()
CMD_HELP.update(
{
"yt": ".yt <text>\
\nUsage: Does a YouTube search."
}
)
CMD_HELP.update(
{
"rip": ".ripaudio <url> or ripvideo <url>\
\nUsage: Download videos and songs from YouTube (and [many other sites](https://ytdl-org.github.io/youtube-dl/supportedsites.html))."
}
)