forked from Sachin79/tx
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
266 lines (226 loc) · 7.85 KB
/
main.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
from telethon import events, Button
from config import bot
from FastTelethonhelper import fast_upload
import os
import subprocess
import helper
from telethon.tl.types import DocumentAttributeVideo
cancel = False
@bot.on(events.NewMessage(pattern="/start"))
async def _(event):
await event.reply("Hello!")
@bot.on(events.NewMessage(pattern="/sthumb"))
async def _(event):
x = await event.get_reply_message()
thumb = await bot.download_media(x.photo)
with open(thumb, "rb") as f:
pic = f.read()
with open("thumb.png", "wb") as f:
f.write(pic)
await event.reply("Set as default thumbnail")
@bot.on(events.NewMessage(pattern=("/cthumb")))
async def _(event):
with open("thumb.png", "w") as f:
f.write("")
os.remove("thumb.png")
await event.reply("cleared thumbnail")
@bot.on(events.NewMessage(pattern=("/vthumb")))
async def _(event):
try:
await event.reply("current default thumbnail", file="thumb.png")
except:
await event.reply("No default thumbnail set")
@bot.on(events.NewMessage(pattern="/cancel"))
async def _(event):
global cancel
cancel = True
await event.reply("Trying to cancel all processes.")
return
@bot.on(events.NewMessage(pattern="/download"))
async def _(event):
global cancel
cancel = False
editable = await event.reply("**Send txt file**")
x =await bot.download_media()
await input.delete(True)
path = f"./downloads/"
try:
with open(x, "r") as f:
content = f.read()
content = content.split("\n")
links = []
for i in content:
links.append(i.split(":", 1))
os.remove(x)
# print(len(links))
except:
await event.reply("Invalid file input.")
os.remove(x)
return
editable = await event.reply_text(f"Total links found are **{len(links)}**\n\nSend From where you want to download initial is **0**")
#input1: Message = await bot.listen(editable.chat.id)
#raw_text = input1.text
try:
arg = int(raw_text)
except:
arg = 0
for i in range(arg, len(links)):
try:
if cancel == True:
await event.reply("Process canceled")
return
url = links[i][1]
name = links[i][0].replace("\t", "")
filename = f"{name[:60]}.mp4"
r = await event.reply(f"`Downloading...\n{name[:60]}\n\nfile number: {i+1}`")
caption = f"`{name[:60]}\n\nfile number: {i+1}`"
k = await helper.download_video(url, filename)
filename = k
res_file = await fast_upload(bot, filename, r)
if not os.path.isfile("thumb.png"):
subprocess.call(f'ffmpeg -i "{filename}" -ss 00:00:01 -vframes 1 "{filename}.jpg"', shell=True)
thumbnail = f"{filename}.jpg"
else:
thumbnail = "thumb.png"
dur = int(helper.duration(filename))
try:
await bot.send_message(
event.chat_id,
caption,
file=res_file,
force_document=False,
thumb=thumbnail,
supports_streaming=True,
attributes=[DocumentAttributeVideo(
duration=dur,
w=1260,
h=720,
supports_streaming=True
)]
)
except:
await bot.send_message(
event.chat_id,
"There was an error while uploading file as streamable so, now trying to upload as document."
)
await bot.send_message(
event.chat_id,
caption,
file=res_file,
force_document=True,
)
os.remove(filename)
os.remove(f"{filename}.jpg")
await r.delete()
except Exception as e:
print(e)
pass
@bot.on(events.NewMessage(pattern="/upload"))
async def _(event):
arg = event.raw_text.split(" ", maxsplit = 1)[1]
arg = arg.split("|")
if len(arg) == 1:
file_name = arg[0].split("/")[-1]
caption = ''
elif len(arg) == 2:
file_name = arg[1].strip()
caption = arg[1].strip()
else:
file_name = arg[1].strip()
caption = arg[2].strip()
cmd = f'yt-dlp -F "{arg[0]}"'
k = await helper.run(cmd)
out = helper.parse_vid_info(str(k))
print(out)
buttons = []
if 'unknown' in out[0][1]:
r = await event.reply("downloading.")
f = helper.old_download(arg[0], file_name)
res_file = await fast_upload(bot, f, r)
await bot.send_message(
event.chat_id,
f"`{caption}`",
file=res_file,
force_document=True,
)
return
for i in out:
if 'youtu' in arg[0]:
print(i[1])
x = i[1].split()[0].split("x")[-1]
buttons.append([Button.inline(i[1], data=f"id:bestvideo[height<={x}][ext=mp4]")])
else:
buttons.append([Button.inline(i[1], data=f"id:{i[0]}")])
await bot.send_message(event.chat_id, f"`Name: {file_name}`\n`Caption: {caption}`\n`Url: {arg[0]}`", buttons=buttons)
@bot.on(events.NewMessage(pattern="/txt"))
async def _(event):
try:
x = await event.get_reply_message()
json_file = await bot.download_media(x)
res, count = helper.parse_json_to_txt(json_file)
await event.reply(f"{count} links detected." ,file=res)
os.remove(json_file)
os.remove(res)
except Exception as e:
print(e)
await event.reply("Invalid Json file input.")
@bot.on(events.NewMessage(pattern="/html"))
async def _(event):
try:
x = await event.get_reply_message()
json_file = await bot.download_media(x)
res, count = helper.parse_json_to_html(json_file)
await event.reply(f"{count} links detected." ,file=res)
os.remove(json_file)
os.remove(res)
except Exception:
await event.reply("Invalid Json file input.")
@bot.on(events.CallbackQuery(pattern=b"id:"))
async def _(event):
r = await event.reply("Trying to download....")
data = event.data.decode('utf-8')
data = data.split(":")
msg = await bot.get_messages(event.chat_id, ids=event.message_id)
await msg.edit(buttons=None)
msg = msg.raw_text.split("\n")
file_name = msg[0].replace("Name: ", "")
caption = msg[1].replace("Caption: ", "")
url = msg[2].replace("Url: ", "")
vid_id = (data[1])
filename = await helper.download_video(url, file_name, vid_id)
res_file = await fast_upload(bot, filename, r)
if not os.path.isfile("thumb.png"):
subprocess.call(f'ffmpeg -i "{filename}" -ss 00:00:01 -vframes 1 "{filename}.jpg"', shell=True)
thumbnail = f"{filename}.jpg"
else:
thumbnail = "thumb.png"
try:
dur = int(helper.duration(filename))
await bot.send_message(
event.chat_id,
f"{caption}",
file=res_file,
force_document=False,
thumb=thumbnail,
supports_streaming=True,
attributes=[DocumentAttributeVideo(
duration=dur,
w=1260,
h=720,
supports_streaming=True
)]
)
except:
await bot.send_message(
"There was an error while uploading file as streamable so, now trying to upload as document."
)
await bot.send_message(
f"`{caption}`",
file=res_file,
force_document=True,
)
os.remove(filename)
os.remove(f"{filename}.jpg")
await r.delete()
bot.start()
bot.run_until_disconnected()