-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclix.sh
executable file
·1525 lines (1245 loc) · 53.6 KB
/
clix.sh
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
#!/bin/bash
# CLIX: A Plex Terminal Media Player
# Browse and play Plex media from the command line
#
# Developed by Jereme Hancock
# https://github.com/jeremehancock/CLIX
#
# MIT License
#
# Copyright (c) 2024 Jereme Hancock
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
###################
# Server Settings #
###################
PLEX_URL="http://localhost:32400"
PLEX_TOKEN=""
######################
# Directory Settings #
######################
DOWNLOAD_BASE_DIR="downloads"
MOVIES_DIR="${DOWNLOAD_BASE_DIR}/movies"
SHOWS_DIR="${DOWNLOAD_BASE_DIR}/shows"
MUSIC_DIR="${DOWNLOAD_BASE_DIR}/music"
CACHE_DIR="/tmp/clix_cache"
########################################################################################################
################################### DO NOT EDIT ANYTHING BELOW #########################################
########################################################################################################
VERSION="1.3.1"
create_download_dirs() {
mkdir -p "${MOVIES_DIR}"
mkdir -p "${SHOWS_DIR}"
mkdir -p "${MUSIC_DIR}"
mkdir -p "${CACHE_DIR}"
}
clear_cache() {
rm -rf "${CACHE_DIR}"/*
}
get_cache_key() {
local function_name="$1"
shift
local params="$*"
echo "${function_name}_${params}" | md5sum | cut -d' ' -f1
}
cache_get() {
local cache_key="$1"
local cache_file="${CACHE_DIR}/${cache_key}"
if [[ -f "$cache_file" ]]; then
local cache_time
cache_time=$(stat -c %Y "$cache_file")
local current_time
current_time=$(date +%s)
local age=$((current_time - cache_time))
# Cache expires after 1 hour
if [[ $age -lt 3600 ]]; then
cat "$cache_file"
return 0
fi
fi
return 1
}
cache_set() {
local cache_key="$1"
local cache_file="${CACHE_DIR}/${cache_key}"
cat > "$cache_file"
}
show_version() {
echo "CLIX v${VERSION}"
check_version
}
check_version() {
if ! command -v curl &> /dev/null; then
echo -e "Error: curl is required for version checking"
return 1
fi
local remote_version
remote_version=$(curl -s https://raw.githubusercontent.com/jeremehancock/CLIX/refs/heads/main/clix.sh | grep "^VERSION=" | cut -d'"' -f2)
if [[ -z "$remote_version" ]]; then
echo -e "Error: Could not fetch remote version"
return 1
fi
if [[ "$remote_version" > "$VERSION" ]]; then
clear_cache
echo -e "Update available: v$VERSION → v$remote_version"
echo -e "Use the Update option in the main menu or run with -u to update to the latest version"
return 0
fi
}
update_script() {
if ! command -v curl &> /dev/null; then
echo -e "Error: curl is required for updating"
return 1
fi
local remote_version
remote_version=$(curl -s https://raw.githubusercontent.com/jeremehancock/CLIX/refs/heads/main/clix.sh | grep "^VERSION=" | cut -d'"' -f2)
if [[ -z "$remote_version" ]]; then
echo -e "Error: Could not fetch remote version"
return 1
fi
if [[ "$remote_version" == "$VERSION" ]]; then
echo -e "No updates available. You are running the latest version (v${VERSION})."
return 0
fi
echo -e "Update available: v$VERSION → v$remote_version"
local backup_dir="backups"
mkdir -p "$backup_dir"
local script_name=$(basename "$0")
local backup_file="${backup_dir}/${script_name}.v${VERSION}.backup"
cp "$0" "$backup_file"
echo -n "Do you want to proceed with the update? [y/N] "
read -r response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
echo "Update cancelled."
return 0
fi
if curl -o "$script_name" -L https://raw.githubusercontent.com/jeremehancock/CLIX/main/clix.sh; then
clear_cache
local last_backup=$(ls -t "$backup_dir"/*.backup | head -n 1)
if [[ -n "$last_backup" ]]; then
local old_plex_url=$(grep "^PLEX_URL=" "$last_backup" | cut -d'"' -f2)
local old_plex_token=$(grep "^PLEX_TOKEN=" "$last_backup" | cut -d'"' -f2)
if [[ -n "$old_plex_url" ]]; then
sed -i "s|^PLEX_URL=.*|PLEX_URL=\"$old_plex_url\"|" "$script_name"
fi
if [[ -n "$old_plex_token" ]]; then
sed -i "s|^PLEX_TOKEN=.*|PLEX_TOKEN=\"$old_plex_token\"|" "$script_name"
fi
fi
chmod +x "$script_name"
echo -e "Successfully updated script"
echo -e "Previous version backed up to $backup_file"
exit 0
else
echo -e "Update failed"
mv "$backup_file" "$script_name"
return 1
fi
}
display_help() {
# Disable terminal echoing to prevent screen clearing
stty -echo
# Clear the screen and display help
clear
show_help | less -R
# Re-enable terminal echoing
stty echo
# Clear screen after help
clear
}
# The show_help function remains the same as it was originally in the script
show_help() {
cat << EOF
CLIX v${VERSION} - Guide
OPTIONS:
-h Show this help message
-v Show version information
-u Update to the latest version
NAVIGATION:
↑/↓ Move up/down in menus
Enter Select current item
ESC Go back to previous menu
Ctrl+C Exit the program or Exit from Music track
Type to search Fuzzy finding in any menu
MENU STRUCTURE:
1. Main Menu
- Movies
- TV Shows
- Music
- Downloads
- Update
- Help
- Quit
2. Library Selection
→ Select your preferred library
If there is only one library of the selected
library type it will be auto selected
3. Media Selection
Movies: Select movie from list
TV Shows: Select show → season → episode
Music: Select artist → album → track
DEPENDENCIES:
Required: curl, xmlstarlet, fzf, mpv, md5sum
Press q to return to main menu
EOF
}
check_dependencies() {
local deps=("curl" "xmlstarlet" "fzf" "mpv" "md5sum")
local missing=()
for dep in "${deps[@]}"; do
if ! command -v "$dep" >/dev/null 2>&1; then
missing+=("$dep")
fi
done
if [ ${#missing[@]} -ne 0 ]; then
echo "Missing required dependencies: ${missing[*]}"
echo "Please install them and try again."
exit 1
fi
}
check_plex_credentials() {
echo "Checking Plex server connection..."
if [ -z "$PLEX_URL" ] || [ -z "$PLEX_TOKEN" ]; then
echo "Error: Plex URL or token not set"
echo "Please edit this script and add your Plex credentials"
exit 1
fi
local basic_response
basic_response=$(curl -s -m 10 "${PLEX_URL}/identity")
if [[ -z "$basic_response" ]]; then
echo "Error: Could not connect to Plex server at ${PLEX_URL}"
echo "Please check if:"
echo "1. The Plex server URL is correct"
echo "2. The Plex server is running"
echo "3. Your network connection is working"
exit 1
fi
local auth_response
auth_response=$(curl -s -m 10 -H "X-Plex-Token: $PLEX_TOKEN" "${PLEX_URL}/library/sections")
if ! echo "$auth_response" | xmlstarlet sel -t -v "/MediaContainer" &>/dev/null; then
echo "Error: Invalid Plex token or unauthorized access"
echo "The server is reachable, but the provided token does not have proper access permissions"
echo "Please check your Plex token and try again"
exit 1
fi
local server_info
server_info=$(curl -s -m 10 -H "X-Plex-Token: $PLEX_TOKEN" "${PLEX_URL}/")
local server_name
server_name=$(echo "$server_info" | xmlstarlet sel -t -v "/MediaContainer/@friendlyName" 2>/dev/null)
if [[ -z "$server_name" ]]; then
server_name=$(echo "$server_info" | xmlstarlet sel -t -v "/MediaContainer/@title" 2>/dev/null || echo "Unknown")
fi
local library_count
library_count=$(echo "$auth_response" | xmlstarlet sel -t -v "count(/MediaContainer/Directory)")
echo "Successfully connected to Plex server: $server_name"
echo "Found $library_count available libraries"
sleep 2
}
downloads_menu() {
while true; do
local choice
choice=$(echo -e "Movies\nTV Shows\nMusic" | fzf --reverse --header="Downloads Menu" --prompt="Search Downloads > ")
case "$choice" in
Movies) list_downloaded_movies ;;
"TV Shows") list_downloaded_shows ;;
Music) list_downloaded_music ;;
"") break ;;
esac
done
}
list_downloaded_movies() {
if [ ! -d "$MOVIES_DIR" ] || [ -z "$(ls -A "$MOVIES_DIR")" ]; then
echo -e "< Go back" | fzf --reverse --header="No downloaded movies found" --disabled
clear
return
fi
while true; do
local movies
movies=$(find "$MOVIES_DIR" -type f -exec basename {} \; | sort -k2,2)
local display_movies=""
declare -A filename_map
while IFS= read -r movie; do
local display_name="${movie%.*}"
# Convert asterisks back to slashes for display
display_name=$(echo "$display_name" | tr '*' '/')
display_movies+="$display_name"$'\n'
filename_map["$display_name"]="$movie"
done <<< "$movies"
local chosen_display
chosen_display=$(echo -e "$display_movies" | sed '/^$/d' | fzf --reverse --header="Select Downloaded Movie" --prompt="Search Downloaded Movies > ")
if [[ -z "$chosen_display" ]]; then
break
fi
local movie_file="${filename_map[$chosen_display]}"
mpv --title="$chosen_display" "${MOVIES_DIR}/${movie_file}"
clear
done
}
list_downloaded_shows() {
if [ ! -d "$SHOWS_DIR" ] || [ -z "$(ls -A "$SHOWS_DIR")" ]; then
echo -e "< Go back" | fzf --reverse --header="No downloaded TV shows found" --disabled
clear
return
fi
while true; do
local shows
shows=$(find "$SHOWS_DIR" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | sort -k2,2)
# Convert asterisks to slashes for display
local display_shows=""
declare -A show_map
while IFS= read -r show; do
local display_name=$(echo "$show" | tr '*' '/')
display_shows+="$display_name"$'\n'
show_map["$display_name"]="$show"
done <<< "$shows"
local chosen_show_display
chosen_show_display=$(echo -e "$display_shows" | sed '/^$/d' | fzf --reverse --header="Select Downloaded TV Show" --prompt="Search Downloaded TV Shows > ")
if [[ -z "$chosen_show_display" ]]; then
break
fi
local original_show="${show_map[$chosen_show_display]}"
while true; do
local seasons
seasons=$(find "$SHOWS_DIR/$original_show" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | sort -V)
if [[ -z "$seasons" ]]; then
echo -e "< Go back" | fzf --reverse --header="No seasons found" --disabled
clear
break
fi
local display_seasons=""
declare -A season_map
while IFS= read -r season; do
local display_name=$(echo "$season" | tr '*' '/')
display_seasons+="$display_name"$'\n'
season_map["$display_name"]="$season"
done <<< "$seasons"
local chosen_season_display
chosen_season_display=$(echo -e "$display_seasons" | sed '/^$/d' | fzf --reverse --header="TV Show: $chosen_show_display
Select Downloaded Season" --prompt="Search Downloaded Seasons > ")
if [[ -z "$chosen_season_display" ]]; then
break
fi
local original_season="${season_map[$chosen_season_display]}"
while true; do
local episodes
episodes=$(find "$SHOWS_DIR/$original_show/$original_season" -type f -exec basename {} \; | sort -V)
if [[ -z "$episodes" ]]; then
echo -e "< Go back" | fzf --reverse --header="No episodes found" --disabled
clear
break
fi
local display_episodes=""
declare -A filename_map
while IFS= read -r episode; do
local base_name="${episode%.*}"
# Extract episode number and title
if [[ $base_name =~ S[0-9]+E([0-9]+)[[:space:]]-[[:space:]](.+)$ ]]; then
local ep_num="${BASH_REMATCH[1]}"
local ep_title="${BASH_REMATCH[2]}"
# Remove leading zeros from episode number
ep_num=$((10#$ep_num))
# Create simple display format
local display_name="${ep_num}. ${ep_title}"
display_episodes+="$display_name"$'\n'
filename_map["$display_name"]="$episode"
else
# Fallback to full name if pattern doesn't match
local display_name=$(echo "$base_name" | tr '*' '/')
display_episodes+="$display_name"$'\n'
filename_map["$display_name"]="$episode"
fi
done <<< "$episodes"
local chosen_display
chosen_display=$(echo -e "$display_episodes" | sed '/^$/d' | fzf --reverse --header="TV Show: $chosen_show_display
Season: $chosen_season_display
Select Downloaded Episode" --prompt="Search Downloaded Episodes > ")
if [[ -z "$chosen_display" ]]; then
break
fi
local episode_file="${filename_map[$chosen_display]}"
# Extract episode number and title from filename
if [[ $episode_file =~ S([0-9]{2})E([0-9]{2})[[:space:]]-[[:space:]](.+)\..+ ]]; then
local season_num="${BASH_REMATCH[1]}"
local episode_num="${BASH_REMATCH[2]}"
local episode_title="${BASH_REMATCH[3]}"
local display_title="${chosen_show_display} - S${season_num}E${episode_num} - ${episode_title}"
mpv --title="$display_title" "${SHOWS_DIR}/${original_show}/${original_season}/${episode_file}"
else
mpv --title="$chosen_show_display - $chosen_season_display - $chosen_display" "${SHOWS_DIR}/${original_show}/${original_season}/${episode_file}"
fi
clear
done
done
done
}
list_downloaded_music() {
if [ ! -d "$MUSIC_DIR" ] || [ -z "$(ls -A "$MUSIC_DIR")" ]; then
echo -e "< Go back" | fzf --reverse --header="No downloaded music found" --disabled
clear
return
fi
while true; do
local artists
artists=$(find "$MUSIC_DIR" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | sort)
local display_artists=""
declare -A artist_map
while IFS= read -r artist; do
# Convert asterisks to slashes for display
local display_name=$(echo "$artist" | tr '*' '/')
display_artists+="$display_name"$'\n'
artist_map["$display_name"]="$artist"
done <<< "$artists"
local chosen_artist_display
chosen_artist_display=$(echo -e "$display_artists" | sed '/^$/d' | fzf --reverse --header="Select Downloaded Artist" --prompt="Search Downloaded Artists > ")
if [[ -z "$chosen_artist_display" ]]; then
break
fi
local original_artist="${artist_map[$chosen_artist_display]}"
while true; do
local albums
albums=$(find "$MUSIC_DIR/$original_artist" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | sort)
if [[ -z "$albums" ]]; then
echo -e "< Go back" | fzf --reverse --header="No albums found" --disabled
clear
break
fi
local display_albums=""
declare -A album_map
while IFS= read -r album; do
# Convert asterisks to slashes for display
local display_name=$(echo "$album" | tr '*' '/')
display_albums+="$display_name"$'\n'
album_map["$display_name"]="$album"
done <<< "$albums"
local chosen_album_display
chosen_album_display=$(echo -e "$display_albums" | sed '/^$/d' | fzf --reverse --header="Artist: $chosen_artist_display
Select Downloaded Album" --prompt="Search Downloaded Albums > ")
if [[ -z "$chosen_album_display" ]]; then
break
fi
local original_album="${album_map[$chosen_album_display]}"
while true; do
local tracks
tracks=$(find "$MUSIC_DIR/$original_artist/$original_album" -type f -exec basename {} \; | sort -V)
if [[ -z "$tracks" ]]; then
echo -e "< Go back" | fzf --reverse --header="No tracks found" --disabled
clear
break
fi
local display_tracks=""
declare -A track_map
while IFS= read -r track; do
local base_name="${track%.*}"
# Extract track number and title
if [[ $base_name =~ -[[:space:]]([0-9]+)[[:space:]]-[[:space:]](.+)$ ]]; then
local track_num="${BASH_REMATCH[1]}"
local track_title="${BASH_REMATCH[2]}"
# Remove leading zeros from track number
track_num=$((10#$track_num))
# Create simple display format
local display_name="${track_num}. ${track_title}"
display_tracks+="$display_name"$'\n'
track_map["$display_name"]="$track"
else
# Fallback to full name if pattern doesn't match
local display_name=$(echo "$base_name" | tr '*' '/')
display_tracks+="$display_name"$'\n'
track_map["$display_name"]="$track"
fi
done <<< "$tracks"
local chosen_track_display
chosen_track_display=$(echo -e "$display_tracks" | sed '/^$/d' | fzf --reverse --header="Artist: $chosen_artist_display
Album: $chosen_album_display
Select Downloaded Track" --prompt="Search Downloaded Tracks > ")
if [[ -z "$chosen_track_display" ]]; then
break
fi
local track_file="${track_map[$chosen_track_display]}"
mpv --title="$chosen_artist_display - $chosen_album_display - $chosen_track_display" "${MUSIC_DIR}/${original_artist}/${original_album}/${track_file}"
clear
done
done
done
}
get_libraries() {
local cache_key
cache_key=$(get_cache_key "get_libraries")
local cached_data
if cached_data=$(cache_get "$cache_key"); then
echo "$cached_data"
return
fi # Changed from } to fi
local response
response=$(curl -s -H "X-Plex-Token: $PLEX_TOKEN" "${PLEX_URL}/library/sections")
if [[ -z "$response" ]]; then
echo "Error: No response from Plex server."
exit 1
fi
local result
result=$(echo "$response" | xmlstarlet sel -t -m "//Directory" -v "concat(@key, '|', @title, '|', @type)" -n | sed 's/&/\&/g')
echo "$result" | cache_set "$cache_key"
echo "$result"
}
get_library_contents() {
local library_key="$1"
local cache_key
cache_key=$(get_cache_key "get_library_contents" "$library_key")
local cached_data
if cached_data=$(cache_get "$cache_key"); then
echo "$cached_data"
return
fi # Changed from } to fi
local page=1
local page_size=50
local all_items=""
local libraries
libraries=$(get_libraries | grep "|${library_key}|")
local library_name
library_name=$(echo "$libraries" | cut -d'|' -f2)
local total_size
local first_response
first_response=$(curl -s -H "X-Plex-Token: $PLEX_TOKEN" "${PLEX_URL}/library/sections/${library_key}/all?X-Plex-Container-Start=0&X-Plex-Container-Size=1")
total_size=$(echo "$first_response" | xmlstarlet sel -t -v "/MediaContainer/@totalSize" -n)
if [[ "$total_size" -eq 0 ]]; then
echo "EMPTY_LIBRARY"
return 0
fi
local first_item_type
first_item_type=$(echo "$first_response" | xmlstarlet sel -t -m "//Video | //Directory" -v "name()" -n | head -n 1)
if [[ -z "$first_item_type" ]]; then
echo "EMPTY_LIBRARY"
return 0
fi
echo "Retrieving contents of library: $library_name" >&2
echo "Total items: $total_size" >&2
clear >&2
while true; do
local start_index=$((($page - 1) * $page_size))
local response
response=$(curl -s -H "X-Plex-Token: $PLEX_TOKEN" "${PLEX_URL}/library/sections/${library_key}/all?X-Plex-Container-Start=${start_index}&X-Plex-Container-Size=${page_size}")
if [[ -z "$response" ]]; then
echo "EMPTY_LIBRARY"
return 0
fi
local current_items
if [[ "$first_item_type" == "Video" ]]; then
current_items=$(echo "$response" | xmlstarlet sel -t -m "//Video" -v "concat(@title, ' (', @year, ')|', @ratingKey)" -n | sed 's/&/\&/g')
elif [[ "$first_item_type" == "Directory" ]]; then
current_items=$(echo "$response" | xmlstarlet sel -t -m "//Directory" -v "concat(@title, '|', @ratingKey)" -n | sed 's/&/\&/g')
else
echo "EMPTY_LIBRARY"
return 0
fi
if [[ -z "$current_items" ]]; then
break
fi
all_items+="$current_items"$'\n'
local current_count=$((page * page_size))
if [[ $current_count -gt "$total_size" ]]; then
current_count="$total_size"
fi
local progress_percent=$((current_count * 100 / total_size))
printf "\rRetrieving items: [%-50s] %d%% (%d/%d)" \
"$(printf "#%.0s" $(seq 1 $((progress_percent / 2))))" \
"$progress_percent" "$current_count" "$total_size" >&2
if [[ $((page * page_size)) -ge "$total_size" ]]; then
break
fi
((page++))
done
echo "" >&2
clear >&2
echo "$all_items" | sed '/^$/d' | tee >(cache_set "$cache_key")
}
get_stream_url() {
local media_key="$1"
local media_type="$2"
local response
response=$(curl -s -H "X-Plex-Token: $PLEX_TOKEN" "${PLEX_URL}/library/metadata/${media_key}")
if [[ -z "$response" ]]; then
echo "Error: No response from Plex server."
return 1
fi
local stream_url
stream_url=$(echo "$response" | xmlstarlet sel -t -m "//Part" -v "@key" -n)
if [[ -n "$stream_url" ]]; then
echo "${PLEX_URL}${stream_url}?X-Plex-Token=${PLEX_TOKEN}"
else
echo "Error: Could not retrieve stream URL."
return 1
fi
}
get_albums() {
local artist_key="$1"
local cache_key
cache_key=$(get_cache_key "get_albums" "$artist_key")
local cached_data
if cached_data=$(cache_get "$cache_key"); then
echo "$cached_data"
return
fi
local response
response=$(curl -s -H "X-Plex-Token: $PLEX_TOKEN" "${PLEX_URL}/library/metadata/${artist_key}/children")
if [[ -z "$response" ]]; then
echo "Error: No response from Plex server."
exit 1
fi
echo "$response" | xmlstarlet sel -t -m "//Directory" -v "concat(@title, '|', @ratingKey)" -n |
sed 's/&/\&/g' | tee >(cache_set "$cache_key")
}
get_tracks() {
local album_key="$1"
local cache_key
cache_key=$(get_cache_key "get_tracks" "$album_key")
local cached_data
if cached_data=$(cache_get "$cache_key"); then
echo "$cached_data"
return
fi
local response
response=$(curl -s -H "X-Plex-Token: $PLEX_TOKEN" "${PLEX_URL}/library/metadata/${album_key}/children")
if [[ -z "$response" ]]; then
echo "Error: No response from Plex server."
exit 1
fi
echo "$response" | xmlstarlet sel -t -m "//Track" -v "concat(@index, '. ', @title, '|', @ratingKey)" -n |
sed 's/&/\&/g' | tee >(cache_set "$cache_key")
}
get_seasons() {
local show_key="$1"
local cache_key
cache_key=$(get_cache_key "get_seasons" "$show_key")
local cached_data
if cached_data=$(cache_get "$cache_key"); then
echo "$cached_data"
return
fi
local response
response=$(curl -s -H "X-Plex-Token: $PLEX_TOKEN" "${PLEX_URL}/library/metadata/${show_key}/children")
if [[ -z "$response" ]]; then
echo "Error: No response from Plex server."
exit 1
fi
echo "$response" | xmlstarlet sel -t -m "//Directory[@type='season']" -v "@title" -o "|" -v "@ratingKey" -n | \
grep -v "^All episodes|" | sort -V | sed 's/&/\&/g' | tee >(cache_set "$cache_key")
}
get_episodes() {
local season_key="$1"
local cache_key
cache_key=$(get_cache_key "get_episodes" "$season_key")
local cached_data
if cached_data=$(cache_get "$cache_key"); then
echo "$cached_data"
return
fi
local response
response=$(curl -s -H "X-Plex-Token: $PLEX_TOKEN" "${PLEX_URL}/library/metadata/${season_key}/children")
if [[ -z "$response" ]]; then
echo "Error: No response from Plex server."
exit 1
fi
echo "$response" | xmlstarlet sel -t -m "//Video" -v "concat(@index, '. ', @title, '|', @ratingKey)" -n |
sed 's/&/\&/g' | tee >(cache_set "$cache_key")
}
play_media() {
local media_key="$1"
local media_type="$2"
local title="$3"
local media_url
media_url=$(get_stream_url "$media_key" "$media_type")
if [[ -n "$media_url" ]]; then
echo "Playing $media_type: $title"
mpv --title="$title" "$media_url"
clear
return 0
else
echo "Error: Could not retrieve stream URL."
read -p "Press Enter to continue..."
return 1
fi
}
download_media() {
local media_key="$1"
local media_type="$2"
local title="$3"
local additional_path="$4"
echo -n "Do you want to proceed with the download? [y/N] "
read -r response
clear
if [[ ! "$response" =~ ^[Yy]$ ]]; then
echo "Download cancelled."
read -p "Press Enter to continue..."
return 0
fi
local media_url
media_url=$(get_stream_url "$media_key" "$media_type")
if [[ -z "$media_url" ]]; then
echo "Error: Could not retrieve download URL."
read -p "Press Enter to continue..."
return 1
fi
local original_ext
original_ext=$(echo "$media_url" | grep -oP '\.[^./?]+(?=(\?|$))' || echo ".mp4")
local response
response=$(curl -s -H "X-Plex-Token: $PLEX_TOKEN" "${PLEX_URL}/library/metadata/${media_key}")
local display_filename
local safe_filename
local relative_path
case "$media_type" in
movie)
local clean_title
clean_title=$(echo "$title" | sed -E 's/ \([0-9]{4}\)$//')
local year
year=$(echo "$response" | xmlstarlet sel -t -v "//Video/@year" 2>/dev/null)
# Keep original title for display
if [[ -n "$year" ]]; then
display_filename="${clean_title} (${year})${original_ext}"
else
display_filename="${clean_title}${original_ext}"
fi
# Create safe version for filesystem
safe_filename=$(echo "$display_filename" | tr '/' '*')
;;
episode)
local show_title=$(echo "$response" | xmlstarlet sel -t -v "//Video/@grandparentTitle" 2>/dev/null)
local season_num=$(echo "$response" | xmlstarlet sel -t -v "//Video/@parentIndex" 2>/dev/null)
local episode_num=$(echo "$response" | xmlstarlet sel -t -v "//Video/@index" 2>/dev/null)
local episode_title=$(echo "$response" | xmlstarlet sel -t -v "//Video/@title" 2>/dev/null)
season_num=$(printf "%02d" "$season_num")
episode_num=$(printf "%02d" "$episode_num")
local season_folder=$(echo "$response" | xmlstarlet sel -t -v "//Video/@parentTitle" 2>/dev/null)
# Create display versions with slashes
display_filename="${show_title} - S${season_num}E${episode_num} - ${episode_title}${original_ext}"
# Create safe versions with asterisks for filesystem
local show_title_safe=$(echo "$show_title" | tr '/' '*')
local season_folder_safe=$(echo "$season_folder" | tr '/' '*')
local episode_title_safe=$(echo "$episode_title" | tr '/' '*')
relative_path="${show_title_safe}/${season_folder_safe}"
safe_filename="${show_title_safe} - S${season_num}E${episode_num} - ${episode_title_safe}${original_ext}"
;;
music)
local artist=$(echo "$response" | xmlstarlet sel -t -v "//Track/@grandparentTitle" 2>/dev/null)
local album=$(echo "$response" | xmlstarlet sel -t -v "//Track/@parentTitle" 2>/dev/null)
local track_num=$(echo "$response" | xmlstarlet sel -t -v "//Track/@index" 2>/dev/null)
local track_title=$(echo "$response" | xmlstarlet sel -t -v "//Track/@title" 2>/dev/null)
track_num=$(printf "%02d" "$track_num")
# Create display version with slashes
display_filename="${artist} - ${album} - ${track_num} - ${track_title}${original_ext}"
# Create safe versions with asterisks for filesystem
local artist_safe=$(echo "$artist" | tr '/' '*')
local album_safe=$(echo "$album" | tr '/' '*')
local track_title_safe=$(echo "$track_title" | tr '/' '*')
relative_path="${artist_safe}/${album_safe}"
safe_filename="${artist_safe} - ${album_safe} - ${track_num} - ${track_title_safe}${original_ext}"
;;
*)
echo "Unsupported media type for download"
return 1
;;
esac
# Replace & with & in filenames and paths for downloads
display_filename=$(echo "$display_filename" | sed 's/&/\&/g' | tr -d '\"' | tr ':' '-')
safe_filename=$(echo "$safe_filename" | sed 's/&/\&/g' | tr -d '\"' | tr ':' '-')
[[ -n "$relative_path" ]] && relative_path=$(echo "$relative_path" | sed 's/&/\&/g' | tr -d '\"' | tr ':' '-')
local target_dir
case "$media_type" in
movie)
target_dir="${MOVIES_DIR}"
;;
episode)
target_dir="${SHOWS_DIR}/${relative_path}"
;;
music)
target_dir="${MUSIC_DIR}/${relative_path}"
;;
esac
mkdir -p "$target_dir"
echo "Downloading: ${display_filename}"
echo "Destination: ${target_dir}/${safe_filename}"
if curl -# -L \
-H "X-Plex-Token: $PLEX_TOKEN" \
--progress-bar \
-o "${target_dir}/${safe_filename}" \
"$media_url"; then
echo "Download completed successfully!"
else
echo "Download failed!"
return 1
fi
read -p "Press Enter to continue..."
}
check_local_file() {
local media_type="$1"
local title="$2"
local additional_path="$3"
local search_dir
local found_file=""
case "$media_type" in
episode)
local base_search_dir="${SHOWS_DIR}"
# Replace forward slashes with asterisks in the path components
local processed_path=""
IFS='/' read -ra path_parts <<< "$additional_path"
for part in "${path_parts[@]}"; do
local processed_part=$(echo "$part" | tr '/' '*' | sed 's/&/\&/g' | tr -d '\"' | tr ':' '-')
if [ -z "$processed_path" ]; then
processed_path="$processed_part"
else
processed_path="$processed_path/$processed_part"
fi
done
search_dir="${base_search_dir}/${processed_path}"
# Convert title to use asterisks for filesystem matching
local safe_title=$(echo "$title" | tr '/' '*')
if [[ -d "$search_dir" ]]; then
if [[ "$safe_title" =~ ^(.+)[[:space:]]-[[:space:]]S([0-9]+)E([0-9]+)[[:space:]]-[[:space:]] ]]; then
while IFS= read -r -d $'\0' file; do