-
Notifications
You must be signed in to change notification settings - Fork 0
/
not_wanted_magnets.py
executable file
·65 lines (54 loc) · 2.22 KB
/
not_wanted_magnets.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
import pickle
def load_not_wanted_magnets():
try:
with open('db_content/not_wanted_magnets.pkl', 'rb') as f:
return pickle.load(f)
except (EOFError, pickle.UnpicklingError):
# If the file is empty or not a valid pickle object, return an empty set
return set()
except FileNotFoundError:
# If the file does not exist, create it and return an empty set
with open('db_content/not_wanted_magnets.pkl', 'wb') as f:
pickle.dump(set(), f)
return set()
def save_not_wanted_magnets(not_wanted_set):
with open('db_content/not_wanted_magnets.pkl', 'wb') as f:
pickle.dump(not_wanted_set, f)
def add_to_not_wanted(magnet):
not_wanted = load_not_wanted_magnets()
not_wanted.add(magnet)
save_not_wanted_magnets(not_wanted)
def is_magnet_not_wanted(magnet):
not_wanted = load_not_wanted_magnets()
return magnet in not_wanted
def task_purge_not_wanted_magnets_file():
# Purge the contents of the file by overwriting it with an empty set
with open('db_content/not_wanted_magnets.pkl', 'wb') as f:
pickle.dump(set(), f)
print("The 'not_wanted_magnets.pkl' file has been purged.")
# New function to get the current set of not wanted magnets
def get_not_wanted_magnets():
return load_not_wanted_magnets()
def get_not_wanted_urls():
return load_not_wanted_urls()
def add_to_not_wanted_urls(url):
not_wanted = load_not_wanted_urls()
not_wanted.add(url)
save_not_wanted_urls(not_wanted)
def is_url_not_wanted(url):
not_wanted = load_not_wanted_urls()
file_part = url.split("file=")[-1] if "file=" in url else url
return any(file_part in nw_url.split("file=")[-1] if "file=" in nw_url else nw_url for nw_url in not_wanted)
def load_not_wanted_urls():
try:
with open('db_content/not_wanted_urls.pkl', 'rb') as f:
return pickle.load(f)
except (EOFError, pickle.UnpicklingError):
return set()
except FileNotFoundError:
with open('db_content/not_wanted_urls.pkl', 'wb') as f:
pickle.dump(set(), f)
return set()
def save_not_wanted_urls(not_wanted_set):
with open('db_content/not_wanted_urls.pkl', 'wb') as f:
pickle.dump(not_wanted_set, f)