-
Notifications
You must be signed in to change notification settings - Fork 30
/
fastdownload.py
132 lines (118 loc) · 4.31 KB
/
fastdownload.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
"""
command: .url
"""
import asyncio
import logging
import os
import aria2p
from userbot import bot
from userbot.util import admin_cmd
logging.basicConfig(
format="[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s", level=logging.WARNING
)
logger = logging.getLogger(__name__)
cmd = "aria2c --enable-rpc --rpc-listen-all=false --rpc-listen-port 6800 --max-connection-per-server=10 --rpc-max-request-size=1024M --seed-time=0.01 --min-split-size=10M --follow-torrent=mem --split=10 --daemon=true"
aria2_is_running = os.system(cmd)
aria2 = aria2p.API(aria2p.Client(host="http://localhost", port=6800, secret=""))
EDIT_SLEEP_TIME_OUT = 10
@bot.on(admin_cmd(pattern="ariaurl ?(.*)"))
async def magnet_download(event):
if event.fwd_from:
return
var = event.pattern_match.group(1)
print(var)
uris = [var]
# Add URL Into Queue
try:
download = aria2.add_uris(uris, options=None, position=None)
except Exception as e:
# await event.edit("`Error:\n`"+str(e))
return
gid = download.gid
complete = None
await progress_status(gid=gid, event=event, previous=None)
file = aria2.get_download(gid)
if file.followed_by_ids:
new_gid = await check_metadata(gid)
await progress_status(gid=new_gid, event=event, previous=None)
while complete is not True:
file = aria2.get_download(gid)
complete = file.is_complete
try:
msg = (
"**Downloading File:** "
+ str(file.name)
+ "\n**Speed:** "
+ str(file.download_speed_string())
+ "\n**Progress:** "
+ str(file.progress_string())
+ "\n**Total Size:** "
+ str(file.total_length_string())
+ "\n**ETA:** "
+ str(file.eta_string())
+ "\n\n"
)
await event.edit(msg)
await asyncio.sleep(10)
except Exception as e:
pass
await event.edit("**File Downloaded Successfully:** `{}`".format(file.name))
async def progress_status(gid, event, previous):
try:
file = aria2.get_download(gid)
if not file.is_complete:
if not file.error_message:
msg = (
(
(
(
(
f"Downloading File: `{str(file.name)}"
+ "`\nSpeed: "
+ str(file.download_speed_string())
+ "\nProgress: "
+ str(file.progress_string())
)
+ "\nTotal Size: "
)
+ str(file.total_length_string())
+ "\nStatus: "
)
+ str(file.status)
+ "\nETA: "
)
+ str(file.eta_string())
+ "\n\n"
)
if previous != msg:
await event.edit(msg)
previous = msg
else:
logger.info(str(file.error_message))
await event.edit("Error : `{}`".format(str(file.error_message)))
return
await asyncio.sleep(EDIT_SLEEP_TIME_OUT)
await progress_status(gid, event, previous)
else:
await event.edit("File Downloaded Successfully: `{}`".format(file.name))
return
except Exception as e:
if " not found" in str(e) or "'file'" in str(e):
await event.edit("Download Canceled :\n`{}`".format(file.name))
return
if " depth exceeded" in str(e):
file.remove(force=True)
await event.edit(
"Download Auto Canceled :\n`{}`\nYour Torrent/Link is Dead.".format(
file.name
)
)
else:
logger.info(str(e))
await event.edit("Error :\n`{}`".format(str(e)))
return
async def check_metadata(gid):
file = aria2.get_download(gid)
new_gid = file.followed_by_ids[0]
logger.info(f"Changing GID {gid} to {new_gid}")
return new_gid