-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
126 lines (98 loc) · 4.25 KB
/
main.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
import asyncio
import os
import logging
import argparse
import time
import traceback
import gc
from src.coverCleaner import cover_cleaner, load_language_data, consolidate_series_folders
from src.getIDs import get_jellyfin_content
from src.detectContentChange import check_jellyfin_content
from src.logging import setup_logging
from src.updateCover import UpdateCover
from src.languageLookup import collect_titles
from src.blacklist import update_output_file
from src.constants import RAW_COVER_DIR, MEDIUX_FILE, COVER_DIR
from src.mediux_downloader import mediux_downloader
from src.webhook import WebhookServer
from src.config import ENABLE_WEBHOOK
from src.rematchNoMatchFolder import FolderMatcher
from src.cleanupEmptyFolder import cleanup_empty_folders
logger = logging.getLogger(__name__)
async def main_loop(force: bool, webhook_server: WebhookServer):
updater = UpdateCover()
folder_matcher = None
while True:
try:
RAW_COVER_DIR.mkdir(parents=True, exist_ok=True)
mediux = False
if os.path.exists(MEDIUX_FILE):
with open(MEDIUX_FILE, 'r') as file:
content = file.read().rstrip()
mediux = bool(content)
else:
with open(MEDIUX_FILE, 'w'):
pass
files = os.listdir(RAW_COVER_DIR)
content_changed = check_jellyfin_content()
webhook_triggered = webhook_server.get_trigger_status() if ENABLE_WEBHOOK else False
if files or content_changed or force or mediux or webhook_triggered:
if webhook_triggered:
logging.info('Process triggered by webhook!')
else:
logging.info('Found files, new Jellyfin content, or --force flag set!')
get_jellyfin_content()
collect_titles()
update_output_file()
language_data = load_language_data()
updater.scan_directories()
if folder_matcher is None:
folder_matcher = FolderMatcher(language_data)
else:
folder_matcher.update_language_data(language_data)
folder_matcher.reprocess_unmatched_files()
if mediux:
mediux_downloader()
cover_cleaner(language_data)
# Clean up empty folders in COVER_DIR
consolidate_series_folders()
cleanup_empty_folders(COVER_DIR)
if force:
logging.info("Force flag was set, resetting it to False after first iteration.")
force = False
# Use context manager for UpdateCover
async with updater:
logging.info('Run the UpdateCover process')
await updater.run()
# Explicit cleanup after processing
gc.collect()
else:
logging.info('Found no files or new content on Jellyfin')
await asyncio.sleep(30) # Wait for 30 seconds before the next iteration
except Exception as e:
logging.error(f"An error occurred in the main loop: {str(e)}")
logging.error(traceback.format_exc())
await asyncio.sleep(60) # Wait for 1 minute before retrying if an error occurs
async def run_application(force: bool):
webhook_server = WebhookServer()
if ENABLE_WEBHOOK:
logger.info("Webhook functionality enabled")
else:
logger.info("Webhook functionality disabled")
await asyncio.gather(
webhook_server.run_server(),
main_loop(force, webhook_server)
)
if __name__ == "__main__":
setup_logging()
# Argument parser
parser = argparse.ArgumentParser(description="Cover cleaner with optional force update.")
parser.add_argument('--force', action='store_true', help="Force the process to run regardless of conditions.")
args = parser.parse_args()
try:
asyncio.run(run_application(args.force))
except KeyboardInterrupt:
logger.info("Process interrupted by user. Shutting down.")
except Exception as e:
logger.critical(f"Critical error occurred: {str(e)}")
logger.critical(traceback.format_exc())