-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplexcache.py
1383 lines (1179 loc) · 65 KB
/
plexcache.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os, json, logging, glob, socket, platform, shutil, ntpath, posixpath, re, requests, subprocess, time, sys
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime, timedelta
from logging.handlers import RotatingFileHandler
from pathlib import Path
from plexapi.server import PlexServer
from plexapi.video import Episode
from plexapi.video import Movie
from plexapi.myplex import MyPlexAccount
from plexapi.exceptions import NotFound
from plexapi.exceptions import BadRequest
print("*** PlexCache ***")
script_folder = "/mnt/user/system/plexcache/" # Folder path for the PlexCache script storing the settings, watchlist & watched cache files
logs_folder = script_folder # Change this if you want your logs in a different folder
log_level = "" # Set the desired logging level for webhook notifications. Defaults to INFO when left empty. (Options: debug, info, warning, error, critical)
max_log_files = 5 # Maximum number of log files to keep
notification = "system" # "Unraid" or "Webhook", or "Both"; "System" instead will automatically switch to unraid if the scripts detects running on unraid
# Set the desired logging level for the notifications.
unraid_level = "summary"
webhook_level = ""
# Leave empty for notifications only on ERROR. (Options: debug, info, warning, error, critical)
# You can also set it to "summary" and it will notify on error but also give you a short summary at the end of each run.
webhook_url = "" # Your webhook URL, leave empty for no notifications.
webhook_headers = {} # Leave empty for Discord, otherwise edit it accordingly. (Slack example: "Content-Type": "application/json" "Authorization": "Bearer YOUR_SLACK_TOKEN" })
settings_filename = os.path.join(script_folder, "plexcache_settings.json")
watchlist_cache_file = Path(os.path.join(script_folder, "plexcache_watchlist_cache.json"))
watched_cache_file = Path(os.path.join(script_folder, "plexcache_watched_cache.json"))
mover_cache_exclude_file = Path(os.path.join(script_folder, "plexcache_mover_files_to_exclude.txt"))
if os.path.exists(mover_cache_exclude_file):
os.remove(mover_cache_exclude_file) # Remove the existing
RETRY_LIMIT = 3
DELAY = 5 # in seconds
permissions= 0o777
log_file_pattern = "plexcache_log_*.log"
summary_messages = []
files_moved = False
# Define a new level called SUMMARY that is equivalent to INFO level
SUMMARY = logging.WARNING + 1
logging.addLevelName(SUMMARY, 'SUMMARY')
start_time = time.time() # record start time
class UnraidHandler(logging.Handler):
SUMMARY = SUMMARY
def __init__(self):
super().__init__()
self.notify_cmd_base = "/usr/local/emhttp/webGui/scripts/notify"
if not os.path.isfile(self.notify_cmd_base) or not os.access(self.notify_cmd_base, os.X_OK):
logging.warning(f"{self.notify_cmd_base} does not exist or is not executable. Unraid notifications will not be sent.")
print(f"{self.notify_cmd_base} does not exist or is not executable. Unraid notifications will not be sent.")
self.notify_cmd_base = None
def emit(self, record):
if self.notify_cmd_base:
if record.levelno == SUMMARY:
self.send_summary_unraid_notification(record)
else:
self.send_unraid_notification(record)
def send_summary_unraid_notification(self, record):
icon = 'normal'
notify_cmd = f'{self.notify_cmd_base} -e "PlexCache" -s "Summary" -d "{record.msg}" -i "{icon}"'
subprocess.call(notify_cmd, shell=True)
def send_unraid_notification(self, record):
# Map logging levels to icons
level_to_icon = {
'WARNING': 'warning',
'ERROR': 'alert',
'INFO': 'normal',
'DEBUG': 'normal',
'CRITICAL': 'alert'
}
icon = level_to_icon.get(record.levelname, 'normal') # default to 'normal' if levelname is not found in the dictionary
# Prepare the command with necessary arguments
notify_cmd = f'{self.notify_cmd_base} -e "PlexCache" -s "{record.levelname}" -d "{record.msg}" -i "{icon}"'
# Execute the command
subprocess.call(notify_cmd, shell=True)
class WebhookHandler(logging.Handler):
SUMMARY = SUMMARY
def __init__(self, webhook_url):
super().__init__()
self.webhook_url = webhook_url
def emit(self, record):
if record.levelno == SUMMARY:
self.send_summary_webhook_message(record)
else:
self.send_webhook_message(record)
def send_summary_webhook_message(self, record):
summary = "Plex Cache Summary:\n" + record.msg
payload = {
"content": summary
}
headers = {
"Content-Type": "application/json"
}
response = requests.post(self.webhook_url, data=json.dumps(payload), headers=headers)
if not response.status_code == 204:
print(f"Failed to send summary message. Error code: {response.status_code}")
def send_webhook_message(self, record):
payload = {
"content": record.msg
}
headers = {
"Content-Type": "application/json"
}
response = requests.post(self.webhook_url, data=json.dumps(payload), headers=headers)
if not response.status_code == 204:
print(f"Failed to send message. Error code: {response.status_code}")
def check_and_create_folder(folder):
# Check if the folder doesn't already exist
if not os.path.exists(folder):
try:
# Create the folder with necessary parent directories
os.makedirs(folder, exist_ok=True)
except PermissionError:
# Exit the program if the folder is not writable
exit(f"{folder} not writable, please fix the variable accordingly.")
# Check and create the script folder
check_and_create_folder(script_folder)
# Check and create the logs folder if it's different from the script folder
if logs_folder != script_folder:
check_and_create_folder(logs_folder)
current_time = datetime.now().strftime("%Y%m%d_%H%M") # Get the current time and format it as YYYYMMDD_HHMM
log_file = os.path.join(logs_folder, f"{log_file_pattern[:-5]}{current_time}.log") # Create a filename based on the current time
latest_log_file = os.path.join(logs_folder, f"{log_file_pattern[:-5]}latest.log") # Create a filename for the latest log
logger = logging.getLogger() # Get the root logger
if log_level:
log_level = log_level.lower()
if log_level == "debug":
logger.setLevel(logging.DEBUG)
elif log_level == "info":
logger.setLevel(logging.INFO)
elif log_level == "warning":
logger.setLevel(logging.WARNING)
elif log_level == "error":
logger.setLevel(logging.ERROR)
elif log_level == "critical":
logger.setLevel(logging.CRITICAL)
else:
print(f"Invalid webhook_level: {log_level}. Using default level: ERROR")
logger.setLevel(logging.INFO)
# Configure the rotating file handler
handler = RotatingFileHandler(log_file, maxBytes=20*1024*1024, backupCount=max_log_files) # Create a rotating file handler
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) # Set the log message format
logger.addHandler(handler) # Add the file handler to the logger
# Create or update the symbolic link to the latest log file
if os.path.exists(latest_log_file):
os.remove(latest_log_file) # Remove the existing link if it exists
os.symlink(log_file, latest_log_file) # Create a new link to the latest log file
def clean_old_log_files(logs_folder, log_file_pattern, max_log_files):
# Find all log files that match the specified pattern in the logs folder
existing_log_files = glob.glob(os.path.join(logs_folder, log_file_pattern))
# Sort the log files based on their last modification time
existing_log_files.sort(key=os.path.getmtime)
# Remove log files until the number of remaining log files is within the desired limit
while len(existing_log_files) > max_log_files:
# Remove the oldest log file from the list and delete it from the filesystem
os.remove(existing_log_files.pop(0))
# Call the function to clean old log files
clean_old_log_files(logs_folder, log_file_pattern, max_log_files)
def check_os():
# Check the operating system
os_name = platform.system()
# Define information about different operating systems
os_info = {
'Linux': {'path': "/mnt/user0/", 'msg': 'Script is currently running on Linux.'},
'Darwin': {'path': None, 'msg': 'Script is currently running on macOS (untested).'},
'Windows': {'path': None, 'msg': 'Script is currently running on Windows.'}
}
# Check if the operating system is recognized
if os_name not in os_info:
logging.critical('This is an unrecognized system. Exiting...')
exit("Error: Unrecognized system.")
# Determine if the system is Linux
is_linux = True if os_name != 'Windows' else False
# Check if the system is Unraid (specific to Linux)
is_unraid = os.path.exists(os_info[os_name]['path']) if os_info[os_name]['path'] else False
# Modify the information message if the system is Unraid
if is_unraid:
os_info[os_name]['msg'] += ' The script is also running on Unraid.'
# Check if script is running inside a Docker container
is_docker = os.path.exists('/.dockerenv')
if is_docker:
os_info[os_name]['msg'] += ' The script is running inside a Docker container.'
# Log the information about the operating system
logging.info(os_info[os_name]['msg'])
return is_unraid, is_linux, is_docker
# Call the check_os() function and store the returned values
unraid, os_linux, is_docker = check_os()
# Create and add the webhook handler to the logger
if notification.lower() == "unraid" or notification.lower() == "system":
if unraid and not is_docker:
notification = "unraid"
else:
notification = ""
if notification.lower() == "both":
if unraid and is_docker:
notification = "webhook"
if notification.lower() == "both" or notification.lower() == "unraid":
unraid_handler = UnraidHandler()
if unraid_level:
unraid_level = unraid_level.lower()
if unraid_level == "debug":
unraid_handler.setLevel(logging.DEBUG)
elif unraid_level == "info":
unraid_handler.setLevel(logging.INFO)
elif unraid_level == "warning":
unraid_handler.setLevel(logging.WARNING)
elif unraid_level == "error":
unraid_handler.setLevel(logging.ERROR)
elif unraid_level == "critical":
unraid_handler.setLevel(logging.CRITICAL)
elif unraid_level.lower() == "summary":
unraid_handler.setLevel(SUMMARY)
else:
print(f"Invalid unraid_level: {unraid_level}. Using default level: ERROR")
unraid_handler.setLevel(logging.ERROR)
else:
unraid_handler.setLevel(logging.ERROR)
logger.addHandler(unraid_handler) # Add the unraid handler to the logger
# Create and add the webhook handler to the logger
if notification.lower() == "both" or notification.lower() == "webhook":
if webhook_url:
webhook_handler = WebhookHandler(webhook_url)
if webhook_level:
webhook_level = webhook_level.lower()
if webhook_level == "debug":
webhook_handler.setLevel(logging.DEBUG)
elif webhook_level == "info":
webhook_handler.setLevel(logging.INFO)
elif webhook_level == "warning":
webhook_handler.setLevel(logging.WARNING)
elif webhook_level == "error":
webhook_handler.setLevel(logging.ERROR)
elif webhook_level == "critical":
webhook_handler.setLevel(logging.CRITICAL)
elif webhook_level.lower() == "summary":
webhook_handler.setLevel(SUMMARY)
else:
print(f"Invalid webhook_level: {webhook_level}. Using default level: ERROR")
webhook_handler.setLevel(logging.ERROR)
else:
webhook_handler.setLevel(logging.ERROR)
logger.addHandler(webhook_handler) # Add the webhook handler to the logger
logging.info("*** PlexCache ***")
# Remove "/" or "\" from a given path
def remove_trailing_slashes(value):
try:
# Check if the value is a string
if isinstance(value, str):
# Check if the value contains a ':' and if the value with trailing slashes removed is empty
if ':' in value and value.rstrip('/\\') == '':
# Return the value with trailing slashes removed and add a backslash at the end
return value.rstrip('/') + "\\"
else:
# Return the value with trailing slashes removed
return value.rstrip('/\\')
# Return the value if it is not a string
return value
except Exception as e:
# Log an error if an exception occurs and raise it
logging.error(f"Error occurred while removing trailing slashes: {e}")
raise
# Add "/" or "\" to a given path
def add_trailing_slashes(value):
try:
# Check if the value does not contain a ':', indicating it's a Windows-style path
if ':' not in value:
# Add a leading "/" if the value does not start with it
if not value.startswith("/"):
value = "/" + value
# Add a trailing "/" if the value does not end with it
if not value.endswith("/"):
value = value + "/"
# Return the modified value
return value
except Exception as e:
# Log an error if an exception occurs and raise it
logging.error(f"Error occurred while adding trailing slashes: {e}")
raise
# Removed all "/" "\" from a given path
def remove_all_slashes(value_list):
try:
# Iterate over each value in the list and remove leading and trailing slashes
return [value.strip('/\\') for value in value_list]
except Exception as e:
logging.error(f"Error occurred while removing all slashes: {e}")
raise
# Convert the given path to a windows compatible path
def convert_path_to_nt(value, drive_letter):
try:
if value.startswith('/'):
# Add the drive letter to the beginning of the path
value = drive_letter.rstrip(':\\') + ':' + value
# Replace forward slashes with backslashes
value = value.replace(posixpath.sep, ntpath.sep)
# Normalize the path to remove redundant separators and references to parent directories
return ntpath.normpath(value)
except Exception as e:
logging.error(f"Error occurred while converting path to Windows compatible: {e}")
raise
# Convert the given path to a linux/posix compatible path
# If a drive letter is present, it will save it in the settings file.
def convert_path_to_posix(value):
try:
# Save the drive letter if exists
drive_letter = re.search(r'^[A-Za-z]:', value) # Check for a drive letter at the beginning of the path
if drive_letter:
drive_letter = drive_letter.group() + '\\' # Extract the drive letter and add a backslash
else:
drive_letter = None
# Remove drive letter if exists
value = re.sub(r'^[A-Za-z]:', '', value) # Remove the drive letter from the path
# Replace backslashes with slashes
value = value.replace(ntpath.sep, posixpath.sep) # Replace backslashes with forward slashes
return posixpath.normpath(value), drive_letter # Normalize the path and return it along with the drive letter
except Exception as e:
logging.error(f"Error occurred while converting path to Posix compatible: {e}")
raise
# Convert path accordingly to the operating system the script is running
# It assigns drive_letter = 'C:\\' if no drive was ever given/saved
def convert_path(value, key, settings_data, drive_letter=None):
try:
# Normalize paths converting backslashes to slashes
if os_linux: # Check if the operating system is Linux
value, drive_letter = convert_path_to_posix(value) # Convert path to POSIX format
if drive_letter:
settings_data[f"{key}_drive"] = drive_letter # Save the drive letter in the settings data
else:
if drive_letter is None:
if debug:
print(f"Drive letter for {value} not found, using the default one 'C:\\'")
logging.warning(f"Drive letter for {value} not found, using the default one 'C:\\'")
drive_letter = 'C:\\' # Set the default drive letter to 'C:\'
value = convert_path_to_nt(value, drive_letter) # Convert path to Windows format
return value
except Exception as e:
logging.error(f"Error occurred while converting path: {e}")
raise
# Check if the settings file exists
if os.path.exists(settings_filename):
# Loading the settings file
with open(settings_filename, 'r') as f:
settings_data = json.load(f)
else:
logging.critical("Settings file not found, please fix the variable accordingly.")
exit("Settings file not found, please fix the variable accordingly.")
# Reads the settings file and all the settings
try:
# Extracting the 'firststart' flag from the settings data
firststart = settings_data.get('firststart')
if firststart:
debug = True
print("First start is set to true, setting debug mode temporarily to true.")
logging.warning("First start is set to true, setting debug mode temporarily to true.")
del settings_data['firststart']
else:
debug = settings_data.get('debug')
if firststart is not None:
del settings_data['firststart']
# Extracting various settings from the settings data
PLEX_URL = settings_data['PLEX_URL']
PLEX_TOKEN = settings_data['PLEX_TOKEN']
number_episodes = settings_data['number_episodes']
valid_sections = settings_data['valid_sections']
days_to_monitor = settings_data['days_to_monitor']
users_toggle = settings_data['users_toggle']
# Checking and assigning 'skip_ondeck' and 'skip_watchlist' values
skip_ondeck = settings_data.get('skip_ondeck')
skip_watchlist = settings_data.get('skip_watchlist')
skip_users = settings_data.get('skip_users')
if skip_users is not None:
skip_ondeck = settings_data.get('skip_ondeck', skip_users)
skip_watchlist = settings_data.get('skip_watchlist', skip_users)
del settings_data['skip_users']
else:
skip_ondeck = settings_data.get('skip_ondeck', [])
skip_watchlist = settings_data.get('skip_watchlist', [])
watchlist_toggle = settings_data['watchlist_toggle']
watchlist_episodes = settings_data['watchlist_episodes']
watchlist_cache_expiry = settings_data['watchlist_cache_expiry']
watched_cache_expiry = settings_data['watched_cache_expiry']
watched_move = settings_data['watched_move']
plex_source_drive = settings_data.get('plex_source_drive')
plex_source = add_trailing_slashes(settings_data['plex_source'])
cache_dir_drive = settings_data.get('cache_dir_drive')
cache_dir = remove_trailing_slashes(settings_data['cache_dir'])
cache_dir = convert_path(cache_dir, 'cache_dir', settings_data, cache_dir_drive)
cache_dir = add_trailing_slashes(settings_data['cache_dir'])
real_source_drive = settings_data.get('real_source_drive')
real_source = remove_trailing_slashes(settings_data['real_source'])
real_source = convert_path(real_source, 'real_source', settings_data, real_source_drive)
real_source = add_trailing_slashes(settings_data['real_source'])
nas_library_folders = remove_all_slashes(settings_data['nas_library_folders'])
plex_library_folders = remove_all_slashes(settings_data['plex_library_folders'])
exit_if_active_session = settings_data.get('exit_if_active_session')
if exit_if_active_session is None:
exit_if_active_session = not settings_data.get('skip')
del settings_data['skip']
max_concurrent_moves_array = settings_data['max_concurrent_moves_array']
max_concurrent_moves_cache = settings_data['max_concurrent_moves_cache']
deprecated_unraid = settings_data.get('unraid')
if deprecated_unraid is not None:
del settings_data['unraid']
except KeyError as e:
# Error handling for missing key in settings file
logging.critical(f"Error: {e} not found in settings file, please re-run the setup or manually edit the settings file.")
exit(f"Error: {e} not found in settings file, please re-run the setup or manually edit the settings file.")
try:
# Save the updated settings data back to the file
with open(settings_filename, 'w') as f:
settings_data['cache_dir'] = cache_dir
settings_data['real_source'] = real_source
settings_data['plex_source'] = plex_source
settings_data['nas_library_folders'] = nas_library_folders
settings_data['plex_library_folders'] = plex_library_folders
settings_data['skip_ondeck'] = skip_ondeck
settings_data['skip_watchlist'] = skip_watchlist
settings_data['exit_if_active_session'] = exit_if_active_session
json.dump(settings_data, f, indent=4)
except Exception as e:
logging.error(f"Error occurred while saving settings data: {e}")
raise
# Initialising necessary arrays
processed_files = []
files_to_skip = []
media_to = []
media_to_cache = []
media_to_array = []
move_commands = []
skip_cache = "--skip-cache" in sys.argv
debug = "--debug" in sys.argv
# Connect to the Plex server
try:
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
except Exception as e:
logging.critical(f"Error connecting to the Plex server: {e}")
exit(f"Error connecting to the Plex server: {e}")
# Check if any active session
sessions = plex.sessions() # Get the list of active sessions
if sessions: # Check if there are any active sessions
if exit_if_active_session: # Check if the 'exit_if_active_session' boolean is set to true
logging.warning('There is an active session. Exiting...')
exit('There is an active session. Exiting...')
else:
for session in sessions: # Iterate over each active session
try:
media = str(session.source()) # Get the source of the session
media_id = media[media.find(":") + 1:media.find(":", media.find(":") + 1)] # Extract the media ID from the source
media_item = plex.fetchItem(int(media_id)) # Fetch the media item using the media ID
media_title = media_item.title # Get the title of the media item
media_type = media_item.type # Get the media type (e.g., show, movie)
if media_type == "episode": # Check if the media type is an episode
show_title = media_item.grandparentTitle # Get the title of the show
print(f"Active session detected, skipping: {show_title} - {media_title}") # Print a message indicating the active session with show and episode titles
logging.warning(f"Active session detected, skipping: {show_title} - {media_title}") # Log a warning message about the active session with show and episode titles
elif media_type == "movie": # Check if the media type is a movie
print(f"Active session detected, skipping: {media_title}") # Print a message indicating the active session with the movie title
logging.warning(f"Active session detected, skipping: {media_title}") # Log a warning message about the active session with the movie title
media_path = media_item.media[0].parts[0].file # Get the file path of the media item
logging.info(f"Skipping: {media_path}")
files_to_skip.append(media_path) # Add the file path to the list of files to skip
except Exception as e:
logging.error(f"Error occurred while processing session: {session} - {e}") # Log an error message if an exception occurs while processing the session
else:
logging.info('No active sessions found. Proceeding...') # Log an info message indicating no active sessions were found, and proceed with the code execution
# Check if debug mode is active
if debug:
print("Debug mode is active, NO FILE WILL BE MOVED.")
logging.getLogger().setLevel(logging.DEBUG)
logging.warning("Debug mode is active, NO FILE WILL BE MOVED.")
logging.info(f"Real source: {real_source}")
logging.info(f"Cache dir: {cache_dir}")
logging.info(f"Plex source: {plex_source}")
logging.info(f"NAS folders: ({nas_library_folders}")
logging.info(f"Plex folders: {plex_library_folders}")
else:
logging.getLogger().setLevel(logging.INFO)
# Main function to fetch onDeck media files
def fetch_on_deck_media_main(plex, valid_sections, days_to_monitor, number_episodes, users_toggle, skip_ondeck):
try:
users_to_fetch = [None] # Start with main user (None)
if users_toggle:
users_to_fetch += plex.myPlexAccount().users()
# Filter out the users present in skip_ondeck
users_to_fetch = [user for user in users_to_fetch if (user is None) or (user.get_token(plex.machineIdentifier) not in skip_ondeck)]
with ThreadPoolExecutor(max_workers=10) as executor:
futures = {executor.submit(fetch_on_deck_media, plex, valid_sections, days_to_monitor, number_episodes, user) for user in users_to_fetch}
for future in as_completed(futures):
try:
yield from future.result()
except Exception as e:
print(f"An error occurred in fetch_on_deck_media: {e}")
logging.error(f"An error occurred while fetching onDeck media for a user: {e}")
except Exception as e:
print(f"An error occurred in fetch_on_deck_media: {e}")
logging.error(f"An error occurred in fetch_on_deck_media_main: {e}")
def fetch_on_deck_media(plex, valid_sections, days_to_monitor, number_episodes, user=None):
try:
username, plex = get_plex_instance(plex, user) # Get the username and Plex instance
if not plex: # Check if Plex instance is available
return [] # Return an empty list
print(f"Fetching {username}'s onDeck media...") # Print a message indicating that onDeck media is being fetched
logging.info(f"Fetching {username}'s onDeck media...") # Log the message indicating that onDeck media is being fetched
on_deck_files = [] # Initialize an empty list to store onDeck files
# Get all sections available for the user
available_sections = [section.key for section in plex.library.sections()]
# Intersect available_sections and valid_sections
filtered_sections = list(set(available_sections) & set(valid_sections))
for video in plex.library.onDeck(): # Iterate through the onDeck videos in the Plex library
section_key = video.section().key # Get the section key of the video
if not filtered_sections or section_key in filtered_sections: # Check if filtered_sections is empty or the video belongs to a valid section
delta = datetime.now() - video.lastViewedAt # Calculate the time difference between now and the last viewed time of the video
if delta.days <= days_to_monitor: # Check if the video was viewed within the specified number of days
if isinstance(video, Episode): # Check if the video is an episode
process_episode_ondeck(video, number_episodes, on_deck_files) # Process the episode and add it to the onDeck files list
elif isinstance(video, Movie): # Check if the video is a movie
process_movie_ondeck(video, on_deck_files) # Process the movie and add it to the onDeck files list
return on_deck_files # Return the list of onDeck files
except Exception as e: # Handle any exceptions that occur
print(f"An error occurred while fetching onDeck media: {e}") # Print an error message indicating the exception
logging.error(f"An error occurred while fetching onDeck media: {e}") # Log an error message indicating the exception
return [] # Return an empty list
# Function to fetch the Plex instance
def get_plex_instance(plex, user):
if user:
username = user.title # Get the username
try:
return username, PlexServer(PLEX_URL, user.get_token(plex.machineIdentifier)) # Return username and PlexServer instance with user token
except Exception as e:
print(f"Error: Failed to Fetch {username} onDeck media. Error: {e}") # Print error message if failed to fetch onDeck media for the user
logging.error(f"Error: Failed to Fetch {username} onDeck media. Error: {e}") # Log the error
return None, None
else:
username = plex.myPlexAccount().title # Get the username from the Plex account
return username, PlexServer(PLEX_URL, PLEX_TOKEN) # Return username and PlexServer instance with account token
# Function to process the onDeck media files
def process_episode_ondeck(video, number_episodes, on_deck_files):
for media in video.media:
on_deck_files.extend(part.file for part in media.parts) # Add file paths of media parts to onDeck files list
show = video.grandparentTitle # Get the title of the show
library_section = video.section() # Get the library section of the video
episodes = list(library_section.search(show)[0].episodes()) # Search the library section for episodes of the show
current_season = video.parentIndex # Get the index of the current season
next_episodes = get_next_episodes(episodes, current_season, video.index, number_episodes) # Get the next episodes based on the current episode and season
for episode in next_episodes:
for media in episode.media:
on_deck_files.extend(part.file for part in media.parts) # Add file paths of media parts of the next episodes to onDeck files list
for part in media.parts:
logging.info(f"OnDeck found: {(part.file)}") # Log the file path of the onDeck media part
# Function to process the onDeck movies files
def process_movie_ondeck(video, on_deck_files):
for media in video.media:
on_deck_files.extend(part.file for part in media.parts) # Add file paths of media parts to onDeck files list
for part in media.parts:
logging.info(f"OnDeck found: {(part.file)}") # Log the file path of the onDeck media part
# Function to get the next episodes
def get_next_episodes(episodes, current_season, current_episode_index, number_episodes):
next_episodes = []
for episode in episodes:
if (episode.parentIndex > current_season or (episode.parentIndex == current_season and episode.index > current_episode_index)) and len(next_episodes) < number_episodes:
next_episodes.append(episode) # Add the episode to the next_episodes list if it comes after the current episode
if len(next_episodes) == number_episodes:
break # Stop iterating if the desired number of next episodes is reached
return next_episodes # Return the list of next episodes
# Function to search for a file in the Plex server
def search_plex(plex, title):
results = plex.search(title)
return results[0] if len(results) > 0 else None
def fetch_watchlist_media(plex, valid_sections, watchlist_episodes, users_toggle, skip_watchlist):
def get_watchlist(token, user=None, retries=0):
# Retrieve the watchlist for the specified user's token.
account = MyPlexAccount(token=token)
try:
if user:
account = account.switchHomeUser(f'{user.title}')
return account.watchlist(filter='released')
except (BadRequest, NotFound) as e:
if "429" in str(e) and retries < RETRY_LIMIT: # Rate limit exceeded
logging.warning(f"Rate limit exceeded. Retrying {retries + 1}/{RETRY_LIMIT}. Sleeping for {DELAY} seconds...")
time.sleep(DELAY)
return get_watchlist(token, user, retries + 1)
elif isinstance(e, NotFound):
logging.warning(f"Failed to switch to user {user.title if user else 'Unknown'}. Skipping...")
return []
else:
raise e
def process_show(file, watchlist_episodes):
#Process episodes of a TV show file up to a specified number.
episodes = file.episodes()
count = 0
for episode in episodes[:watchlist_episodes]:
if len(episode.media) > 0 and len(episode.media[0].parts) > 0:
count += 1
if not episode.isPlayed:
yield episode.media[0].parts[0].file
def process_movie(file):
#Process a movie file.
if not file.isPlayed:
yield file.media[0].parts[0].file
def fetch_user_watchlist(user):
current_username = plex.myPlexAccount().title if user is None else user.title
available_sections = [section.key for section in plex.library.sections()]
filtered_sections = list(set(available_sections) & set(valid_sections))
if user and user.get_token(plex.machineIdentifier) in skip_watchlist:
logging.info(f"Skipping {current_username}'s watchlist media...")
return []
logging.info(f"Fetching {current_username}'s watchlist media...")
try:
watchlist = get_watchlist(PLEX_TOKEN, user)
results = []
for item in watchlist:
file = search_plex(plex, item.title)
if file and (not filtered_sections or (file.librarySectionID in filtered_sections)):
if file.TYPE == 'show':
results.extend(process_show(file, watchlist_episodes))
else:
results.extend(process_movie(file))
return results
except Exception as e:
logging.error(f"Error fetching watchlist for {current_username}: {str(e)}")
return []
users_to_fetch = [None] # Start with main user (None)
if users_toggle:
users_to_fetch += plex.myPlexAccount().users()
with ThreadPoolExecutor(max_workers=10) as executor:
futures = {executor.submit(fetch_user_watchlist, user) for user in users_to_fetch}
for future in as_completed(futures):
retries = 0
while retries < RETRY_LIMIT:
try:
yield from future.result()
break
except Exception as e:
if "429" in str(e): # rate limit error
logging.warning(f"Rate limit exceeded. Retrying in {DELAY} seconds...")
time.sleep(DELAY)
retries += 1
else:
logging.error(f"Error fetching watchlist media: {str(e)}")
break
# Function to fetch watched media files
def get_watched_media(plex, valid_sections, last_updated, users_toggle):
def fetch_user_watched_media(plex_instance, username, retries=0):
try:
print(f"Fetching {username}'s watched media...")
logging.info(f"Fetching {username}'s watched media...")
# Get all sections available for the user
all_sections = [section.key for section in plex_instance.library.sections()]
# Check if valid_sections is specified. If not, consider all available sections as valid.
if 'valid_sections' in globals() and valid_sections:
available_sections = list(set(all_sections) & set(valid_sections))
else:
available_sections = all_sections
# Filter sections the user has access to
user_accessible_sections = [section for section in available_sections if section in all_sections]
for section_key in user_accessible_sections:
section = plex_instance.library.sectionByID(section_key) # Get the section object using its key
# Search for videos in the section
for video in section.search(unwatched=False):
# Skip if the video was last viewed before the last_updated timestamp
if video.lastViewedAt and last_updated and video.lastViewedAt < datetime.fromtimestamp(last_updated):
continue
# Process the video and yield the file path
yield from process_video(video)
except (BadRequest, NotFound) as e:
if "429" in str(e) and retries < RETRY_LIMIT: # Rate limit exceeded
print(f"Rate limit exceeded. Retrying {retries + 1}/{RETRY_LIMIT}. Sleeping for {DELAY} seconds...")
logging.warning(f"Rate limit exceeded. Retrying {retries + 1}/{RETRY_LIMIT}. Sleeping for {DELAY} seconds...")
time.sleep(DELAY)
return fetch_user_watched_media(user_plex, username, retries + 1)
elif isinstance(e, NotFound):
print(f"Failed to switch to user {user.title if user else 'Unknown'}. Skipping...")
logging.warning(f"Failed to switch to user {user.title if user else 'Unknown'}. Skipping...")
return []
else:
raise e
def process_video(video):
if video.TYPE == 'show':
# Iterate through each episode of a show video
for episode in video.episodes():
yield from process_episode(episode)
else:
# Get the file path of the video
#if video.isPlayed:
file_path = video.media[0].parts[0].file
yield file_path
def process_episode(episode):
# Iterate through each media and part of an episode
for media in episode.media:
for part in media.parts:
if episode.isPlayed:
# Get the file path of the played episode
file_path = part.file
yield file_path
# Create a ThreadPoolExecutor
with ThreadPoolExecutor() as executor:
main_username = plex.myPlexAccount().title
# Start a new task for the main user
futures = [executor.submit(fetch_user_watched_media, plex, main_username)]
if users_toggle:
for user in plex.myPlexAccount().users():
username = user.title
user_token = user.get_token(plex.machineIdentifier)
user_plex = PlexServer(PLEX_URL, user_token)
# Start a new task for each other user
futures.append(executor.submit(fetch_user_watched_media, user_plex, username))
# As each task completes, yield the results
for future in as_completed(futures):
try:
yield from future.result()
except Exception as e:
print(f"An error occurred in get_watched_media: {e}")
logging.error(f"An error occurred in get_watched_media: {e}")
# Function to load watched media from cache
def load_media_from_cache(cache_file):
if cache_file.exists():
with cache_file.open('r') as f:
try:
data = json.load(f)
if isinstance(data, dict):
return set(data.get('media', [])), data.get('timestamp')
elif isinstance(data, list):
# cache file contains just a list of media, without timestamp
return set(data), None
except json.JSONDecodeError:
# Clear the file and return an empty set
with cache_file.open('w') as f:
f.write(json.dumps({'media': [], 'timestamp': None}))
return set(), None
return set(), None
# Modify the files paths from the paths given by plex to link actual files on the running system
def modify_file_paths(files, plex_source, real_source, plex_library_folders, nas_library_folders):
# Print and log a message indicating that file paths are being edited
print("Editing file paths...")
logging.info("Editing file paths...")
# If no files are provided, return an empty list
if files is None:
return []
# Filter the files based on those that start with the plex_source path
files = [file_path for file_path in files if file_path.startswith(plex_source)]
# Iterate over each file path and modify it accordingly
for i, file_path in enumerate(files):
# Log the original file path
logging.info(f"Original path: {file_path}")
# Replace the plex_source with the real_source in the file path
file_path = file_path.replace(plex_source, real_source, 1) # Replace the plex_source with the real_source, thanks to /u/planesrfun
# Determine which library folder is in the file path
for j, folder in enumerate(plex_library_folders):
if folder in file_path:
# Replace the plex library folder with the corresponding NAS library folder
file_path = file_path.replace(folder, nas_library_folders[j])
break
# Update the modified file path in the files list
files[i] = file_path
# Log the edited file path
logging.info(f"Edited path: {file_path}")
# Return the modified file paths or an empty list
return files or []
def get_media_subtitles(media_files, files_to_skip=None, subtitle_extensions=[".srt", ".vtt", ".sbv", ".sub", ".idx"]):
print("Fetching subtitles...")
logging.info("Fetching subtitles...")
files_to_skip = set() if files_to_skip is None else set(files_to_skip)
processed_files = set()
all_media_files = media_files.copy()
for file in media_files:
if file in files_to_skip or file in processed_files:
continue
processed_files.add(file)
directory_path = os.path.dirname(file)
if os.path.exists(directory_path):
subtitle_files = find_subtitle_files(directory_path, file, subtitle_extensions)
all_media_files.extend(subtitle_files)
for subtitle_file in subtitle_files:
logging.info(f"Subtitle found: {subtitle_file}")
return all_media_files or []
def find_subtitle_files(directory_path, file, subtitle_extensions):
file_name, _ = os.path.splitext(os.path.basename(file))
try:
subtitle_files = [
entry.path
for entry in os.scandir(directory_path)
if entry.is_file() and entry.name.startswith(file_name) and entry.name != file and entry.name.endswith(tuple(subtitle_extensions))
]
except PermissionError as e:
logging.error(f"Cannot access directory {directory_path}. Permission denied. Error: {e}")
subtitle_files = []
except OSError as e:
logging.error(f"Cannot access directory {directory_path}. Error: {e}")
subtitle_files = []
return subtitle_files or []
# Function to convert size to readable format
def convert_bytes_to_readable_size(size_bytes):
if size_bytes >= (1024 ** 4):
size = size_bytes / (1024 ** 4)
unit = 'TB'
elif size_bytes >= (1024 ** 3):
size = size_bytes / (1024 ** 3)
unit = 'GB'
elif size_bytes >= (1024 ** 2):
size = size_bytes / (1024 ** 2)
unit = 'MB'
else:
size = size_bytes / 1024
unit = 'KB'
# Return the size and corresponding unit
return size, unit
# Function to check for free space
def get_free_space(dir):
if not os.path.exists(dir):
logging.error(f"Invalid path, unable to calculate free space for: {dir}.")
return 0
stat = os.statvfs(dir) # Get the file system statistics for the specified directory
free_space_bytes = stat.f_bfree * stat.f_frsize # Calculate the free space in bytes
return convert_bytes_to_readable_size(free_space_bytes) # Convert the free space to a human-readable format
# Function to calculate size of the files contained in the given array
def get_total_size_of_files(files):
total_size_bytes = sum(os.path.getsize(file) for file in files) # Calculate the total size of the files in bytes
return convert_bytes_to_readable_size(total_size_bytes) # Convert the total size to a human-readable format
# Function to filter the files, based on the destination
def filter_files(files, destination, real_source, cache_dir, media_to_cache=None, files_to_skip=None):
logging.info(f"Filtering media files for {destination}...")
if files_to_skip:
# Assuming you have a function modify_file_paths() that returns modified file paths.
files_to_skip = modify_file_paths(files_to_skip, plex_source, real_source, plex_library_folders, nas_library_folders)
try:
if media_to_cache is None:
media_to_cache = []
processed_files = set()
media_to = []
cache_files_to_exclude = []
if not files:
return []
for file in files:
if file in processed_files or (files_to_skip and file in files_to_skip):
continue
processed_files.add(file)
cache_file_name = get_cache_paths(file, real_source, cache_dir)[1]
# Get the cache file name using the file's path, real_source, and cache_dir
cache_files_to_exclude.append(cache_file_name)
if destination == 'array':
if should_add_to_array(file, cache_file_name, media_to_cache):
media_to.append(file)
logging.info(f"Adding file to array: {file}")
elif destination == 'cache':
if should_add_to_cache(file, cache_file_name):
media_to.append(file)
logging.info(f"Adding file to cache: {file}")
if unraid:
with open(mover_cache_exclude_file, "w") as file:
for item in cache_files_to_exclude:
file.write(str(item) + "\n")
return media_to or []
except Exception as e:
logging.error(f"Error occurred while filtering media files: {str(e)}")
return []
def should_add_to_array(file, cache_file_name, media_to_cache):
if file in media_to_cache:
return False
array_file = file.replace("/mnt/user/", "/mnt/user0/", 1) if unraid else file
if os.path.isfile(array_file):
# File already exists in the array
if os.path.isfile(cache_file_name):
os.remove(cache_file_name)