-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRDmagnet.py
188 lines (172 loc) · 7.68 KB
/
RDmagnet.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
import requests
import time
import os
import aria2p
databaseinfo = os.getenv('dbinfo')
import sqlite3
connection = sqlite3.connect(databaseinfo, timeout=20)
cursor = connection.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS tasks (id TEXT, filename TEXT, rdstatus TEXT, rdprogressdownload INTEGER, attemptstogetlink INTEGER, rderror TEXT , completed TEXT , Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP )")
cursor.execute("SELECT * FROM settings where id=1")
result = cursor.fetchall();
waitbetween=(result[0][1])
maxattempts=(result[0][2])
hostaria=(result[0][3])
secretkey=(result[0][4])
rdapikey=(result[0][5])
def moveprocessed(pathname,error):
pathname=originalmagnet
head, tail = os.path.split(pathname)
if error == 0 :
newlocaton = (head + "/processed/" + tail)
os.rename(pathname, newlocaton)
else:
newlocaton = (head + "/errored/" + tail + ".MAGNET")
os.rename(pathname, newlocaton)
def realdebridtorrent(magnet):
global completed
addmagneturl = ("https://api.real-debrid.com/rest/1.0/torrents/addMagnet?auth_token="+ rdapikey)
magnetaddjson = {"magnet": magnet}
response = requests.post(addmagneturl, data=magnetaddjson)
responsefromrd = (response.json())
myid = responsefromrd['id']
head, tail = os.path.split(magnet)
filename = tail
rdstatus="Submitted to RD"
attemptstogetlink=0
rderror=" "
completedtask="No"
cursor.execute('''INSERT INTO tasks(id, filename, rdstatus, attemptstogetlink, rderror,completed) VALUES (?,?,?,?,?,?)''',(myid,filename,rdstatus,attemptstogetlink,rderror,completedtask))
connection.commit()
time.sleep(2)
selectfiles = ("https://api.real-debrid.com/rest/1.0/torrents/selectFiles/" + myid + "?auth_token=" + rdapikey)
allfiles = {"files": "all"}
response = requests.post(selectfiles, data=allfiles)
selecttorrentinfo = ("https://api.real-debrid.com/rest/1.0/torrents/info/" + myid + "?auth_token=" + rdapikey)
response = requests.get(selecttorrentinfo)
responsefromrd = (response.json())
status=(responsefromrd["status"])
if status == "downloaded":
completed = 1
elif status == "queued":
completed =0
elif status == "magnet_error":
rdstatus = "Magnet Error"
cursor.execute(
'''INSERT INTO tasks(id, filename, rdstatus, rdprogressdownload, attemptstogetlink, rderror,completed) VALUES (?,?,?,?,?,?,?)''',
(myid, filename, rdstatus, rdprogressdownload, attemptstogetlink, rderror, completedtask))
connection.commit()
completed = 1
global error
error=1
moveprocessed(magnet,error)
elif status == "error":
rdstatus = "General Error"
cursor.execute(
'''INSERT INTO tasks(id, filename, rdstatus, rdprogressdownload, attemptstogetlink, rderror,completed) VALUES (?,?,?,?,?,?,?)''',
(myid, filename, rdstatus, rdprogressdownload, attemptstogetlink, rderror, completedtask))
connection.commit()
completed = 1
error=1
moveprocessed(magnet,error)
elif status == "magnet_conversion":
rdstatus = "Stuck Magnet Conversion"
cursor.execute(
'''INSERT INTO tasks(id, filename, rdstatus, rdprogressdownload, attemptstogetlink, rderror,completed) VALUES (?,?,?,?,?,?,?)''',
(myid, filename, rdstatus, rdprogressdownload, attemptstogetlink, rderror, completedtask))
connection.commit()
completed = 1
error=1
moveprocessed(magnet,error)
elif status == "virus":
rdstatus = "File is Virus"
cursor.execute(
'''INSERT INTO tasks(id, filename, rdstatus, rdprogressdownload, attemptstogetlink, rderror,completed) VALUES (?,?,?,?,?,?,?)''',
(myid, filename, rdstatus, rdprogressdownload, attemptstogetlink, rderror, completedtask))
connection.commit()
completed = 1
error=1
moveprocessed(magnet,error)
elif status == "dead":
rdstatus = "Link is Dead"
cursor.execute(
'''INSERT INTO tasks(id, filename, rdstatus, rdprogressdownload, attemptstogetlink, rderror,completed) VALUES (?,?,?,?,?,?,?)''',
(myid, filename, rdstatus, rdprogressdownload, attemptstogetlink, rderror, completedtask))
connection.commit()
completed = 1
error=1
moveprocessed(magnet,error)
else:
completed = 0
while completed == 0:
selecttorrentinfo = ("https://api.real-debrid.com/rest/1.0/torrents/info/" + myid + "?auth_token=" +rdapikey)
response = requests.get(selecttorrentinfo)
responsefromrd = (response.json())
if responsefromrd['status'] == "downloaded":
completed = 1
else:
completed = 0
rdstatus="Downloading"
attemptstogetlink=attemptstogetlink+1
rdprogressdownload=responsefromrd['progress']
completedtask="No"
rderror="No error"
cursor.execute(
'''INSERT INTO tasks(id, filename, rdstatus, rdprogressdownload, attemptstogetlink, rderror,completed) VALUES (?,?,?,?,?,?,?)''',
(myid, filename, rdstatus, rdprogressdownload,attemptstogetlink, rderror, completedtask))
connection.commit()
if attemptstogetlink >= maxattempts:
completed=2
else:
time.sleep(waitbetween)
if completed==1:
rdstatus="Downloaded to RD"
attemptstogetlink=attemptstogetlink
rderror = "none"
completedtask="Yes"
rdprogressdownload=100
cursor.execute(
'''INSERT INTO tasks(id, filename, rdstatus, attemptstogetlink, rderror,rdprogressdownload,completed) VALUES (?,?,?,?,?,?,?)''',
(myid, filename, rdstatus, attemptstogetlink, rderror,rdprogressdownload, completedtask))
connection.commit()
aria2 = aria2p.API(aria2p.Client(host=hostaria,port=6800,secret=secretkey))
error = 0
links = (responsefromrd['links'])
for i in range(len(links)):
getdownloadlinkurl = ("https://api.real-debrid.com/rest/1.0/unrestrict/link?auth_token="+rdapikey)
filetoselect = {"link": links[i]}
response = requests.post(getdownloadlinkurl, data=filetoselect)
responsefromrd = (response.json())
mylink = responsefromrd['download']
try:
download = aria2.add(mylink)
except:
print("Error Connecting To Your ARIA2 Instance")
moveprocessed(magnet, error)
time.sleep(1)
rdstatus="Sent to aria2"
cursor.execute(
'''INSERT INTO tasks(id, filename, rdstatus, attemptstogetlink, rderror,rdprogressdownload,completed) VALUES (?,?,?,?,?,?,?)''',
(myid, filename, rdstatus, attemptstogetlink, rderror,rdprogressdownload, completedtask))
connection.commit()
elif completed == 2:
time.sleep(1)
deletetorrentfromrd = ("https://api.real-debrid.com/rest/1.0/torrents/delete/" + myid + "?auth_token="+rdapikey)
response = requests.delete(deletetorrentfromrd)
error=1
rdstatus="Max Timeout Reached"
attemptstogetlink=attemptstogetlink
rderror="Max Time"
completedtask="No"
cursor.execute(
'''INSERT INTO tasks(id, filename, rdstatus, attemptstogetlink, rderror,rdprogressdownload,completed) VALUES (?,?,?,?,?,?,?)''',
(myid, filename, rdstatus, attemptstogetlink, rderror,rdprogressdownload, completedtask))
connection.commit()
moveprocessed(magnet, error)
import sys
originalmagnet=sys.argv[1]
print("Starting Script on Received Magnet")
with open(originalmagnet, "r") as f:
magnetlink = f.readlines()
magnetlink = (magnetlink[0])
realdebridtorrent(magnetlink)