-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathsend_media.py
368 lines (344 loc) · 12.8 KB
/
send_media.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import asyncio
import os
import time
from pathlib import Path
from uuid import uuid4
import telethon
from telethon import Button, TelegramClient, events, utils
from telethon.events.newmessage import NewMessage
from telethon.tl.functions.channels import GetMessagesRequest
from telethon.tl.functions.messages import ForwardMessagesRequest
from telethon.tl.patched import Message
from telethon.tl.types import Document
from telethon.types import UpdateEditMessage
from cansend import CanSend
from config import BOT_USERNAME, PRIVATE_CHAT_ID
from FastTelethon import upload_file
from redis_db import db
from tools import (
convert_seconds,
download_file,
download_image_to_bytesio,
extract_code_from_url,
get_formatted_size,
)
class VideoSender:
def __init__(
self,
client: TelegramClient,
message: NewMessage.Event,
edit_message: Message,
url: str,
data,
):
self.client = client
self.data = data
self.url = url
self.edit_message = edit_message
self.message = message
self.uuid = str(uuid4())
self.stop_sending = False
self.thumbnail = self.get_thumbnail()
self.can_send = CanSend()
self.start_time = time.time()
self.task = None
self.client.add_event_handler(
self.stop, events.CallbackQuery(pattern=f"^stop{self.uuid}")
)
self.caption = f"""
File Name: `{self.data['file_name']}`
Size: **{self.data["size"]}**
@RoldexVerse
"""
self.caption2 = f"""
Downloading `{self.data['file_name']}`
Size: **{self.data["size"]}**
@RoldexVerse
"""
async def progress_bar(self, current_downloaded, total_downloaded, state="Sending"):
if not self.can_send.can_send():
return
bar_length = 20
percent = current_downloaded / total_downloaded
arrow = "█" * int(percent * bar_length)
spaces = "░" * (bar_length - len(arrow))
elapsed_time = time.time() - self.start_time
head_text = f"{state} `{self.data['file_name']}`"
progress_bar = f"[{arrow + spaces}] {percent:.2%}"
upload_speed = current_downloaded / elapsed_time if elapsed_time > 0 else 0
speed_line = f"Speed: **{get_formatted_size(upload_speed)}/s**"
time_remaining = (
(total_downloaded - current_downloaded) / upload_speed
if upload_speed > 0
else 0
)
time_line = f"Time Remaining: `{convert_seconds(time_remaining)}`"
size_line = f"Size: **{get_formatted_size(current_downloaded)}** / **{get_formatted_size(total_downloaded)}**"
await self.edit_message.edit(
f"{head_text}\n{progress_bar}\n{speed_line}\n{time_line}\n{size_line}",
parse_mode="markdown",
buttons=[Button.inline("Stop", data=f"stop{self.uuid}")],
)
async def send_media(self, shorturl):
try:
self.thumbnail.seek(0) if self.thumbnail else None
spoiler_media = (
await self.client._file_to_media(
self.data["direct_link"],
supports_streaming=True,
progress_callback=self.progress_bar,
thumb=self.thumbnail,
)
)[1]
spoiler_media.spoiler = True
file = await self.client.send_file(
self.message.chat.id,
file=spoiler_media,
caption=self.caption,
allow_cache=True,
force_document=False,
parse_mode="markdown",
reply_to=self.message.id,
supports_streaming=True,
background=True,
buttons=[
[
Button.url(
"Direct Link",
url=f"https://{BOT_USERNAME}.t.me?start={self.uuid}",
),
],
[
Button.url("Channel ", url="https://t.me/RoldexVerse"),
Button.url("Group ", url="https://t.me/RoldexVerseChats"),
],
],
)
try:
if self.edit_message:
await self.edit_message.delete()
except Exception as e:
pass
except telethon.errors.rpcerrorlist.WebpageCurlFailedError:
path = Path(self.data["file_name"])
if not os.path.exists(path):
try:
download_task = asyncio.create_task(
download_file(
self.data["direct_link"],
self.data["file_name"],
self.progress_bar,
)
)
download = await asyncio.gather(download_task)
except:
await self.edit_message.edit("Failed to Download the media. trying again.")
try:
download_task = asyncio.create_task(
download_file(
self.data["link"],
self.data["file_name"],
self.progress_bar,
)
)
download = await asyncio.gather(download_task)
except:
return await self.handle_failed_download()
else:
download = [path]
if not download or not download[0] or not os.path.exists(download[0]):
return await self.handle_failed_download()
self.download = Path(download[0])
try:
with open(self.download, "rb") as out:
res = await upload_file(
self.client, out, self.progress_bar, self.data["file_name"]
)
attributes, mime_type = utils.get_attributes(
self.download,
)
file = await self.client.send_file(
self.message.chat.id,
file=res,
caption=self.caption,
background=True,
reply_to=self.message.id,
allow_cache=True,
force_document=False,
parse_mode="markdown",
supports_streaming=True,
thumb=self.thumbnail,
# attributes=attributes,
mime_type=mime_type,
buttons=[
[
Button.url(
"Direct Link",
url=f"https://{BOT_USERNAME}.t.me?start={self.uuid}",
),
],
[
Button.url("Channel ", url="https://t.me/RoldexVerse"),
Button.url(
"Group ", url="https://t.me/RoldexVerseChats"
),
],
],
)
try:
os.unlink(self.download)
except Exception:
pass
try:
os.unlink(self.data["file_name"])
except Exception:
pass
except Exception as e:
self.client.remove_event_handler(
self.stop, events.CallbackQuery(pattern=f"^stop{self.uuid}")
)
try:
os.unlink(self.download)
except Exception:
pass
try:
os.unlink(self.data["file_name"])
except Exception:
pass
return await self.handle_failed_download()
await self.save_forward_file(file, shorturl)
async def handle_failed_download(self):
try:
os.unlink(self.data["file_name"])
except Exception:
pass
try:
os.unlink(self.download)
except Exception:
pass
try:
await self.edit_message.edit(
f"Sorry! Download Failed but you can download it from [here]({self.data['direct_link']}) or [here]({self.data['link']}).",
parse_mode="markdown",
buttons=[Button.url("Download", data=self.data["direct_link"])],
)
except Exception:
pass
async def save_forward_file(self, file, shorturl):
forwarded_message = await self.client.forward_messages(
PRIVATE_CHAT_ID,
[file],
from_peer=self.message.chat.id,
with_my_score=True,
background=True,
)
if forwarded_message[0].id:
db.set_key(self.uuid, forwarded_message[0].id)
db.set_key(f"mid_{forwarded_message[0].id}", self.uuid)
db.set_key(shorturl, forwarded_message[0].id)
self.client.remove_event_handler(
self.stop, events.CallbackQuery(pattern=f"^stop{self.uuid}")
)
try:
await self.edit_message.delete()
except Exception:
pass
try:
os.unlink(self.data["file_name"])
except Exception:
pass
try:
os.unlink(self.download)
except Exception:
pass
db.set(self.message.sender_id, time.monotonic(), ex=60)
# await self.forward_file(
# self.client, forwarded_message[0].id, self.message, self.edit_message
# )
async def send_video(self):
self.thumbnail = download_image_to_bytesio(self.data["thumb"], "thumbnail.png")
shorturl = extract_code_from_url(self.url)
if not shorturl:
return await self.edit_message.edit("Seems like your link is invalid.")
try:
if self.edit_message:
await self.edit_message.delete()
except Exception as e:
pass
db.set(self.message.sender_id, time.monotonic(), ex=60)
self.edit_message = await self.message.reply(
self.caption2, file=self.thumbnail, parse_mode="markdown"
)
self.task = asyncio.create_task(self.send_media(shorturl))
async def stop(self, event):
self.task.cancel()
self.client.remove_event_handler(
self.stop, events.CallbackQuery(pattern=f"^stop{self.uuid}")
)
await event.answer("Process stopped.")
try:
os.unlink(self.data["file_name"])
except Exception:
pass
try:
os.unlink(self.download)
except Exception:
pass
try:
await self.edit_message.delete()
except Exception:
pass
def get_thumbnail(self):
return download_image_to_bytesio(self.data["thumb"], "thumbnail.png")
@staticmethod
async def forward_file(
client: TelegramClient,
file_id: int,
message: Message,
edit_message: UpdateEditMessage = None,
uid: str = None,
):
if edit_message:
try:
await edit_message.delete()
except Exception:
pass
result = await client(
GetMessagesRequest(channel=PRIVATE_CHAT_ID, id=[int(file_id)])
)
msg: Message = result.messages[0] if result and result.messages else None
if not msg:
return False
media: Document = (
msg.media.document if hasattr(msg, "media") and msg.media.document else None
)
try:
await message.reply(
message=msg.message,
file=media,
# entity=msg.entities,
background=True,
reply_to=message.id,
force_document=False,
buttons=[
[
Button.url(
"Direct Link",
url=f"https://{BOT_USERNAME}.t.me?start={uid}",
),
],
# [
# Button.url("Channel ", url="https://t.me/RoldexVerse"),
# Button.url("Group ", url="https://t.me/RoldexVerseChats"),
# ],
],
parse_mode="markdown",
)
db.set(message.sender_id, time.monotonic(), ex=60)
db.incr(
f"check_{message.sender_id}",
1,
)
return True
except Exception:
return False