-
Notifications
You must be signed in to change notification settings - Fork 3
/
RDRSS.py
357 lines (267 loc) · 9.17 KB
/
RDRSS.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
#!/usr/bin/python3
# Python script for feeding magnet links from RSS feed into Real-Debrid
import json
import requests
import feedparser
import argparse
import datetime
import os
# SECTION: VARIABLES
__location__ = os.path.realpath(os.path.join(
os.getcwd(), os.path.dirname(__file__)))
# Save file information
save_file_name = "RDRSSconfig/rdrss.json"
save_file_path = os.path.join(__location__, save_file_name)
BASE_DATE_STRING = "2000-01-01 00:00:00"
# Variables loaded from file
_auth_token = ""
_data = {}
_headers = {"Authorization": "Bearer " + _auth_token}
# !SECTION
# SECTION: METHODS
def load_data(initialize_if_not: bool) -> bool:
"""Load data from config file into data variable
@param initialize_if_not Create empty boilerplate data if file didnt exist
@return bool File does exist
"""
global _data
try:
json_file = open(save_file_path, "r+", encoding="utf-8")
_data = json.load(json_file)
json_file.close()
return True
except Exception:
if initialize_if_not:
_data["rssUrls"] = []
_data["updated"] = BASE_DATE_STRING
_data["authToken"] = ""
return False
def store_data() -> bool:
"""Store data to config file from data variable
@return bool Storing was successful
"""
try:
os.makedirs(os.path.dirname(save_file_path), exist_ok=True)
json_file = open(save_file_path, "w", encoding="utf-8")
json.dump(_data, json_file, indent=4)
json_file.close()
return True
except Exception:
return False
def ready_and_parse():
"""Try to parse RSS urls to Real-Debrid """
global _data
# Check for token
if not (token_check()):
return
# Load stored last updated time
if not load_data(True):
return
try:
last_updated_date = datetime.datetime.strptime(
str(_data["updated"]), '%Y-%m-%d %H:%M:%S').timetuple()
except Exception:
last_updated_date = datetime.datetime.strptime(
str(BASE_DATE_STRING), '%Y-%m-%d %H:%M:%S').timetuple()
# Load stored urls
urls = get_rss()
if len(urls) < 1:
print("Missing RSS url. To add RSS url, use --add <value>")
return
# For each url print info and fetch to Real-Debrid
x = 0
for rss in urls:
x += 1
print("(" + str(x) + "/" + str(len(urls)) + ") " + rss)
parse_feed(rss, last_updated_date)
# Store now as last update time
_data["updated"] = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
store_data()
# Select files in Real-Debrid
select_files()
def parse_feed(rss_url, last_load_date):
"""Parse RSS feed into Real-Debrid
@param rss_url RSS feed url
@param last_load_date Last date this feed was updated (when to fetch new entries from)
"""
feed = feedparser.parse(rss_url)
# If feed is empty return
if len(feed.entries) == 0:
print("-> Fetch from RSS failed. (RSS had no entries)")
return
# Try to add magnet from each entry that has not yet been added to Real-Debrid
# based on update time
for entry in feed.entries:
if (entry.updated_parsed > last_load_date):
add_magnet(entry.link)
print("-> Successfully fetched RSS to RD.")
def process_api_response(result, indent = 1) -> bool:
"""Process response codes from Real-Debrid api
@param result Recieved result
@param indent Requested indentation size
@returns bool Response is ok
"""
if not result.ok:
# Process error message indentation
indent_string = ""
for x in range(indent):
indent_string += "-"
if indent > 0:
indent_string += "> "
if result.status_code == 401:
print(indent_string +
"Failed reaching RD: Invalid token, to enter authentication token, use --token <value>.")
elif result.status_code == 402:
print(indent_string + "Failed reaching RD: User not premium.")
elif result.status_code == 503:
print(indent_string + "Failed reaching RD: Service not available.")
else:
print(indent_string + "Failed reaching RD.")
return False
return True
def add_magnet(magnet) -> bool:
"""Add magnet url into Real-Debrid using API
@param magnet Url to magnet
@returns bool Magnet added successfully
"""
print("--> Adding magnet: " + magnet)
# Add magnet to Real-Debrid and process response
request_data = {"magnet": magnet, "host": "real-debrid.com"}
result = requests.post(
"https://api.real-debrid.com/rest/1.0/torrents/addMagnet", headers=_headers, data=request_data)
if not process_api_response(result, 3):
return False
return True
def select_files() -> bool:
"""Select files added into Real-Debrid using API
@returns bool Files selected successfully
"""
# Get files from Real-Debrid
result = requests.get(
"https://api.real-debrid.com/rest/1.0/torrents?limit=100", headers=_headers)
if not process_api_response(result):
print("-> Selecting files on RD failed.")
return False
# Select correct files
files = result.json()
for file in files:
if file["status"] == "waiting_files_selection":
result = requests.post("https://api.real-debrid.com/rest/1.0/torrents/selectFiles/" +
file["id"], data={"files": "all"}, headers=_headers)
if not process_api_response(result):
print("--> File could not be selected.")
return False
print("-> Successfully selected files on RD.")
return True
def get_rss():
""" Retrieve stored RSS urls
@return array of urls
"""
if load_data(True):
if ("rssUrls" in _data) and (len(_data["rssUrls"]) != 0):
return _data["rssUrls"]
return []
def set_token(token):
"""Store Real-Debrid token
@param token Real-Debrid user token
"""
global _data
# Load data and store token
load_data(True)
_data["authToken"] = token
# Store data
if not store_data():
print("Couldn't store token.")
return
print("Token succesfully added.")
def token_check() -> bool:
"""Check if Real-Debrid token is stored
@returns bool If true, token is stored
"""
global _auth_token
global _headers
# Check if token is in loaded data
if load_data(True):
if len(_data["authToken"]) != 0:
_auth_token = _data["authToken"]
_headers = {"Authorization": "Bearer " + _auth_token}
return True
print(
"Missing Real-Debrid authentication token. To enter auth token, use --token <value>")
return False
def add_rss(rss):
"""Store RSS url
param rss Url to RSS feed
"""
global _data
# Load data and add new rss
load_data(True)
_data["rssUrls"].append(rss)
# Store data
if not store_data():
print("Couldn't store RSS url.")
return
print("RSS url succesfully added.")
def list_rss():
"""List stored RSS urls"""
if load_data(True):
if ("rssUrls" in _data) and (len(_data["rssUrls"]) != 0):
print("RSS urls stored:")
# Loop through urls and print them numbered
x = 0
for rss in _data["rssUrls"]:
x += 1
print(" [" + str(x) + "] " + rss)
if (x > 0):
return
print("No RSS url added. To add RSS url, use --add <value>")
def remove_rss(n):
"""Remove stored RSS url number n
@param n Index of url to remove
"""
global _data
if not load_data(True):
print("Configuration file is empty.")
# Check if url at index exists
if ("rssUrls" not in _data) or (len(_data["rssUrls"]) < n):
print("No url at index " + str(n) + " found.")
return
# Remove url from data
_data["rssUrls"].pop(n-1)
# Store data back into file
if not store_data():
print("Couldn't remove RSS url.")
return
print("RSS url succesfully removed.")
# !SECTION
# SECTION: ARGUMENT PROCESSING
parser = argparse.ArgumentParser(description='RSS feed to Real-Debrid.')
parser.add_argument('-t', '--token', type=str,
help='set Real-Debrid token (acquire token at https://real-debrid.com/apitoken)')
parser.add_argument('-l', '--list',
help='list RSS urls', action='store_true')
parser.add_argument('-a', '--add', type=str, help='add RSS url')
parser.add_argument('-r', '--remove', type=int,
help='remove RSS url at index (obtained using --list)')
parser.add_argument('-m', '--magnet', type=str,
help='add magnet to Real-Debrid')
parser.add_argument('-s', '--select',
help='select added files on Real-Debrid', action='store_true')
args = parser.parse_args()
if args.token:
set_token(args.token)
elif args.list:
list_rss()
elif args.add:
add_rss(args.add)
elif args.remove:
remove_rss(args.remove)
elif args.magnet:
if token_check():
add_magnet(args.magnet)
elif args.select:
if token_check():
select_files()
else:
ready_and_parse()
# !SECTION