forked from StudioNirin/PlexCache-D
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplexcache_setup.py
More file actions
executable file
·1402 lines (1167 loc) · 54.7 KB
/
Copy pathplexcache_setup.py
File metadata and controls
executable file
·1402 lines (1167 loc) · 54.7 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
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 json, os, requests, ntpath, posixpath, uuid, time, webbrowser
from urllib.parse import urlparse
from plexapi.server import PlexServer
from plexapi.exceptions import BadRequest
# Script folder and settings file
script_folder = os.path.dirname(os.path.abspath(__file__))
settings_filename = os.path.join(script_folder, "plexcache_settings.json")
# ensure a settings container exists early so helper functions can reference it
settings_data = {}
# ---------------- Helper Functions ----------------
def check_directory_exists(folder):
if not os.path.exists(folder):
raise FileNotFoundError(f'Wrong path given, please edit the "{folder}" variable accordingly.')
def read_existing_settings(filename):
try:
with open(filename, 'r', encoding='utf-8') as f:
return json.load(f)
except (IOError, OSError) as e:
print(f"Error reading settings file: {e}")
raise
def write_settings(filename, data):
try:
with open(filename, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4)
except (IOError, OSError) as e:
print(f"Error writing settings file: {e}")
raise
def convert_path_to_posix(path):
path = path.replace(ntpath.sep, posixpath.sep)
return posixpath.normpath(path)
def convert_path_to_nt(path):
path = path.replace(posixpath.sep, ntpath.sep)
return ntpath.normpath(path)
def prompt_user_for_number(prompt_message, default_value, data_key, data_type=int):
while True:
user_input = input(prompt_message) or default_value
try:
value = data_type(user_input)
if value < 0:
print("Please enter a non-negative number")
continue
settings_data[data_key] = value
break
except ValueError:
print("User input is not a valid number")
def prompt_user_for_duration(prompt_message, default_value, data_key):
"""Prompt for a duration value that accepts hours (default) or days.
Accepts formats: 12, 12h, 12d (defaults to hours if no suffix)
Stores the value in hours.
"""
while True:
user_input = (input(prompt_message) or default_value).strip().lower()
try:
# Check for day suffix
if user_input.endswith('d'):
days = float(user_input[:-1])
if days < 0:
print("Please enter a non-negative number")
continue
hours = int(days * 24)
settings_data[data_key] = hours
print(f" Set to {hours} hours ({days} days)")
break
# Check for hour suffix (or no suffix - default to hours)
elif user_input.endswith('h'):
hours = int(user_input[:-1])
else:
hours = int(user_input)
if hours < 0:
print("Please enter a non-negative number")
continue
settings_data[data_key] = hours
break
except ValueError:
print("Invalid input. Enter a number, optionally with 'h' for hours or 'd' for days (e.g., 12, 12h, 2d)")
def prompt_user_for_duration_days(prompt_message, default_value, data_key):
"""Prompt for a duration value that accepts days (default) or hours.
Accepts formats: 30, 30d, 12h (defaults to days if no suffix)
Stores the value in days (as float to support fractional days from hours).
"""
while True:
user_input = (input(prompt_message) or default_value).strip().lower()
try:
# Check for hour suffix
if user_input.endswith('h'):
hours = float(user_input[:-1])
if hours < 0:
print("Please enter a non-negative number")
continue
days = hours / 24
settings_data[data_key] = days
print(f" Set to {days:.2f} days ({hours} hours)")
break
# Check for day suffix (or no suffix - default to days)
elif user_input.endswith('d'):
days = float(user_input[:-1])
else:
days = float(user_input)
if days < 0:
print("Please enter a non-negative number")
continue
settings_data[data_key] = days
break
except ValueError:
print("Invalid input. Enter a number, optionally with 'd' for days or 'h' for hours (e.g., 30, 30d, 12h)")
def is_valid_plex_url(url):
try:
result = urlparse(url)
return all([result.scheme, result.netloc])
except ValueError:
return False
# Helper to compute a common root for a list of paths
def find_common_root(paths):
"""Return the deepest common directory for all given paths."""
if not paths:
return "/"
# Normalize trailing slashes and split
normed = [p.rstrip('/') for p in paths]
split_paths = [p.split('/') for p in normed]
common_parts = []
for parts in zip(*split_paths):
if all(part == parts[0] for part in parts):
common_parts.append(parts[0])
else:
break
# Handle leading empty string (absolute paths)
if common_parts and common_parts[0] == '':
if len(common_parts) == 1:
return '/'
return "/" + "/".join(common_parts[1:])
return "/" + "/".join(common_parts) if common_parts else "/"
def is_unraid():
"""Check if running on Unraid."""
return os.path.exists('/etc/unraid-version')
# ---------------- Multi-Path Mapping Functions ----------------
def prompt_library_path_mapping(library_name: str, plex_locations: list, cache_root: str = None) -> list:
"""Prompt user to configure path mappings for a library's locations.
Args:
library_name: Display name of the library (e.g., "Movies")
plex_locations: List of Plex paths for this library
cache_root: Cache root directory (e.g., /mnt/cache_downloads/)
Returns:
List of path mapping dicts for this library's locations
"""
mappings = []
if len(plex_locations) > 1:
print(f"\n Plex locations for this library:")
for loc in plex_locations:
print(f" - {loc}")
for i, plex_path in enumerate(plex_locations):
# For libraries with multiple locations, number them
if len(plex_locations) > 1:
mapping_name = f"{library_name} ({i+1})"
print(f"\n Configuring location {i+1}: {plex_path}")
else:
mapping_name = library_name
# Suggest a real path based on common patterns
suggested_real = plex_path.replace('/data/', '/mnt/user/').replace('/media/', '/mnt/user/')
print(f" Where is this located on your filesystem?")
real_path = input(f" Real path [{suggested_real}]: ").strip() or suggested_real
# Ensure trailing slash
if real_path and not real_path.endswith('/'):
real_path = real_path + '/'
# Ensure plex_path has trailing slash
plex_path_normalized = plex_path if plex_path.endswith('/') else plex_path + '/'
# Ask if cacheable
print(f"\n Can this be cached locally? (No for remote/network storage)")
cacheable_input = input(f" Cacheable? [Y/n]: ").strip().lower()
cacheable = cacheable_input not in ['n', 'no']
cache_path = None
if cacheable and cache_root:
# Use cache_root + library name (sanitized)
# e.g., /mnt/cache_downloads/ + "Movies" = /mnt/cache_downloads/Movies/
lib_folder = library_name.replace('/', '_').replace('\\', '_')
if len(plex_locations) > 1:
# For multi-location libraries, use folder name from plex path
lib_folder = plex_path.rstrip('/').split('/')[-1]
suggested_cache = cache_root.rstrip('/') + '/' + lib_folder + '/'
print(f"\n Where should cached files be stored?")
cache_path = input(f" Cache path [{suggested_cache}]: ").strip() or suggested_cache
# Ensure trailing slash
if cache_path and not cache_path.endswith('/'):
cache_path = cache_path + '/'
elif cacheable and not cache_root:
# No cache root set - shouldn't happen in new flow but handle gracefully
suggested_cache = real_path.replace('/mnt/user/', '/mnt/cache/')
print(f"\n Where should cached files be stored?")
cache_path = input(f" Cache path [{suggested_cache}]: ").strip() or suggested_cache
if cache_path and not cache_path.endswith('/'):
cache_path = cache_path + '/'
mapping = {
'name': mapping_name,
'plex_path': plex_path_normalized,
'real_path': real_path,
'cache_path': cache_path,
'cacheable': cacheable,
'enabled': True
}
mappings.append(mapping)
cache_display = f" → {cache_path}" if cache_path else " (non-cacheable)"
print(f"\n ✓ {mapping_name}: {plex_path_normalized} → {real_path}{cache_display}")
return mappings
def display_path_mappings(mappings):
"""Display current path mappings in a formatted table."""
if not mappings:
print("\n No path mappings configured.")
return
print("\n Current Path Mappings:")
print(" " + "-" * 70)
for i, m in enumerate(mappings, 1):
status = "enabled" if m.get('enabled', True) else "DISABLED"
cacheable = "cacheable" if m.get('cacheable', True) else "non-cacheable"
print(f" {i}. {m.get('name', 'Unnamed')}")
print(f" Plex path: {m.get('plex_path', '')}")
print(f" Real path: {m.get('real_path', '')}")
if m.get('cacheable', True):
cache_path = m.get('cache_path') or 'Not set'
print(f" Cache path: {cache_path}")
print(f" Status: {status}, {cacheable}")
print()
def prompt_for_path_mapping(existing=None):
"""Prompt user to create or edit a path mapping."""
print("\n" + "-" * 60)
if existing:
print("EDIT PATH MAPPING")
print(f"Current name: {existing.get('name', '')}")
else:
print("ADD NEW PATH MAPPING")
print("-" * 60)
# Name
default_name = existing.get('name', '') if existing else ''
name = input(f"\nMapping name (e.g., 'Local Array', 'Remote NAS') [{default_name}]: ").strip()
if not name and default_name:
name = default_name
elif not name:
name = f"Mapping {1}"
# Plex path
default_plex = existing.get('plex_path', '') if existing else ''
print(f"\nPlex path: The path as seen by Plex (inside Docker container)")
print(f" Example: /data or /media")
plex_path = input(f"Plex path [{default_plex}]: ").strip()
if not plex_path and default_plex:
plex_path = default_plex
# Ensure trailing slash
if plex_path and not plex_path.endswith('/'):
plex_path = plex_path + '/'
# Real path
default_real = existing.get('real_path', '') if existing else ''
print(f"\nReal path: The actual filesystem path (on host/Unraid)")
print(f" Example: /mnt/user or /mnt/remotes/NAS")
real_path = input(f"Real path [{default_real}]: ").strip()
if not real_path and default_real:
real_path = default_real
# Ensure trailing slash
if real_path and not real_path.endswith('/'):
real_path = real_path + '/'
# Cacheable?
default_cacheable = existing.get('cacheable', True) if existing else True
default_cacheable_str = 'Y' if default_cacheable else 'N'
print(f"\nIs this path cacheable? (Set to No for remote/network storage)")
cacheable_input = input(f"Cacheable? [{'Y/n' if default_cacheable else 'y/N'}]: ").strip().lower()
if not cacheable_input:
cacheable = default_cacheable
else:
cacheable = cacheable_input in ['y', 'yes']
# Cache path (only if cacheable)
cache_path = None
if cacheable:
default_cache = existing.get('cache_path', '') if existing else ''
print(f"\nCache path: Where cached files are stored")
print(f" Example: /mnt/cache")
cache_path = input(f"Cache path [{default_cache}]: ").strip()
if not cache_path and default_cache:
cache_path = default_cache
# Ensure trailing slash
if cache_path and not cache_path.endswith('/'):
cache_path = cache_path + '/'
# Enabled?
default_enabled = existing.get('enabled', True) if existing else True
return {
'name': name,
'plex_path': plex_path,
'real_path': real_path,
'cache_path': cache_path,
'cacheable': cacheable,
'enabled': default_enabled
}
def configure_path_mappings(settings):
"""Interactive menu to configure multiple path mappings."""
mappings = settings.get('path_mappings', [])
# If no mappings but legacy settings exist, offer to convert
if not mappings and settings.get('plex_source') and settings.get('real_source'):
print("\n" + "=" * 60)
print("MULTI-PATH MAPPING CONFIGURATION")
print("=" * 60)
print("\nYou have legacy single-path settings configured:")
print(f" Plex source: {settings.get('plex_source')}")
print(f" Real source: {settings.get('real_source')}")
print(f" Cache dir: {settings.get('cache_dir')}")
convert = input("\nConvert to multi-path format? [Y/n]: ").strip().lower()
if convert in ['', 'y', 'yes']:
mappings = [{
'name': 'Primary',
'plex_path': settings.get('plex_source', ''),
'real_path': settings.get('real_source', ''),
'cache_path': settings.get('cache_dir', ''),
'cacheable': True,
'enabled': True
}]
print("Converted legacy settings to path mapping.")
while True:
print("\n" + "=" * 60)
print("PATH MAPPINGS MENU")
print("=" * 60)
display_path_mappings(mappings)
print(" Options:")
print(" [A] Add new path mapping")
if mappings:
print(" [E] Edit existing mapping")
print(" [D] Delete mapping")
print(" [T] Toggle enabled/disabled")
print(" [S] Save and return")
print()
choice = input("Select option: ").strip().lower()
if choice == 'a':
new_mapping = prompt_for_path_mapping()
if new_mapping.get('plex_path') and new_mapping.get('real_path'):
mappings.append(new_mapping)
print(f"\nAdded mapping: {new_mapping['name']}")
else:
print("\nMapping not added - plex_path and real_path are required.")
elif choice == 'e' and mappings:
try:
idx = int(input(f"Enter mapping number to edit (1-{len(mappings)}): ")) - 1
if 0 <= idx < len(mappings):
mappings[idx] = prompt_for_path_mapping(mappings[idx])
print(f"\nUpdated mapping: {mappings[idx]['name']}")
else:
print("Invalid selection.")
except ValueError:
print("Invalid input.")
elif choice == 'd' and mappings:
try:
idx = int(input(f"Enter mapping number to delete (1-{len(mappings)}): ")) - 1
if 0 <= idx < len(mappings):
removed = mappings.pop(idx)
print(f"\nDeleted mapping: {removed['name']}")
else:
print("Invalid selection.")
except ValueError:
print("Invalid input.")
elif choice == 't' and mappings:
try:
idx = int(input(f"Enter mapping number to toggle (1-{len(mappings)}): ")) - 1
if 0 <= idx < len(mappings):
mappings[idx]['enabled'] = not mappings[idx].get('enabled', True)
status = "enabled" if mappings[idx]['enabled'] else "disabled"
print(f"\nMapping '{mappings[idx]['name']}' is now {status}")
else:
print("Invalid selection.")
except ValueError:
print("Invalid input.")
elif choice == 's':
settings['path_mappings'] = mappings
print(f"\nSaved {len(mappings)} path mapping(s).")
return settings
else:
print("Invalid option. Please try again.")
# ---------------- Plex OAuth PIN Authentication ----------------
# PlexCache-R client identifier - stored in settings for consistency
PLEXCACHE_CLIENT_ID_KEY = 'plexcache_client_id'
PLEXCACHE_PRODUCT_NAME = 'PlexCache-R'
PLEXCACHE_PRODUCT_VERSION = '1.0'
def get_or_create_client_id(settings: dict) -> str:
"""Get existing client ID from settings or create a new one."""
if PLEXCACHE_CLIENT_ID_KEY in settings:
return settings[PLEXCACHE_CLIENT_ID_KEY]
# Generate new UUID for this installation
client_id = str(uuid.uuid4())
settings[PLEXCACHE_CLIENT_ID_KEY] = client_id
return client_id
def plex_oauth_authenticate(settings: dict, timeout_seconds: int = 300):
"""
Authenticate with Plex using the PIN-based OAuth flow.
This is the official Plex authentication method that provides a user-scoped token.
Workflow:
1. Generate a PIN via POST to plex.tv/api/v2/pins
2. User opens URL in browser and logs in
3. Script polls until token is returned or timeout
4. Returns the authentication token
Args:
settings: The settings dict (used to get/store client ID)
timeout_seconds: How long to wait for user to authenticate (default 5 min)
Returns:
Authentication token string, or None if failed/cancelled
"""
client_id = get_or_create_client_id(settings)
headers = {
'Accept': 'application/json',
'X-Plex-Product': PLEXCACHE_PRODUCT_NAME,
'X-Plex-Version': PLEXCACHE_PRODUCT_VERSION,
'X-Plex-Client-Identifier': client_id,
}
# Step 1: Request a PIN
print("\nRequesting authentication PIN from Plex...")
try:
response = requests.post(
'https://plex.tv/api/v2/pins',
headers=headers,
data={'strong': 'true'}, # Request a strong (long-lived) token
timeout=30
)
response.raise_for_status()
pin_data = response.json()
except requests.RequestException as e:
print(f"Error requesting PIN from Plex: {e}")
return None
pin_id = pin_data.get('id')
pin_code = pin_data.get('code')
if not pin_id or not pin_code:
print("Error: Invalid response from Plex PIN endpoint")
return None
# Step 2: Build the auth URL and prompt user
auth_url = f"https://app.plex.tv/auth#?clientID={client_id}&code={pin_code}&context%5Bdevice%5D%5Bproduct%5D={PLEXCACHE_PRODUCT_NAME}"
print("\n" + "=" * 70)
print("PLEX AUTHENTICATION")
print("=" * 70)
print("\nPlease open the following URL in your browser to authenticate:")
print(f"\n {auth_url}\n")
# Try to open browser automatically
try:
webbrowser.open(auth_url)
print("(A browser window should have opened automatically)")
except Exception:
print("(Could not open browser automatically - please copy the URL above)")
print("\nAfter logging in and clicking 'Allow', return here.")
print(f"Waiting for authentication (timeout: {timeout_seconds // 60} minutes)...")
print("=" * 70)
# Step 3: Poll for the token
poll_interval = 2 # seconds between polls
start_time = time.time()
while time.time() - start_time < timeout_seconds:
try:
response = requests.get(
f'https://plex.tv/api/v2/pins/{pin_id}',
headers=headers,
timeout=30
)
response.raise_for_status()
pin_status = response.json()
auth_token = pin_status.get('authToken')
if auth_token:
print("\nAuthentication successful!")
return auth_token
# Check if PIN expired
if pin_status.get('expiresAt'):
# PIN is still valid, keep polling
pass
except requests.RequestException as e:
print(f"\nWarning: Error checking PIN status: {e}")
# Continue polling despite transient errors
# Show progress indicator
elapsed = int(time.time() - start_time)
remaining = timeout_seconds - elapsed
print(f"\r Waiting... ({remaining}s remaining) ", end='', flush=True)
time.sleep(poll_interval)
print("\n\nAuthentication timed out. Please try again.")
return None
# ---------------- Setup Function ----------------
TOTAL_SETUP_STEPS = 5 # Connection, Libraries, Behavior, Users, Advanced
def print_step_header(step: int, total: int, title: str):
"""Print a formatted step header with progress indicator."""
print("\n" + "=" * 60)
print(f"STEP {step}/{total}: {title}")
print("=" * 60)
def _setup_plex_connection():
"""Step 1: Configure Plex connection and library paths.
Returns:
PlexServer instance on success, None on failure.
"""
global settings_data
print_step_header(1, TOTAL_SETUP_STEPS, "PLEX CONNECTION")
# Plex URL
while 'PLEX_URL' not in settings_data:
url = input('\nPlex server address [http://localhost:32400]: ').strip() or 'http://localhost:32400'
if is_valid_plex_url(url):
settings_data['PLEX_URL'] = url
print(f"✓ Plex URL: {url}")
else:
print("✗ Invalid URL format")
# Plex Token
plex = None
while 'PLEX_TOKEN' not in settings_data:
token = None
print("\nHow would you like to authenticate?")
print(" [1] Authenticate via Plex.tv (recommended)")
print(" [2] Enter token manually")
while token is None:
auth_choice = input("\nSelect option [1/2]: ").strip()
if auth_choice == '1':
token = plex_oauth_authenticate(settings_data)
if token is None:
print("\nAuthentication failed or was cancelled.")
retry = input("Try again or enter manually? [retry/manual]: ").strip().lower()
if retry == 'manual':
token = input('\nEnter your Plex token: ')
break
elif auth_choice == '2':
print("\nTo get your token:")
print(" 1. Open Plex Web App → Developer Tools (F12)")
print(" 2. Network tab → Find request to plex.tv")
print(" 3. Copy 'X-Plex-Token' from headers")
token = input('\nEnter your Plex token: ')
break
else:
print("Please enter 1 or 2")
if not token or not token.strip():
print("Token cannot be empty.")
continue
try:
plex = PlexServer(settings_data['PLEX_URL'], token)
user = plex.myPlexAccount().username
print(f"\n✓ Connected as: {user}")
settings_data['PLEX_TOKEN'] = token
print(f"✓ Plex platform: {plex.platform}")
except (BadRequest, requests.exceptions.RequestException) as e:
print(f'Unable to connect to Plex server. Error: {e}')
except ValueError as e:
print(f'Token is not valid. Error: {e}')
except TypeError as e:
print(f'An unexpected error occurred: {e}')
return plex
def _setup_library_paths(plex):
"""Step 2: Configure library selection and path mappings.
Args:
plex: PlexServer instance.
"""
global settings_data
print_step_header(2, TOTAL_SETUP_STEPS, "LIBRARY SELECTION")
libraries = plex.library.sections()
valid_sections = []
path_mappings = []
# Ask for cache root ONCE at the beginning
print("\nWhere is your cache drive located?")
print("(All cacheable libraries will use subdirectories here)")
default_cache = '/mnt/cache' if is_unraid() else '/mnt/cache'
cache_root = input(f"Cache drive path [{default_cache}]: ").strip() or default_cache
if not cache_root.endswith('/'):
cache_root = cache_root + '/'
print(f"\n✓ Cache root: {cache_root}")
print("\nNow select which libraries to include and configure their paths.")
print("For each library, you'll specify where files are actually stored.\n")
while not valid_sections:
for library in libraries:
# Get library locations
try:
locs = library.locations
if isinstance(locs, str):
locs = [locs]
except Exception as e:
print(f"\nWarning: Could not get locations for '{library.title}': {e}")
locs = []
print("-" * 60)
print(f"Library: {library.title}")
if locs:
print(f" Plex path: {locs[0]}" + (f" (+{len(locs)-1} more)" if len(locs) > 1 else ""))
include = input("Include? [Y/n] ") or 'yes'
if include.lower() in ['n', 'no']:
print(f" → Skipped\n")
continue
elif include.lower() in ['y', 'yes']:
if library.key not in valid_sections:
valid_sections.append(library.key)
# Collect path mappings for this library
if locs:
lib_mappings = prompt_library_path_mapping(
library.title,
locs,
cache_root
)
path_mappings.extend(lib_mappings)
print()
else:
print("Please enter yes or no")
if not valid_sections:
print("\n⚠ You must select at least one library. Please try again.\n")
settings_data['valid_sections'] = valid_sections
settings_data['path_mappings'] = path_mappings
settings_data['cache_dir'] = cache_root
# Show library summary
print("-" * 60)
print(f"✓ Configured {len(path_mappings)} library path(s)")
for m in path_mappings:
status = "cacheable" if m.get('cacheable', True) else "non-cacheable"
print(f" • {m['name']}: {status}")
def _setup_caching_behavior():
"""Step 3: Configure OnDeck and Watchlist settings."""
global settings_data
print_step_header(3, TOTAL_SETUP_STEPS, "CACHING BEHAVIOR")
# OnDeck Settings
if 'number_episodes' not in settings_data:
print("\n--- OnDeck Settings ---")
prompt_user_for_number('Episodes to fetch from OnDeck per show [6]: ', '6', 'number_episodes')
if 'days_to_monitor' not in settings_data:
prompt_user_for_number('Max age of OnDeck items in days [99]: ', '99', 'days_to_monitor')
# Watchlist Settings
if 'watchlist_toggle' not in settings_data:
print("\n--- Watchlist Settings ---")
watchlist = input('Fetch your own watchlist media? [Y/n] ') or 'yes'
if watchlist.lower() in ['n', 'no']:
settings_data['watchlist_toggle'] = False
settings_data['watchlist_episodes'] = 0
elif watchlist.lower() in ['y', 'yes']:
settings_data['watchlist_toggle'] = True
prompt_user_for_number('Episodes to fetch per watchlist show [3]: ', '3', 'watchlist_episodes')
else:
print("Please enter yes or no")
def _setup_users(plex):
"""Step 4: Configure user settings (OnDeck/Watchlist for other users).
Args:
plex: PlexServer instance.
"""
global settings_data
print_step_header(4, TOTAL_SETUP_STEPS, "USER CONFIGURATION")
while 'users_toggle' not in settings_data:
fetch_all_users = input('\nFetch OnDeck media from other Plex users? [Y/n] ') or 'yes'
if fetch_all_users.lower() not in ['y', 'yes', 'n', 'no']:
print("Please enter yes or no")
continue
if fetch_all_users.lower() in ['y', 'yes']:
settings_data['users_toggle'] = True
_configure_user_list(plex)
else:
settings_data['users_toggle'] = False
settings_data["skip_ondeck"] = []
settings_data["skip_watchlist"] = []
# Remote Watchlist RSS
if 'remote_watchlist_toggle' not in settings_data:
remote_watchlist = input('\nFetch watchlists from remote/friend users via RSS? [y/N] ') or 'no'
if remote_watchlist.lower() in ['n', 'no']:
settings_data['remote_watchlist_toggle'] = False
elif remote_watchlist.lower() in ['y', 'yes']:
settings_data['remote_watchlist_toggle'] = True
_configure_rss_feed()
def _configure_user_list(plex):
"""Helper: Build and configure the user list from Plex API."""
global settings_data
# Build the full user list (local + remote)
user_entries = []
local_users = []
remote_users = []
skipped_users = []
for user in plex.myPlexAccount().users():
name = user.title
user_id = getattr(user, "id", None)
user_uuid = None
thumb = getattr(user, "thumb", "")
if thumb and "/users/" in thumb:
try:
user_uuid = thumb.split("/users/")[1].split("/")[0]
except (IndexError, AttributeError):
pass
is_home = getattr(user, "home", False)
is_restricted = getattr(user, "restricted", False)
is_local = bool(is_home) or (is_restricted == "1" or is_restricted == 1 or is_restricted is True)
try:
token = user.get_token(plex.machineIdentifier)
except Exception:
skipped_users.append((name, "no server access"))
continue
if token is None:
skipped_users.append((name, "no token"))
continue
user_entry = {
"title": name,
"id": user_id,
"uuid": user_uuid,
"token": token,
"is_local": is_local,
"skip_ondeck": False,
"skip_watchlist": False
}
user_entries.append(user_entry)
if is_local:
local_users.append(name)
else:
remote_users.append(name)
# Display user summary
print(f"\nFound {len(user_entries)} accessible user(s):")
if local_users:
print(f" Local/Home ({len(local_users)}): {', '.join(local_users)}")
print(" → Can fetch OnDeck + Watchlist")
if remote_users:
print(f" Remote/Friends ({len(remote_users)}): {', '.join(remote_users)}")
print(" → OnDeck only (Watchlist via RSS)")
if skipped_users:
print(f" Skipped ({len(skipped_users)}):")
for name, reason in skipped_users:
print(f" • {name} ({reason})")
settings_data["users"] = user_entries
# Skip OnDeck configuration
if user_entries:
skip_users_choice = input('\nSkip OnDeck for specific users? [y/N] ') or 'no'
if skip_users_choice.lower() in ['y', 'yes']:
for u in settings_data["users"]:
answer = input(f' Skip OnDeck for {u["title"]}? [y/N] ') or 'no'
if answer.lower() in ['y', 'yes']:
u["skip_ondeck"] = True
# Skip Watchlist configuration (local users only)
local_user_entries = [u for u in settings_data["users"] if u["is_local"]]
if local_user_entries:
print("\nLocal users can have their watchlists fetched individually.")
for u in local_user_entries:
answer = input(f' Skip watchlist for {u["title"]}? [y/N] ') or 'no'
if answer.lower() in ['y', 'yes']:
u["skip_watchlist"] = True
# Build final skip lists
settings_data["skip_ondeck"] = [u["token"] for u in settings_data["users"] if u["skip_ondeck"]]
settings_data["skip_watchlist"] = [u["token"] for u in settings_data["users"] if u["is_local"] and u["skip_watchlist"]]
def _configure_rss_feed():
"""Helper: Configure and validate RSS feed URL."""
global settings_data
print("\nTo get the RSS feed URL:")
print(" 1. Go to https://app.plex.tv/desktop/#!/settings/watchlist")
print(" 2. Enable 'Friends Watchlist'")
print(" 3. Copy the generated RSS URL")
while True:
rss_url = input('\nEnter RSS URL: ').strip()
if not rss_url:
print("URL cannot be empty.")
continue
try:
response = requests.get(rss_url, timeout=10)
if response.status_code == 200 and b'<Error' not in response.content:
print("✓ RSS feed validated")
settings_data['remote_watchlist_rss_url'] = rss_url
break
else:
print("✗ Invalid RSS feed. Please check and try again.")
except requests.RequestException as e:
print(f"✗ Error accessing URL: {e}")
def _setup_advanced_settings():
"""Step 5: Configure advanced settings (retention, limits, etc.)."""
global settings_data
print_step_header(5, TOTAL_SETUP_STEPS, "ADVANCED SETTINGS")
# Watched Move
if 'watched_move' not in settings_data:
watched_move = input('Move watched media from cache back to array? [Y/n] ') or 'yes'
settings_data['watched_move'] = watched_move.lower() in ['y', 'yes']
# Cache Retention
if 'cache_retention_hours' not in settings_data:
print('\n--- Retention Settings ---')
print('Cache retention: How long to keep files on cache before moving back.')
print('(Protects against accidental unwatching or Plex glitches)')
prompt_user_for_duration('Cache retention in hours [12]: ', '12', 'cache_retention_hours')
# Watchlist Retention
if 'watchlist_retention_days' not in settings_data:
print('\nWatchlist retention: Auto-expire watchlist items after X days.')
print('(0 = keep forever while on watchlist)')
prompt_user_for_duration_days('Watchlist retention in days [0]: ', '0', 'watchlist_retention_days')
# Cache Size Limit
if 'cache_limit' not in settings_data:
print('\n--- Cache Limits ---')
print('Limit cache usage (e.g., 250GB, 50%, or empty for no limit)')
cache_limit = input('Cache size limit [no limit]: ').strip()
settings_data['cache_limit'] = cache_limit
# Smart Cache Eviction
if 'cache_eviction_mode' not in settings_data:
_configure_eviction_settings()
# Notification Level
if 'unraid_level' not in settings_data:
print('\n--- Unraid Notifications ---')
print(' summary - Notify after every run with files moved (default)')
print(' warning - Only notify on warnings and errors')
print(' error - Only notify on errors')
print(' blank - Disable notifications')
unraid_level = input('Notification level [summary]: ').strip().lower() or 'summary'
if unraid_level in ['disable', 'disabled', 'none', 'blank', '']:
unraid_level = ''
settings_data['unraid_level'] = unraid_level
# Legacy path configuration (skip if path_mappings already configured)
_setup_legacy_paths_if_needed()
# Active Session Handling
if 'exit_if_active_session' not in settings_data:
print('\n--- Playback Handling ---')
print('When someone is actively watching media:')
print(' No - Skip that file but continue processing others (default)')
print(' Yes - Exit completely and try again next run')
session = input('Exit if media is actively playing? [y/N] ') or 'no'
settings_data['exit_if_active_session'] = session.lower() in ['y', 'yes']
# Concurrent Moves
if 'max_concurrent_moves_cache' not in settings_data:
print('\n--- Performance ---')
prompt_user_for_number('Concurrent file moves (array→cache) [5]: ', '5', 'max_concurrent_moves_cache')
if 'max_concurrent_moves_array' not in settings_data:
prompt_user_for_number('Concurrent file moves (cache→array) [2]: ', '2', 'max_concurrent_moves_array')
# Debug/dry-run mode - default to off
if 'debug' not in settings_data:
settings_data['debug'] = False
def _configure_eviction_settings():
"""Helper: Configure cache eviction mode and thresholds."""
global settings_data
print('\nEviction mode when cache is full:')
print(' none - Skip new files (default)')
print(' smart - Evict lowest priority items')
print(' fifo - Evict oldest items first')
eviction_mode = input('Eviction mode [none]: ').strip().lower() or 'none'
if eviction_mode not in ['none', 'smart', 'fifo']:
eviction_mode = 'none'
settings_data['cache_eviction_mode'] = eviction_mode
if eviction_mode in ['smart', 'fifo']:
threshold = input('Eviction threshold % [90]: ').strip() or '90'
threshold = threshold.rstrip('%').strip()
try:
settings_data['cache_eviction_threshold_percent'] = int(threshold)
except ValueError:
print(f"Invalid number '{threshold}', using default 90")
settings_data['cache_eviction_threshold_percent'] = 90
if eviction_mode == 'smart':
min_pri = input('Min priority to evict (0-100) [60]: ').strip() or '60'
try:
settings_data['eviction_min_priority'] = int(min_pri)
except ValueError:
print(f"Invalid number '{min_pri}', using default 60")
settings_data['eviction_min_priority'] = 60