-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmanual_blacklist.py
executable file
·96 lines (81 loc) · 3.3 KB
/
manual_blacklist.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
import json
import os
import logging
from typing import Dict
# Get db_content directory from environment variable with fallback
DB_CONTENT_DIR = os.environ.get('USER_DB_CONTENT', '/user/db_content')
# Update the path to use the environment variable
BLACKLIST_FILE = os.path.join(DB_CONTENT_DIR, 'manual_blacklist.json')
def load_manual_blacklist():
os.makedirs(os.path.dirname(BLACKLIST_FILE), exist_ok=True)
if not os.path.exists(BLACKLIST_FILE):
return {}
try:
with open(BLACKLIST_FILE, 'r') as f:
return json.load(f)
except json.JSONDecodeError:
logging.error(f"Error decoding {BLACKLIST_FILE}. Starting with empty blacklist.")
return {}
def save_manual_blacklist(blacklist):
with open(BLACKLIST_FILE, 'w') as f:
json.dump(blacklist, f)
def add_to_manual_blacklist(imdb_id: str, media_type: str, title: str, year: str, season: int = None):
blacklist = get_manual_blacklist()
if season is not None and media_type == 'tv':
# If this is the first season for this show
if imdb_id not in blacklist:
blacklist[imdb_id] = {
'media_type': 'tv',
'title': title,
'year': year,
'seasons': [season]
}
else:
# Add season if not already blacklisted
if 'seasons' not in blacklist[imdb_id]:
blacklist[imdb_id]['seasons'] = []
if season not in blacklist[imdb_id]['seasons']:
blacklist[imdb_id]['seasons'].append(season)
blacklist[imdb_id]['seasons'].sort()
else:
# Regular blacklisting for movies or entire shows
blacklist[imdb_id] = {
'media_type': media_type,
'title': title,
'year': year
}
if media_type == 'tv':
blacklist[imdb_id]['seasons'] = [] # Empty list means all seasons
save_manual_blacklist(blacklist)
if season is not None:
logging.info(f"Added {imdb_id}: {title} ({year}) Season {season} to manual blacklist")
else:
logging.info(f"Added {imdb_id}: {title} ({year}) to manual blacklist as {media_type}")
def remove_from_manual_blacklist(imdb_id):
blacklist = get_manual_blacklist()
if imdb_id in blacklist:
item = blacklist.pop(imdb_id)
save_manual_blacklist(blacklist)
logging.info(f"Removed {imdb_id}: {item['title']} ({item['year']}) from manual blacklist.")
else:
logging.warning(f"{imdb_id} not found in manual blacklist.")
def is_blacklisted(imdb_id, season: int = None):
blacklist = get_manual_blacklist()
if imdb_id not in blacklist:
return False
item = blacklist[imdb_id]
if item['media_type'] != 'tv':
return True
# If seasons is empty or doesn't exist, the entire show is blacklisted
if 'seasons' not in item or not item['seasons']:
return True
# If season is None, we're checking the whole show
if season is None:
return False # Show isn't fully blacklisted if specific seasons are listed
return season in item['seasons']
def get_manual_blacklist() -> Dict[str, Dict[str, str]]:
try:
with open(BLACKLIST_FILE, 'r') as f:
return json.load(f)
except FileNotFoundError:
return {}