-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_missing_media_list.py
More file actions
132 lines (107 loc) · 5.06 KB
/
generate_missing_media_list.py
File metadata and controls
132 lines (107 loc) · 5.06 KB
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
#!/usr/bin/env python3
import os
import argparse
import glob
from email_utils import send_missing_media_email, create_email_config_hardcoded
def check_for_updates_if_enabled():
"""Check for updates in a non-intrusive way"""
try:
from update_checker import UpdateChecker
from version import __version__
checker = UpdateChecker()
update_info = checker.check_for_updates()
if not 'error' in update_info and update_info.get('update_available', False):
print(f"📢 Media Manager update available: v{update_info['current_version']} → v{update_info['latest_version']}")
print(f" View release: {update_info['release_url']}")
print()
except ImportError:
# Update checker not available, skip silently
pass
except Exception:
# Any error checking updates should be silent to not interfere with main operation
pass
def load_expected_titles(expected_titles_file):
"""
Loads the expected media titles from a file.
Args:
expected_titles_file (str): Path to the file containing expected media titles.
Returns:
set of str: A set containing the expected media titles.
"""
with open(expected_titles_file, 'r', encoding='utf-8') as f:
return set(f.read().splitlines())
def find_two_most_recent_media_lists(directory, pattern):
"""
Finds the two most recent media list files in the specified directory matching the given pattern.
Args:
directory (str): The directory to search in.
pattern (str): The pattern to match filenames.
Returns:
tuple of str: The paths to the most recent and the second most recent media list files.
"""
files = sorted(glob.glob(os.path.join(directory, pattern)), key=os.path.getmtime, reverse=True)
if len(files) < 2:
return None, None
return files[0], files[1] # Return the most recent and the second most recent
def generate_missing_media_list(media_list_dir, output_file):
"""
Compares the two most recent media lists to identify titles present in the second most recent list but missing in the most recent list.
Args:
media_list_dir (str): Directory containing the media list files.
output_file (str): Path to the output file where the list of missing media titles will be saved.
"""
most_recent_file, second_most_recent_file = find_two_most_recent_media_lists(media_list_dir, 'media_list_*.txt')
# Check if we have enough files to compare
if not most_recent_file or not second_most_recent_file:
error_msg = f"Error: Need at least 2 media list files in {media_list_dir} to compare"
print(error_msg)
print("Please run generate_media_list.py at least twice to create comparison files")
return False
# Load titles from both files
def load_titles_from_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
return set(f.read().splitlines())
except Exception as e:
print(f"Error reading {file_path}: {e}")
return set()
most_recent_titles = load_titles_from_file(most_recent_file)
second_most_recent_titles = load_titles_from_file(second_most_recent_file)
if not most_recent_titles and not second_most_recent_titles:
print("Error: Could not load any media lists")
return False
# Find missing titles
missing_titles = second_most_recent_titles - most_recent_titles
if missing_titles:
try:
# Ensure output directory exists
output_dir = os.path.dirname(output_file)
if output_dir:
os.makedirs(output_dir, exist_ok=True)
with open(output_file, 'w', encoding='utf-8') as f:
for title in sorted(missing_titles):
f.write(f"{title}\n")
print(f"Missing titles written to {output_file}")
# Attempt to send email notification
print("Attempting to send email notification...")
email_sent = send_missing_media_email(missing_titles)
if not email_sent:
print("Note: Email notification failed. Check your email configuration in email_utils.py")
except Exception as e:
print(f"Error writing output file {output_file}: {e}")
return False
else:
print("No titles are missing.")
return True
def main():
parser = argparse.ArgumentParser(description="Generate a list of missing media titles.")
parser.add_argument('-m', '--media-list-dir', help='Directory containing the media list files.', required=True)
parser.add_argument('-o', '--output', help='Output file to write the list of missing media titles.', required=True)
parser.add_argument('--no-update-check', action='store_true', help='Skip checking for updates')
args = parser.parse_args()
# Check for updates unless disabled
if not args.no_update_check:
check_for_updates_if_enabled()
generate_missing_media_list(args.media_list_dir, args.output)
if __name__ == "__main__":
main()