-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathppm
More file actions
executable file
·464 lines (392 loc) · 12.8 KB
/
ppm
File metadata and controls
executable file
·464 lines (392 loc) · 12.8 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
#!/bin/bash
# Persistent Playlist Manager - M3U8 Playlist Manager
# This script manages persistent playlists using m3u8 files and hard links
#
# Kaya-Sem
# Version 1.0
# Configuration
DATA_DIR="$HOME/Music/PPM"
ALBUMS_DIR="$DATA_DIR/playlists"
MYPLAYCOMMAND="mpv --shuffle"
# Initialize the data directory structure
init_directories() {
if [ ! -d "$ALBUMS_DIR" ]; then
mkdir -p "$ALBUMS_DIR"
fi
}
# Display usage information
show_usage() {
local script_name="$(basename "$0")"
cat >&2 << EOF
Usage: $script_name <command> [options]
Commands:
list List all playlists
tracks <playlist> List songs in a playlist
add <playlist> [song1] [song2] ... Add songs to a playlist
remove <playlist> <song1> [song2] ... Remove songs from a playlist
create [playlist1] [playlist2] ... Create new playlists
delete <playlist> Delete a playlist
play <playlist> Play a playlist using the configured player
Examples:
$script_name list
$script_name tracks 'My Favorites'
$script_name add 'Rock Hits' /path/to/song1.flac /path/to/song2.mp3
$script_name remove 'Rock Hits' song1.mp3 song2.mp3
$script_name create 'Rock Hits' 'My Favorites'
$script_name create < playlist_names.txt
$script_name add 'Rock Hits' <<< 'song1.mp3'
$script_name play 'Rock Hits'
EOF
}
# Check if playlist exists
playlist_exists() {
local playlist_name="$1"
[ -d "$ALBUMS_DIR/$playlist_name" ] && [ -f "$ALBUMS_DIR/$playlist_name/$playlist_name.m3u8" ]
}
# Check if song exists in playlist
song_in_playlist() {
local playlist_name="$1"
local song_name="$2"
local playlist_file="$ALBUMS_DIR/$playlist_name/$playlist_name.m3u8"
if [ -f "$playlist_file" ]; then
grep -q "^#EXTINF:.*$song_name$" "$playlist_file" 2>/dev/null
else
return 1
fi
}
# Get absolute path of a file
get_absolute_path() {
local file_path="$1"
if [[ "$file_path" == /* ]]; then
echo "$file_path"
else
echo "$(pwd)/$file_path"
fi
}
# Validate song file
validate_song_file() {
local song_path="$1"
local abs_path=$(get_absolute_path "$song_path")
if [ ! -f "$abs_path" ]; then
echo -e "track file not found: $song_path" >&2
return 1
fi
# Check if it's an audio file (basic check)
local extension="${abs_path##*.}"
case "${extension,,}" in
mp3|m4a|flac|wav|ogg|aac|wma)
return 0
;;
*)
echo -e "$song_path may not be an audio file" >&2
return 0
;;
esac
}
# List all playlists
list_playlists() {
if [ ! -d "$ALBUMS_DIR" ] || [ -z "$(ls -A "$ALBUMS_DIR" 2>/dev/null)" ]; then
echo "No playlists found." >&2
return
fi
local count=0
for playlist_dir in "$ALBUMS_DIR"/*/; do
if [ -d "$playlist_dir" ]; then
local playlist_name=$(basename "$playlist_dir")
local playlist_file="$playlist_dir$playlist_name.m3u8"
if [ -f "$playlist_file" ]; then
local song_count=$(grep -c "^#EXTINF:" "$playlist_file" 2>/dev/null)
echo -e "$playlist_name ($song_count tracks)"
((count++))
fi
fi
done
if [ $count -eq 0 ]; then
echo "No playlists found."
fi
}
# List songs in a playlist
list_songs() {
local playlist_name="$1"
if [ -z "$playlist_name" ]; then
echo -e "${RED}Error:${NC} Playlist name is required"
echo "Usage: $(basename "$0") list-songs <playlist_name>"
exit 1
fi
local playlist_file="$ALBUMS_DIR/$playlist_name/$playlist_name.m3u8"
if [ ! -f "$playlist_file" ]; then
echo -e "playlist '$playlist_name' not found" >&2
exit 1
fi
local count=0
while IFS= read -r line; do
if [[ "$line" =~ ^#EXTINF: ]]; then
# Extract song name from EXTINF line
local song_name=$(echo "$line" | sed 's/^#EXTINF:.*,//')
echo -e "$song_name"
((count++))
fi
done < "$playlist_file"
if [ $count -eq 0 ]; then
echo "No songs in this playlist."
fi
}
# Create new playlists
create_playlists() {
local playlists=()
# If arguments provided, use them
if [ $# -gt 0 ]; then
playlists=("$@")
else
# Read from STDIN
while IFS= read -r line; do
# Skip empty lines and comments
if [[ -n "$line" && ! "$line" =~ ^[[:space:]]*# ]]; then
playlists+=("$line")
fi
done
fi
if [ ${#playlists[@]} -eq 0 ]; then
echo -e "no playlist names provided" >&2
echo "Usage: $(basename "$0") create [playlist1] [playlist2] ..." >&2
echo " echo 'playlist1' | $(basename "$0") create" >&2
exit 1
fi
local created_count=0
local skipped_count=0
for playlist_name in "${playlists[@]}"; do
# Trim whitespace
playlist_name=$(echo "$playlist_name" | xargs)
if [ -z "$playlist_name" ]; then
continue
fi
if playlist_exists "$playlist_name"; then
echo -e "'$playlist_name' already exists" >&2
((skipped_count++))
continue
fi
# Create playlist directory
local playlist_dir="$ALBUMS_DIR/$playlist_name"
mkdir -p "$playlist_dir"
# Create m3u8 file with header
cat > "$playlist_dir/$playlist_name.m3u8" << EOF
#EXTM3U
# Created by Persistent Playlist Manager
# Playlist: $playlist_name
# Created: $(date)
EOF
done
}
# Add songs to a playlist
add_songs() {
local playlist_name="$1"
shift
if [ -z "$playlist_name" ]; then
echo -e "playlist name is required" >&2
echo "Usage: $(basename "$0") add <playlist_name> [song1] [song2] ..." >&2
echo " echo 'song1.mp3' | $(basename "$0") add <playlist_name>" >&2
exit 1
fi
local songs=()
# If arguments provided, use them
if [ $# -gt 0 ]; then
songs=("$@")
else
# Read from STDIN
while IFS= read -r line; do
# Skip empty lines and comments
if [[ -n "$line" && ! "$line" =~ ^[[:space:]]*# ]]; then
songs+=("$line")
fi
done
fi
if [ ${#songs[@]} -eq 0 ]; then
echo -e "No tracks provided" >&2
echo "Usage: $(basename "$0") add <playlist_name> [song1] [song2] ..." >&2
echo " echo 'song1.mp3' | $(basename "$0") add <playlist_name>" >&2
exit 1
fi
# Create playlist if it doesn't exist
if ! playlist_exists "$playlist_name"; then
create_playlists "$playlist_name"
fi
local playlist_dir="$ALBUMS_DIR/$playlist_name"
local playlist_file="$playlist_dir/$playlist_name.m3u8"
local added_count=0
local skipped_count=0
for song_path in "${songs[@]}"; do
# Trim whitespace
song_path=$(echo "$song_path" | xargs)
if [ -z "$song_path" ]; then
continue
fi
if ! validate_song_file "$song_path"; then
continue
fi
local abs_path=$(get_absolute_path "$song_path")
local song_name=$(basename "$song_path")
# Check if song is already in playlist
if song_in_playlist "$playlist_name" "$song_name"; then
echo -e "'$song_name' already in playlist '$playlist_name'" >&2
((skipped_count++))
continue
fi
# Create hard link in playlist directory
local link_path="$playlist_dir/$song_name"
# Handle duplicate filenames
local counter=1
local original_name="$song_name"
while [ -f "$link_path" ]; do
local name_without_ext="${original_name%.*}"
local extension="${original_name##*.}"
song_name="${name_without_ext}_$counter.$extension"
link_path="$playlist_dir/$song_name"
((counter++))
done
# Create hard link
if ! ln "$abs_path" "$link_path" 2>/dev/null; then
echo -e "Failed to add '$song_name' to playlist '$playlist_name'" >&2
else
# Add to m3u8 file
echo "#EXTINF:-1,$song_name" >> "$playlist_file"
echo "$link_path" >> "$playlist_file"
fi
done
}
# Remove songs from a playlist
remove_songs() {
local playlist_name="$1"
shift
if [ -z "$playlist_name" ]; then
echo -e "playlist name is required" >&2
echo "Usage: $(basename "$0") remove <playlist> <song1> [song2] ..." >&2
exit 1
fi
if [ $# -eq 0 ]; then
echo -e "at least one track is required" >&2
echo "Usage: $(basename "$0") remove <playlist> <song1> [song2] ..." >&2
exit 1
fi
local playlist_dir="$ALBUMS_DIR/$playlist_name"
local playlist_file="$playlist_dir/$playlist_name.m3u8"
if [ ! -f "$playlist_file" ]; then
echo -e "playlist '$playlist_name' not found" >&2
exit 1
fi
local removed_count=0
local not_found_count=0
for song_name in "$@"; do
if song_in_playlist "$playlist_name" "$song_name"; then
# Create temporary file without the song
local temp_file=$(mktemp)
local in_song_block=false
local skip_next=false
while IFS= read -r line; do
if [[ "$line" =~ ^#EXTINF: ]]; then
# Check if this is the song we want to remove
local current_song=$(echo "$line" | sed 's/^#EXTINF:.*,//')
if [ "$current_song" = "$song_name" ]; then
skip_next=true
# Remove hard link if it exists
local next_line=$(head -n 1)
if [ -f "$next_line" ]; then
rm -f "$next_line"
fi
continue
fi
in_song_block=true
echo "$line" >> "$temp_file"
elif [ "$skip_next" = true ]; then
skip_next=false
continue
else
echo "$line" >> "$temp_file"
fi
done < "$playlist_file"
# Replace original file with temp file
mv "$temp_file" "$playlist_file"
else
echo -e "track '$song_name' not found in playlist '$playlist_name'" >&2
fi
done
}
# Delete a playlist
delete_playlist() {
local playlist_name="$1"
if [ -z "$playlist_name" ]; then
echo -e "playlist name is required" >&2
echo "Usage: $(basename "$0") delete <playlist>" >&2
exit 1
fi
if ! playlist_exists "$playlist_name"; then
echo -e "playlist '$playlist_name' not found" >&2
exit 1
fi
local playlist_dir="$ALBUMS_DIR/$playlist_name"
local playlist_file="$playlist_dir/$playlist_name.m3u8"
# Remove hard links for songs in this playlist
while IFS= read -r line; do
if [[ "$line" =~ ^[^#] ]] && [ -f "$line" ]; then
rm -f "$line"
fi
done < "$playlist_file"
# Remove the entire playlist directory
rm -rf "$playlist_dir"
}
# Play a playlist
play_playlist() {
local playlist_name="$1"
if [ -z "$playlist_name" ]; then
echo -e "${RED}Error:${NC} Playlist name is required"
echo "Usage: $(basename "$0") play <playlist_name>"
exit 1
fi
local playlist_dir="$ALBUMS_DIR/$playlist_name"
local playlist_file="$playlist_dir/$playlist_name.m3u8"
if [ ! -f "$playlist_file" ]; then
echo -e "${RED}Error:${NC} Playlist '$playlist_name' not found"
exit 1
fi
(cd "$playlist_dir" && eval $MYPLAYCOMMAND "$playlist_name.m3u8")
}
# Main script logic
main() {
# Initialize directories
init_directories
case "${1:-}" in
"list")
list_playlists
;;
"tracks")
list_songs "$2"
;;
"add")
shift
add_songs "$@"
;;
"remove")
shift
remove_songs "$@"
;;
"create")
shift
create_playlists "$@"
;;
"delete")
delete_playlist "$2"
;;
"play")
play_playlist "$2"
;;
"help"|"-h"|"--help"|"")
show_usage
;;
*)
echo -e "unknown command '$1'" >&2
echo "Use '$(basename "$0") help' for usage information" >&2
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"