-
Notifications
You must be signed in to change notification settings - Fork 1
/
rename.py
212 lines (199 loc) · 7.84 KB
/
rename.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
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
import os
import subprocess
import time
import datetime
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
from telethon.tl.types import DocumentAttributeVideo
from . import *
thumb_image_path = Config.TMP_DOWNLOAD_DIRECTORY + "/thumb_image.jpg"
def get_video_thumb(file, output=None, width=90):
metadata = extractMetadata(createParser(file))
p = subprocess.Popen(
[
"ffmpeg",
"-i",
file,
"-ss",
str(
int((0, metadata.get("duration").seconds)[metadata.has("duration")] / 2)
),
"-filter:v",
"scale={}:-1".format(width),
"-vframes",
"1",
output,
],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
if not p.returncode and os.path.lexists(file):
return output
@bot.on(d3vil_cmd(pattern="rename (.*)", outgoing=True))
@bot.on(sudo_cmd(pattern="rename (.*)", allow_sudo=True))
async def _(event):
if event.fwd_from:
return
d3vil = await eor(event,
"Renaming in progress...\nThis might take some time if file is big. 🥴"
)
input_str = event.pattern_match.group(1)
if not os.path.isdir(Config.TMP_DOWNLOAD_DIRECTORY):
os.makedirs(Config.TMP_DOWNLOAD_DIRECTORY)
if event.reply_to_msg_id:
start = datetime.datetime.now()
file_name = input_str
reply_message = await event.get_reply_message()
# c_time = time.time()
to_download_directory = Config.TMP_DOWNLOAD_DIRECTORY
downloaded_file_name = os.path.join(to_download_directory, file_name)
downloaded_file_name = await bot.download_media(
reply_message, downloaded_file_name
)
end = datetime.datetime.now()
ms = (end - start).seconds
if os.path.exists(downloaded_file_name):
await d3vil.edit("Downloaded to `{}` in {} seconds.".format(downloaded_file_name, ms)
)
else:
await eod(d3vil, "Error Occurred\n {}".format(input_str))
else:
await eod(d3vil, f"Syntax `{hl}rename file.name` as reply to a Telegram media")
@bot.on(d3vil_cmd(pattern="rnupload (.*)", outgoing=True))
@bot.on(sudo_cmd(pattern="rnupload (.*)", allow_sudo=True))
async def _(event):
if event.fwd_from:
return
thumb = None
if os.path.exists(thumb_image_path):
thumb = thumb_image_path
d3vil = await eor(event,
"Renaming And Uploading File..."
)
input_str = event.pattern_match.group(1)
if not os.path.isdir(Config.TMP_DOWNLOAD_DIRECTORY):
os.makedirs(Config.TMP_DOWNLOAD_DIRECTORY)
if event.reply_to_msg_id:
start = datetime.datetime.now()
file_name = input_str
reply_message = await event.get_reply_message()
to_download_directory = Config.TMP_DOWNLOAD_DIRECTORY
downloaded_file_name = os.path.join(to_download_directory, file_name)
downloaded_file_name = await bot.download_media(
reply_message, downloaded_file_name
)
end = datetime.datetime.now()
ms_one = (end - start).seconds
if os.path.exists(downloaded_file_name):
time.time()
await bot.send_file(
event.chat_id,
downloaded_file_name,
force_document=False,
supports_streaming=False,
allow_cache=False,
reply_to=event.message.id,
thumb=thumb,
)
end_two = datetime.datetime.now()
os.remove(downloaded_file_name)
ms_two = (end_two - end).seconds
await d3vil.edit("Downloaded in {} seconds. Uploaded in {} seconds.".format(
ms_one, ms_two
)
)
else:
await eod(event, "File Not Found {}".format(input_str))
else:
await d3vil.edit("Syntax // `{}rnupload file.name` as reply to a Telegram media".format(hl))
@bot.on(d3vil_cmd(pattern="rnsupload (.*)", outgoing=True))
@bot.on(sudo_cmd(pattern="rnsupload (.*)", allow_sudo=True))
async def _(event):
if event.fwd_from:
return
d3vil = await eor(event,
"Rename & Upload as streamable format is in progress..."
)
input_str = event.pattern_match.group(1)
if not os.path.isdir(Config.TMP_DOWNLOAD_DIRECTORY):
os.makedirs(Config.TMP_DOWNLOAD_DIRECTORY)
if event.reply_to_msg_id:
start = datetime.datetime.now()
file_name = input_str
reply_message = await event.get_reply_message()
time.time()
to_download_directory = Config.TMP_DOWNLOAD_DIRECTORY
downloaded_file_name = os.path.join(to_download_directory, file_name)
downloaded_file_name = await bot.download_media(
reply_message, downloaded_file_name
)
end_one = datetime.datetime.now()
ms_one = (end_one - start).seconds
if os.path.exists(downloaded_file_name):
thumb = None
if not downloaded_file_name.endswith((".mkv", ".mp4", ".mp3", ".flac")):
await eor(event,
"Sorry. But I don't think {} is a streamable file. Please try again.\n**Supported Formats**: MKV, MP4, MP3, FLAC".format(
downloaded_file_name
)
)
return False
if os.path.exists(thumb_image_path):
thumb = thumb_image_path
else:
thumb = get_video_thumb(downloaded_file_name, thumb_image_path)
start = datetime.datetime.now()
metadata = extractMetadata(createParser(downloaded_file_name))
duration = 0
width = 0
height = 0
if metadata.has("duration"):
duration = metadata.get("duration").seconds
if os.path.exists(thumb_image_path):
metadata = extractMetadata(createParser(thumb_image_path))
if metadata.has("width"):
width = metadata.get("width")
if metadata.has("height"):
height = metadata.get("height")
try:
await bot.send_file(
event.chat_id,
downloaded_file_name,
thumb=thumb,
caption="reuploaded by [D3vilBot](https://t.me/d3vilbot_official_chat)",
force_document=False,
allow_cache=False,
reply_to=event.message.id,
attributes=[
DocumentAttributeVideo(
duration=duration,
w=width,
h=height,
round_message=False,
supports_streaming=True,
)
],
)
except Exception as e:
await d3vil.edit(event, str(e))
else:
end = datetime.datetime.now()
os.remove(downloaded_file_name)
ms_two = (end - end_one).seconds
await d3vil.edit("Downloaded in {} seconds. Uploaded in {} seconds.".format(
ms_one, ms_two
)
)
else:
await eod(d3vil, "File Not Found {}".format(input_str))
else:
await d3vil.edit(
"Syntax // .rnsupload file.name as reply to a Telegram media"
)
CmdHelp("rename").add_command(
"rename", "<reply to media> <new name>", "Renames the replied media and downloads it to userbot local storage"
).add_command(
"rnupload", "<reply to media> <new name>", "Renames the replied media and directly uploads it to the chat"
).add_command(
"rnsupload", "<reply to media> <new name>", "Renames the replied media and directly upload in streamable format."
).add()