add directory for playlists & add list function

This commit is contained in:
Emma Tebibyte 2023-11-19 20:16:11 -07:00
parent 9b8cca6de5
commit b47033a965
Signed by: emma
GPG Key ID: 6D661C738815E7DD

91
yt
View File

@ -19,24 +19,35 @@
set -e set -e
test -z "$DEBUG" || set -x test -z "$DEBUG" || set -x
test -z "$XDG_CACHE_HOME" && cachefile="$HOME/.cache/yt.cache" \
|| cachefile="$XDG_CACHE_HOME/yt.cache"
test -z "$YT_PL_DIR" \
|| test -n "$XDG_DATA_HOME" && YT_PL_DIR="$XDG_DATA_HOME/yt" \
|| YT_PL_DIR="$HOME/.local/share/yt"
argv0="$0" argv0="$0"
com="$1" com="$1"
cachefile="$XDG_CACHE_HOME/yt.cache"
FMT='%(title)s %(channel)s (%(duration>%H:%M:%S)s) [%(webpage_url)s]' FMT='%(title)s %(channel)s (%(duration>%H:%M:%S)s) [%(webpage_url)s]'
WBAPI='https://archive.org/wayback/available?url=' WBAPI='https://archive.org/wayback/available?url='
add() { # adds a video to a playlist file add() { # adds a video to a playlist file
while test -n "$2"; do while test -f "$YT_PL_DIR/$2.m3u"; do
if test -n "$(grep -e "$1" "$2")" file="$YT_PL_DIR/$2.m3u"
video="$(printf '%s\n' "$1" \
| sed -e 's/youtu\.be\//www.youtube\.com\/watch?q=/g' -e 's/\?[^q].*$//g')"
printf '%s\n' "$video"
if test -n "$(grep -e "$video" "$file")"
then then
printf "%s: %s: %s: Video already in playlist.\n" "$argv0" "$2" "$1" printf "%s: %s: %s: Video already in playlist.\n" \
"$argv0" "$file" "$video"
exit exit
else else
cache "$1" cache "$video"
archive "$1" archive "$video"
printf "%s\n" "$1" >> "$2" printf "%s\n" "$video" >> "$file"
fi fi
shift shift
@ -58,18 +69,14 @@ archive() { # archives a video to the Wayback Machine
if [ "$wayback_url" != "null" ]; then shift; continue; fi if [ "$wayback_url" != "null" ]; then shift; continue; fi
printf "%s: %s: Saving video to the Wayback Machine.\n" "$argv0" "$1" 1>&2 printf "%s: %s: Saving video to the Wayback Machine.\n" "$argv0" "$1" 1>&2
curl -s -d "url=$1" https://web.archive.org/{url} >/dev/null curl -s -d "url=$1" 'https://web.archive.org/{url}' >/dev/null
fi fi
shift shift
done done
} }
cache() { # cache the video title for faster retrieval cache() { # cache the video title for faster retrieval
if test -z "$XDG_CACHE_HOME"; then test -e "$cachefile" || touch "$cachefile"
printf "%s: unable to create title cachefile: \$XDG_CACHE_HOME unset." \
"$argv0" 2>&1
exit 69 # sysexits.h(3) EX_UNAVAILABLE
fi
while test -n "$1"; do while test -n "$1"; do
grep "$1" "$cachefile" 2>/dev/null 1>&2 || printf '%s\n' \ grep "$1" "$cachefile" 2>/dev/null 1>&2 || printf '%s\n' \
@ -79,8 +86,11 @@ cache() { # cache the video title for faster retrieval
} }
clone() { # clones a YouTube playlist to a file clone() { # clones a YouTube playlist to a file
yt-dlp --flat-playlist "$1" --print url > "$2" test -d "$YT_PL_DIR" || mkdir -p "$YT_PL_DIR"
verify "$2" file="$YT_PL_DIR/$2.m3u"
yt-dlp --flat-playlist "$1" --print url > "$file"
verify "$file"
} }
lines() { lines() {
@ -90,6 +100,17 @@ lines() {
done done
} }
list() {
selection="$(ls "$YT_PL_DIR" | sed 's/\.m3u//g' | $YTPICK)"
while test -d "$YT_PL_DIR/$selection"; do
dir="$selection"
selection="$(ls "$YT_PL_DIR/$selection" | sed 's/\.m3u//g' | $YTPICK)"
done
pick "$dir/$selection"
}
music() { # downloads a video, splitting by chapter and only saving the audio music() { # downloads a video, splitting by chapter and only saving the audio
while test -n "$1"; do while test -n "$1"; do
yt-dlp -vx --split-chapters -o \ yt-dlp -vx --split-chapters -o \
@ -99,14 +120,17 @@ music() { # downloads a video, splitting by chapter and only saving the audio
done done
} }
pick() { # Pick a video to play from a list of videos pick() { # Pick a video to play from a playlist of videos
if test -z "$YTPICK"; then if test -z "$YTPICK"; then
printf "%s: Please set \$YTPICK to your preferred picking tool." "$argv0" 1>&2 printf "%s: Please set \$YTPICK to your preferred picking tool." \
"$argv0" 1>&2
exit 78 # sysexits.h(3) EX_CONFIG exit 78 # sysexits.h(3) EX_CONFIG
fi fi
while test -n "$1"; do while test -f "$YT_PL_DIR/$1.m3u"; do
for line in $(lines "$1"); do file="$YT_PL_DIR/$1.m3u"
for line in $(lines "$file"); do
if test -n "$(printf '%s\n' "$line" | sed -n '/^\#/p')" if test -n "$(printf '%s\n' "$line" | sed -n '/^\#/p')"
then then
continue continue
@ -124,17 +148,18 @@ pick() { # Pick a video to play from a list of videos
done done
chosen="$(printf '%s\n' "$list" | $YTPICK | sed -e 's/.*\[//g' -e 's/\]//g')" chosen="$(printf '%s\n' "$list" | $YTPICK | sed -e 's/.*\[//g' -e 's/\]//g')"
printf "%s: %s: Playing stream.\n" "$argv0" "$chosen" 1>&2
test -n "$chosen" && mpv "$chosen" test -n "$chosen" && mpv "$chosen"
} }
play() { # play a video after caching its title play() { # play a video after caching its title
cache $@ cache "$@"
mpv $@ mpv "$@"
} }
queue() { queue() {
while test -n "$1"; do while test -f "$YT_PL_DIR/$1.m3u"; do
mpv "$1" mpv "$YT_PL_DIR/$1.m3u"
shift shift
done done
} }
@ -145,8 +170,10 @@ usage() {
} }
verify() { # replaces videos with archived versions if they are not available verify() { # replaces videos with archived versions if they are not available
while test -n "$1"; do while test -f "$YT_PL_DIR/$1.m3u"; do
for video in $(lines "$1"); do file="$YT_PL_DIR/$1.m3u"
for video in $(lines "$file"); do
if test -n "$(yt-dlp -s "$video" 2>&1 \ if test -n "$(yt-dlp -s "$video" 2>&1 \
| grep -i -e 'video unavailable' -e 'private video' -e 'been removed')" | grep -i -e 'video unavailable' -e 'private video' -e 'been removed')"
then then
@ -162,16 +189,16 @@ verify() { # replaces videos with archived versions if they are not available
printf "%s: %s: Video not available on the Wayback Machine.\n" \ printf "%s: %s: Video not available on the Wayback Machine.\n" \
"$argv0" "$video" 1>&2 "$argv0" "$video" 1>&2
# replace the video link with a comment containing link # replace the video link with a comment containing link
content="$(sed "s;^$video;\# $video;g" "$1")" content="$(sed "s;^$video;\# $video;g" "$file")"
# write the new content buffer to file # write the new content buffer to file
printf '%s\n' "$content" > "$1" printf '%s\n' "$content" > "$file"
else else
printf "%s: %s: Replacing link with Wayback link.\n" \ printf "%s: %s: Replacing link with Wayback link.\n" \
"$argv0" "$video" 1>&2 "$argv0" "$video" 1>&2
content="$(sed "s;^$video;$wayback_url;g" "$1")" content="$(sed "s;^$video;$wayback_url;g" "$file")"
printf '%s\n' "$content" >"$1" printf '%s\n' "$content" >"$file"
cache "$wayback_url" cache "$wayback_url"
fi fi
else else
@ -215,6 +242,10 @@ case "$com" in
shift 2>/dev/null || usage 'clone uri file' shift 2>/dev/null || usage 'clone uri file'
clone "$@" clone "$@"
;; ;;
list)
shift 2>/dev/null || usage 'list'
list "$@"
;;
music) music)
shift 2>/dev/null || usage 'music uri...' shift 2>/dev/null || usage 'music uri...'
music "$@" music "$@"
@ -236,6 +267,6 @@ case "$com" in
verify "$@" verify "$@"
;; ;;
*) *)
usage 'add | clone | music | pick | play | verify' usage 'add | clone | list | music | pick | play | verify'
;; ;;
esac esac