-
Notifications
You must be signed in to change notification settings - Fork 1
/
Telegram.py
196 lines (176 loc) · 6.35 KB
/
Telegram.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
import asyncio
import os
import time
from datetime import datetime, timedelta
from typing import Union
from pyrogram.types import (InlineKeyboardButton,
InlineKeyboardMarkup, Voice)
import config
from config import MUSIC_BOT_NAME, lyrical
from AnonX import app
from ..utils.formatters import (convert_bytes, get_readable_time,
seconds_to_min)
downloader = {}
class TeleAPI:
def __init__(self):
self.chars_limit = 4096
self.sleep = config.TELEGRAM_DOWNLOAD_EDIT_SLEEP
async def send_split_text(self, message, string):
n = self.chars_limit
out = [(string[i : i + n]) for i in range(0, len(string), n)]
j = 0
for x in out:
if j <= 2:
j += 1
await message.reply_text(x)
return True
async def get_link(self, message):
if message.chat.username:
link = f"https://t.me/{message.chat.username}/{message.reply_to_message.message_id}"
else:
xf = str((message.chat.id))[4:]
link = f"https://t.me/c/{xf}/{message.reply_to_message.message_id}"
return link
async def get_filename(
self, file, audio: Union[bool, str] = None
):
try:
file_name = file.file_name
if file_name is None:
file_name = (
"ᴛᴇʟᴇɢʀᴀᴍ ᴀᴜᴅɪᴏ"
if audio
else "ᴛᴇʟᴇɢʀᴀᴍ ᴠɪᴅᴇᴏ"
)
except:
file_name = (
"ᴛᴇʟᴇɢʀᴀᴍ ᴀᴜᴅɪᴏ"
if audio
else "ᴛᴇʟᴇɢʀᴀᴍ ᴠɪᴅᴇᴏ"
)
return file_name
async def get_duration(self, file):
try:
dur = seconds_to_min(file.duration)
except:
dur = "Unknown"
return dur
async def get_filepath(
self,
audio: Union[bool, str] = None,
video: Union[bool, str] = None,
):
if audio:
try:
file_name = (
audio.file_unique_id
+ "."
+ (
(audio.file_name.split(".")[-1])
if (not isinstance(audio, Voice))
else "ogg"
)
)
except:
file_name = audio.file_unique_id + "." + ".ogg"
file_name = os.path.join(
os.path.realpath("downloads"), file_name
)
if video:
try:
file_name = (
video.file_unique_id
+ "."
+ (video.file_name.split(".")[-1])
)
except:
file_name = video.file_unique_id + "." + "mp4"
file_name = os.path.join(
os.path.realpath("downloads"), file_name
)
return file_name
async def download(self, _, message, mystic, fname):
left_time = {}
speed_counter = {}
if os.path.exists(fname):
return True
async def down_load():
async def progress(current, total):
if current == total:
return
current_time = time.time()
start_time = speed_counter.get(message.message_id)
check_time = current_time - start_time
upl = InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
text="↻ ᴄᴀɴᴄᴇʟ ↺",
callback_data="stop_downloading",
),
]
]
)
if datetime.now() > left_time.get(message.message_id):
percentage = current * 100 / total
percentage = str(round(percentage, 2))
speed = current / check_time
eta = int((total - current) / speed)
downloader[message.message_id] = eta
eta = get_readable_time(eta)
if not eta:
eta = "0 sec"
total_size = convert_bytes(total)
completed_size = convert_bytes(current)
speed = convert_bytes(speed)
text = f"""
**{MUSIC_BOT_NAME} ᴍᴇᴅɪᴀ ᴅᴏᴡɴʟᴏᴀᴅᴇʀ**
**sɪᴢᴇ :** {total_size}
**ᴅᴏᴡɴʟᴏᴀᴅᴇᴅ :** {completed_size}
**ᴩᴇʀᴄᴇɴᴛᴀɢᴇ :** {percentage[:5]}%
**sᴩᴇᴇᴅ :** {speed}/s
**ᴇᴛᴀ :** {eta}"""
try:
await mystic.edit_text(text, reply_markup=upl)
except:
pass
left_time[
message.message_id
] = datetime.now() + timedelta(seconds=self.sleep)
speed_counter[message.message_id] = time.time()
left_time[message.message_id] = datetime.now()
try:
await app.download_media(
message.reply_to_message,
file_name=fname,
progress=progress,
)
await mystic.edit_text(
"**ғɪʟᴇ sᴜᴄᴄᴇssғᴜʟʟʏ ᴅᴏᴡɴʟᴏᴀᴅᴇᴅ.\n\n ᴩʀᴏᴄᴇssɪɴɢ...**"
)
downloader.pop(message.message_id)
except:
await mystic.edit_text(_["tg_2"])
if len(downloader) > 10:
timers = []
for x in downloader:
timers.append(downloader[x])
try:
low = min(timers)
eta = get_readable_time(low)
except:
eta = "Unknown"
await mystic.edit_text(_["tg_1"].format(eta))
return False
task = asyncio.create_task(down_load())
lyrical[mystic.message_id] = task
await task
downloaded = downloader.get(message.message_id)
if downloaded:
downloader.pop(message.message_id)
return False
verify = lyrical.get(mystic.message_id)
if not verify:
return False
lyrical.pop(mystic.message_id)
return True