forked from frenchcutgreenbean/UNIT3D-Upload-Checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
400 lines (382 loc) · 16.6 KB
/
settings.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import os
import json
import traceback
import requests
class Settings:
def __init__(self):
self.data_folder = "./data/"
self.default_settings = {
"directories": [],
"tmdb_key": "", # https://www.themoviedb.org/settings/api
"enabled_sites": [],
"keys": {
"aither": "",
"blutopia": "",
"fearnopeer": "",
"reelflix": "",
"lst": "",
"ulcx": "",
"onlyencodes": "",
},
"gg_path": "", # Path to GG-Bot e.g. /home/user/gg-bot-upload-assistant/ --- Not required only for export_gg_bot()
"ua_path": "", # Path to upload-assistant, e.g. /home/user/uplaad-assistant/ --- Optional
"search_cooldown": 5, # In seconds. Anything less than 3 isn't recommended. 30 requests per minute is max before hit rate limits. - HDVinnie
"min_file_size": 800, # In MB
"allow_dupes": True, # If false only check for completely unique movies
"banned_groups": [],
"ignored_qualities": [
"dvdrip",
"webrip",
"bdrip",
"cam",
"ts",
"telesync",
"hdtv",
], # See patterns.py for valid options, note "bluray" get's changed to encode in scan_directories()
"ignored_keywords": [
"10bit",
"10-bit",
"DVD",
], # This could be anything that would end up in the excess of parsed filename.
}
self.tracker_nicknames = {
"fnp": "fearnopeer",
"fearnopeer": "fearnopeer",
"reelflix": "reelflix",
"rfx": "reelflix",
"aither": "aither",
"aith": "aither",
"blu": "blutopia",
"blutopia": "blutopia",
"lst": "lst",
"lstgg": "lst",
"ulcx": "ulcx",
"upload.cx": "ulcx",
"onlyencodes": "onlyencodes",
"oe": "onlyencodes",
}
# Basic hierarchy for qualities used to see if a file is an upgrade
self.quality_hierarchy = {
"webrip": 0,
"web-dl": 1,
"encode": 2,
"remux": 3,
}
self.current_settings = None
self.tracker_info = None
try:
# Creating settings.json with default settings
if (
not os.path.exists(f"{self.data_folder}settings.json")
or os.path.getsize(f"{self.data_folder}settings.json") < 10
):
with open(f"{self.data_folder}settings.json", "w") as outfile:
json.dump(self.default_settings, outfile)
# Load settings.json
if os.path.getsize(f"{self.data_folder}settings.json") > 10:
with open(f"{self.data_folder}settings.json", "r") as file:
self.current_settings = json.load(file)
self.validate_directories()
# Set the settings to our class
if not self.current_settings:
self.current_settings = self.default_settings
# Load tracker_info.json used for resolution mapping
if not self.tracker_info:
with open("tracker_info.json", "r") as file:
self.tracker_info = json.load(file)
except Exception as e:
print("Error initializing settings: ", e)
# Clean directories from loaded settings
def validate_directories(self):
try:
directories = self.current_settings["directories"]
directories = list(set(directories))
# Remove trailing slashes for os.path.commonpath
clean = [
(
dir_path[:-1]
if dir_path[-1] == "\\" or dir_path[-1] == "/"
else dir_path
)
for dir_path in directories
]
clean_copy = clean
if len(clean) > 1:
for dir_path in clean:
# Check if the directory exists
if os.path.exists(dir_path):
drive, tail = os.path.splitdrive(dir_path)
if drive and not tail.strip("\\/"):
print(
f"{dir_path} is a root directory, removing child directories"
)
clean_copy = [
c for c in clean_copy if not c.startswith(drive[0])
]
clean_copy.append(dir_path)
continue
elif dir_path in clean_copy:
is_subpath = False
child_path = None
parent_path = None
for other_dir in clean_copy:
if (
dir_path != other_dir
and os.path.commonpath([dir_path, other_dir])
== dir_path
):
is_subpath = True
child_path = (
other_dir
if len(other_dir) > len(dir_path)
else dir_path
)
parent_path = (
other_dir
if len(other_dir) < len(dir_path)
else dir_path
)
print(
f"{child_path} is a sub-path of {parent_path}, removing"
)
else:
continue
if is_subpath and child_path in clean_copy:
clean_copy.remove(child_path)
else:
continue
else:
print(f"{dir_path} does not exist, removing")
normalized_directories = []
for c in clean_copy:
if not c.endswith(os.path.sep):
# List comp with os.path.join() wasn't working on root directory on Windows for some reason
c += os.path.sep
normalized_directories.append(c)
else:
normalized_directories.append(c)
self.current_settings["directories"] = normalized_directories
self.write_settings()
except Exception as e:
print("Error Validating Directories:", e)
print(traceback.format_exc())
# Add and validate new directories.
def add_directory(self, path):
directories = self.current_settings["directories"]
if not os.path.exists(path):
raise ValueError("Path doesn't exist")
if path not in directories:
# Add the new path to the list
directories.append(path)
self.validate_directories()
def validate_tmdb(self, key):
try:
url = f"https://api.themoviedb.org/3/configuration?api_key={key}"
response = requests.get(url)
if response.status_code != 200:
print("Invalid API Key")
return
else:
self.current_settings["tmdb_key"] = key
print("Key is valid and was added to tmdb")
except Exception as e:
print("Error searching api:", e)
return
def validate_key(self, key, target):
api_key = None
tracker = None
try:
for nn in self.tracker_nicknames:
if target == nn:
tracker = self.tracker_nicknames[nn]
break
if not tracker:
print(target, " is not a supported site")
return
try:
url = self.tracker_info[tracker]["url"]
url = f"{url}api/torrents?perPage=10&api_token={key}"
response = requests.get(url)
# UNIT3D pushes you to the homepage if the api key is invalid
if response.history:
print("Invalid API Key")
return
else:
api_key = key
self.current_settings["keys"][tracker] = api_key
self.write_settings()
print("Key is valid and was added to", tracker)
except Exception as e:
print("Error searching api:", e)
return
except Exception as e:
print("Error Validating Key:", e)
def setting_helper(self, target):
settings = self.current_settings
nicknames = self.tracker_nicknames
matching_keys = [key for key in settings.keys() if target in key]
matching_nicks = [nick for nick in nicknames.keys() if target in nick]
if len(matching_nicks) >= 1:
return False
if len(matching_keys) == 1:
return matching_keys[0]
elif len(matching_keys) > 1:
print(
"Multiple settings match the provided substring. Please provide a more specific target."
)
print(settings.keys())
print(
"Unique substrings accepted: dir, tmdb, sites, gg, search, size, dupes, banned, qual, keywords"
)
print(
"If you're trying to add a tracker key, you can use setting-add -t <site> -s <api_key>"
)
print("Accepted sites: ", nicknames.keys())
return
else:
print(target, " is not a supported setting")
print("Accepted targets: ", settings.keys())
print(
"Unique substrings accepted: dir, tmdb, sites, gg, search, size, dupes, banned, qual, keywords"
)
print(
"If you're trying to add a tracker key, you can use setting-add -t <site> -s <api_key>"
)
print("Accepted sites: ", nicknames.keys())
return
# Update a specific setting
def update_setting(self, target, value):
try:
settings = self.current_settings
nicknames = self.tracker_nicknames
matching_key = self.setting_helper(target)
if matching_key:
target = matching_key # Update target to the full key
if target == "tmdb_key":
self.validate_tmdb(value)
settings[target] = value
if isinstance(settings[target], str):
settings[target] = value
print(value, " Successfully added to ", target)
elif isinstance(settings[target], list):
if target == "directories":
self.add_directory(value)
elif target == "enabled_sites":
if value in nicknames:
tracker = nicknames[value]
if not self.current_settings["keys"].get(tracker):
print(
"There is currently no api key for",
value,
f"\nAdd one using setting-add -t {value} -s <api_key>",
)
else:
print(value, " is not a supported site")
return
if value in settings[target]: # Don't add duplicates
print(value, " Already in ", target)
return
else:
settings[target].append(tracker) # Add new site
print(tracker, "Successfully added to", target)
else:
settings[target].append(
value
) # banned_groups, ignored_qualities, ignored_keywords these shouldn't need extra validation
print(value, " Successfully added to ", target)
elif isinstance(settings[target], bool):
if "t" in value.lower():
settings[target] = True
print(target, " Set to True")
elif "f" in value.lower():
settings[target] = False
print(target, " Set to False")
else:
print(
"Value ", value, " Not recognized, try False, F or True, T"
)
elif isinstance(settings[target], int):
settings[target] = int(value)
print(value, " Successfully added to ", target)
# Add a new key
elif target in nicknames:
if not value:
print("No api key provided")
else:
self.validate_key(value, target)
self.current_settings = settings
self.write_settings()
except Exception as e:
print("Error updating setting", e)
print(traceback.format_exc())
def return_setting(self, target):
try:
matching_key = self.setting_helper(target)
matching_nick = (
self.tracker_nicknames[target]
if target in self.tracker_nicknames
else False
)
if matching_key:
target = matching_key # Update target to the full key
return self.current_settings[target]
elif matching_nick:
if self.current_settings["keys"][matching_nick]:
return self.current_settings["keys"][matching_nick]
except Exception as e:
print("Error returning settings: ", e)
def remove_setting(self, target):
try:
matching_key = self.setting_helper(target)
if matching_key:
target = matching_key # Update target to the full key
setting = self.current_settings[target]
if isinstance(setting, list):
if len(setting) > 0:
print(
"Which option would you like to remove?",
setting,
"\nType in the number of the option you want to remove:",
"\n0 being the first option, 1 being the second option, etc.",
)
option = int(input())
if option < 0 or option >= len(setting):
print("Option out of range")
return
removed_item = setting.pop(option)
# Remove trailing backslash if exists
removed_item = removed_item.rstrip("\\")
print("Removed:", removed_item)
else:
print("The setting is empty.")
else:
print("The setting is not a list.")
print(f"Use setting-add -t {target} -s <new_value>")
self.write_settings()
except Exception as e:
print("Error removing setting:", e)
def is_upgrade(self, file, tr):
if not file or not tr:
print("error comparing qualities")
return False
if (
file not in self.quality_hierarchy.keys()
or tr not in self.quality_hierarchy.keys()
):
return False
if self.quality_hierarchy[file] > self.quality_hierarchy[tr]:
return True
else:
return False
def write_settings(self):
try:
with open(f"{self.data_folder}settings.json", "w") as outfile:
json.dump(self.current_settings, outfile)
except Exception as e:
print("Error writing settings: ", e)
def reset_settings(self):
try:
with open(f"{self.data_folder}settings.json", "w") as outfile:
json.dump(self.default_settings, outfile)
except Exception as e:
print("Error resetting settings: ", e)