-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
311 lines (274 loc) · 11.1 KB
/
app.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
"""
Media Utilities
"""
version = "v2.3.2"
import sys
import os
import json
import logging
import signal
from sys import platform
from logging.handlers import RotatingFileHandler
from apscheduler.schedulers.blocking import BlockingScheduler
from api.plex import PlexAPI
from api.tautulli import TautulliAPI
from api.emby import EmbyAPI
from api.jellystat import JellystatAPI
from common.gotify_handler import GotifyHandler
from common.plain_text_formatter import PlainTextFormatter
from common.gotify_plain_text_formatter import GotifyPlainTextFormatter
import colorlog
from common import utils
from service.ServiceBase import ServiceBase
from service.DeleteWatched import DeleteWatched
from service.DvrMaintainer import DvrMaintainer
from service.FolderCleanup import FolderCleanup
from service.PlaylistSync import PlaylistSync
from service.SyncWatched import SyncWatched
# Global Variables #######
logger = logging.getLogger(__name__)
scheduler = BlockingScheduler()
# Api
plex_api: PlexAPI = None
tautulli_api: TautulliAPI = None
emby_api: EmbyAPI = None
jellystat_api: JellystatAPI = None
# Available Services
services: list[ServiceBase] = []
##########################
conf_loc_path_file = ""
config_file_valid = True
def handle_sigterm(signum, frame):
logger.info("SIGTERM received, shutting down ...")
for service in services:
service.shutdown()
scheduler.shutdown(wait=True)
sys.exit(0)
def do_nothing():
""" Do nothing """
pass
if "CONFIG_PATH" in os.environ:
conf_loc_path_file = os.environ["CONFIG_PATH"].rstrip("/")
else:
config_file_valid = False
if config_file_valid and os.path.exists(conf_loc_path_file):
try:
# Opening JSON file
with open(conf_loc_path_file, "r") as f:
data = json.load(f)
# Main script run ####################################################
# Set up signal termination handle
signal.signal(signal.SIGTERM, handle_sigterm)
# date format
date_format = "%Y-%m-%d %H:%M:%S"
# Set up the logger
logger.setLevel(logging.INFO)
formatter = PlainTextFormatter()
# Create a file handler to write logs to a file
rotating_handler: RotatingFileHandler = RotatingFileHandler(
"/logs/media-utility.log", maxBytes=50000, backupCount=5
)
rotating_handler.setLevel(logging.INFO)
rotating_handler.setFormatter(formatter)
log_colors = {
"DEBUG": "cyan",
"INFO": "light_green",
"WARNING": "light_yellow",
"ERROR": "light_red",
"CRITICAL": "bold_red"
}
# Create a stream handler to print logs to the console
console_info_handler = colorlog.StreamHandler()
console_info_handler.setLevel(logging.INFO)
console_info_handler.setFormatter(
colorlog.ColoredFormatter(
"%(white)s%(asctime)s %(light_white)s- %(log_color)s%(levelname)s %(light_white)s- %(message)s",
date_format,
log_colors=log_colors
)
)
gotify_formatter: GotifyPlainTextFormatter = None
gotify_handler: GotifyHandler = None
if (
"gotify_logging" in data
and "enabled" in data["gotify_logging"]
and data["gotify_logging"]["enabled"] == "True"
):
gotify_formatter = GotifyPlainTextFormatter()
gotify_handler = GotifyHandler(
data["gotify_logging"]["url"],
data["gotify_logging"]["app_token"],
data["gotify_logging"]["message_title"],
data["gotify_logging"]["priority"]
)
gotify_handler.setLevel(logging.WARNING)
gotify_handler.setFormatter(gotify_formatter)
# Add the handlers to the logger
logger.addHandler(rotating_handler)
logger.addHandler(console_info_handler)
if gotify_handler is not None:
logger.addHandler(gotify_handler)
logger.info(
f"Starting Media Utilities {version} *************************************"
)
# Create all the api servers
if "plex_url" in data and "plex_api_key" in data:
plex_api = PlexAPI(
data["plex_url"],
data["plex_api_key"],
data["plex_admin_user_name"],
data["plex_media_path"],
logger
)
if plex_api.get_valid():
logger.info(
f"Connected to {utils.get_formatted_plex()}:{plex_api.get_name()} successfully"
)
else:
plex_url_tag = utils.get_tag("url", data["plex_url"])
plex_api_tag = utils.get_tag("api_key", data["plex_api_key"])
logger.warning(
f"{utils.get_formatted_plex()} server not available. Is this correct {plex_url_tag} {plex_api_tag}"
)
elif "plex_url" in data or "plex_api_key" in data:
logger.warning(
f"{utils.get_formatted_plex()} configuration error must define both plex_url and plex_api_key"
)
if "tautulli_url" in data and "tautulli_api_key" in data:
tautulli_api = TautulliAPI(
data["tautulli_url"], data["tautulli_api_key"], logger
)
if tautulli_api.get_valid():
logger.info(
f"Connected to {utils.get_formatted_tautulli()}:{tautulli_api.get_name()} successfully"
)
else:
tautulli_url_tag = utils.get_tag("url", data["tautulli_url"])
tautulli_api_tag = utils.get_tag("api_key", data["tautulli_api_key"])
logger.warning(
f"{utils.get_formatted_tautulli()} not available. Is this correct {tautulli_url_tag} {tautulli_api_tag}"
)
elif "tautulli_url" in data or "tautulli_api_key" in data:
logger.warning(
f"{utils.get_formatted_tautulli()} configuration error must define both tautulli_url and tautulli_api_key"
)
if "emby_url" in data and "emby_api_key" in data:
emby_api = EmbyAPI(
data["emby_url"],
data["emby_api_key"],
data["emby_media_path"],
logger
)
if emby_api.get_valid():
logger.info(
f"Connected to {utils.get_formatted_emby()}:{emby_api.get_name()} successfully"
)
else:
emby_url_tag = utils.get_tag("url", data["emby_url"])
emby_api_tag = utils.get_tag("api_key", data["emby_api_key"])
logger.warning(
f"{utils.get_formatted_emby()} server not available. Is this correct {emby_url_tag} {emby_api_tag}"
)
elif "emby_url" in data or "emby_api_key" in data:
logger.warning(
f"{utils.get_formatted_emby()} configuration error must define both emby_url and emby_api_key"
)
if "jellystat_url" in data and "jellystat_api_key" in data:
jellystat_api = JellystatAPI(
data["jellystat_url"],
data["jellystat_api_key"],
logger
)
if jellystat_api.get_valid():
logger.info(
f"Connected to {utils.get_formatted_jellystat()} successfully"
)
else:
jellystat_url_tag = utils.get_tag("url", data["jellystat_url"])
jellystat_api_tag = utils.get_tag("api_key", data["jellystat_api_key"])
logger.warning(
f"{utils.get_formatted_jellystat()} not available. Is this correct {jellystat_url_tag} {jellystat_api_tag}"
)
elif "jellystat_url" in data or "jellystat_api_key" in data:
logger.warning(
f"{utils.get_formatted_jellystat()} configuration error must define both jellystat_url and jellystat_api_key"
)
# Create the services ####################################
# Create the Sync Watched Status Service
if "sync_watched" in data and data["sync_watched"]["enabled"] == "True":
services.append(
SyncWatched(
f"{utils.ansi_start_code}45{utils.ansi_end_code}",
plex_api,
tautulli_api,
emby_api,
jellystat_api,
data["sync_watched"],
logger,
scheduler
)
)
if "delete_watched" in data and data["delete_watched"]["enabled"] == "True":
services.append(
DeleteWatched(
f"{utils.ansi_start_code}142{utils.ansi_end_code}",
plex_api,
tautulli_api,
emby_api,
jellystat_api,
data["delete_watched"],
logger,
scheduler
)
)
if "dvr_maintainer" in data and data["dvr_maintainer"]["enabled"] == "True":
services.append(
DvrMaintainer(
f"{utils.ansi_start_code}210{utils.ansi_end_code}",
plex_api,
emby_api,
data["dvr_maintainer"],
logger,
scheduler
)
)
if "folder_cleanup" in data and data["folder_cleanup"]["enabled"] == "True":
services.append(
FolderCleanup(
f"{utils.ansi_start_code}70{utils.ansi_end_code}",
plex_api,
emby_api,
data["folder_cleanup"],
logger,
scheduler
)
)
if "playlist_sync" in data and data["playlist_sync"]["enabled"] == "True":
services.append(
PlaylistSync(
f"{utils.ansi_start_code}171{utils.ansi_end_code}",
plex_api,
emby_api,
data["playlist_sync"],
logger,
scheduler
)
)
# ########################################################
# Init the services ######################################
for service in services:
service.init_scheduler_jobs()
# ########################################################
# Add a job to do nothing to keep the script alive
scheduler.add_job(
do_nothing,
trigger="interval",
hours=24
)
# Start the scheduler for all jobs
scheduler.start()
except Exception as e:
logger.error(f"Error starting: {e}")
else:
sys.stderr.write(f"Error opening config file {conf_loc_path_file}")
# END Main script run ####################################################