refactor and added sync functionality
This commit is contained in:
parent
f67691e8d7
commit
c34856d134
138
yt
138
yt
@ -27,14 +27,19 @@ test -n "$YT_PL_DIR" \
|
|||||||
|| test -n "$XDG_DATA_HOME" && YT_PL_DIR="$XDG_DATA_HOME/yt" \
|
|| test -n "$XDG_DATA_HOME" && YT_PL_DIR="$XDG_DATA_HOME/yt" \
|
||||||
|| YT_PL_DIR="$HOME/.local/share/yt"
|
|| YT_PL_DIR="$HOME/.local/share/yt"
|
||||||
|
|
||||||
|
test -d "$YT_PL_DIR" \
|
||||||
|
|| mkdir -p "$YT_PL_DIR"
|
||||||
|
|
||||||
argv0="$0"
|
argv0="$0"
|
||||||
com="$1"
|
com="$1"
|
||||||
|
u='add | archive | clone | list | new | pick | play | search | sync | verify'
|
||||||
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
|
||||||
|
test -z "$2" || test -n "$3" && usage 'add uri playlist'
|
||||||
|
|
||||||
while test -f "$YT_PL_DIR/$2.m3u"; do
|
while test -f "$YT_PL_DIR/$2.m3u"; do
|
||||||
file="$YT_PL_DIR/$2.m3u"
|
file="$YT_PL_DIR/$2.m3u"
|
||||||
video="$(printf '%s\n' "$1" \
|
video="$(printf '%s\n' "$1" \
|
||||||
@ -57,6 +62,8 @@ add() { # adds a video to a playlist file
|
|||||||
}
|
}
|
||||||
|
|
||||||
archive() { # archives a video to the Wayback Machine
|
archive() { # archives a video to the Wayback Machine
|
||||||
|
test -z "$1" && usage 'archive uri...'
|
||||||
|
|
||||||
while test -n "$1"; do
|
while test -n "$1"; do
|
||||||
if test -n "$(printf '%s\n' "$1" | grep -e 'web\.archive\.org')"
|
if test -n "$(printf '%s\n' "$1" | grep -e 'web\.archive\.org')"
|
||||||
then
|
then
|
||||||
@ -78,6 +85,8 @@ archive() { # archives a video to the Wayback Machine
|
|||||||
}
|
}
|
||||||
|
|
||||||
cache() { # cache the video title for faster retrieval
|
cache() { # cache the video title for faster retrieval
|
||||||
|
test -z "$1" && usage 'cache video...'
|
||||||
|
|
||||||
test -e "$cachefile" || touch "$cachefile"
|
test -e "$cachefile" || touch "$cachefile"
|
||||||
|
|
||||||
while test -n "$1"; do
|
while test -n "$1"; do
|
||||||
@ -88,12 +97,20 @@ cache() { # cache the video title for faster retrieval
|
|||||||
}
|
}
|
||||||
|
|
||||||
clone() { # clones a YouTube playlist to a file
|
clone() { # clones a YouTube playlist to a file
|
||||||
test -d "$YT_PL_DIR" || mkdir -p "$YT_PL_DIR"
|
test -z "$1" && usage 'clone uri [playlist]'
|
||||||
|
|
||||||
test -n "$2" && file="$2" \
|
title="$(yt-dlp -s -I '1:1' --print '%(playlist)s' "$1")"
|
||||||
|| file="$YT_PL_DIR/$(yt-dlp -s -I '1:1' --print '%(playlist)s' "$1").m3u"
|
|
||||||
|
file="$YT_PL_DIR/$title.m3u"
|
||||||
|
creator="$(yt-dlp -s -I '1:1' --print '%(playlist_uploader)s' "$1")"
|
||||||
|
|
||||||
|
videos="$(yt-dlp --flat-playlist "$1" --print url)"
|
||||||
|
prefix="$(printf "#EXTM3U\n#EXTART:%s\n#PLAYLIST:%s [%s]\n" \
|
||||||
|
"$creator" "$title" "$1")"
|
||||||
|
|
||||||
|
printf "%s: %s: Saving playlist to %s.\n" "$argv0" "$title" "$file" 1>&2
|
||||||
|
printf '%s\n\n%s\n' "$prefix" "$videos" >"$file"
|
||||||
|
|
||||||
yt-dlp --flat-playlist "$1" --print url > "$file"
|
|
||||||
verify "$file"
|
verify "$file"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,6 +122,8 @@ lines() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
list() {
|
list() {
|
||||||
|
test -n "$1" && usage 'list'
|
||||||
|
|
||||||
if test -z "$YTPICK"; then
|
if test -z "$YTPICK"; then
|
||||||
printf "%s: Please set \$YTPICK to your preferred picking tool." \
|
printf "%s: Please set \$YTPICK to your preferred picking tool." \
|
||||||
"$argv0" 1>&2
|
"$argv0" 1>&2
|
||||||
@ -128,16 +147,23 @@ menu() {
|
|||||||
printf '%s/%s\n' "$dir" "$playlist"
|
printf '%s/%s\n' "$dir" "$playlist"
|
||||||
}
|
}
|
||||||
|
|
||||||
music() { # downloads a video, splitting by chapter and only saving the audio
|
new() {
|
||||||
while test -n "$1"; do
|
test -z "$1" && usage 'new playlist...'
|
||||||
yt-dlp -vx --split-chapters -o \
|
|
||||||
"chapter:%(fulltitle)s - %(section_number)s %(section_title)s.%(ext)s" \
|
file="$YT_PL_DIR/$1.m3u"
|
||||||
"$1" --audio-quality 0 >> log 2>&1
|
|
||||||
shift
|
if test -f "$file"; then
|
||||||
done
|
printf "%s: %s: File exists." "$argv0" "$file"
|
||||||
|
elif test -d "$file"; then
|
||||||
|
printf "%s: %s: Is a directory." "$argv0" "$file"
|
||||||
|
else
|
||||||
|
touch "$file"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
pick() { # Pick a video to play from a playlist of videos
|
pick() { # Pick a video to play from a playlist of videos
|
||||||
|
test -z "$1" && usage 'pick playlist...'
|
||||||
|
|
||||||
if test -z "$YTPICK"; then
|
if test -z "$YTPICK"; then
|
||||||
printf "%s: Please set \$YTPICK to your preferred picking tool." \
|
printf "%s: Please set \$YTPICK to your preferred picking tool." \
|
||||||
"$argv0" 1>&2
|
"$argv0" 1>&2
|
||||||
@ -172,6 +198,8 @@ pick() { # Pick a video to play from a playlist of videos
|
|||||||
}
|
}
|
||||||
|
|
||||||
play() { # play a video after caching its title
|
play() { # play a video after caching its title
|
||||||
|
test -z "$1" && usage 'play uri...'
|
||||||
|
|
||||||
if test -z "$PLAYER"; then
|
if test -z "$PLAYER"; then
|
||||||
printf "%s: Please set \$PLAYER to your preferred video player." \
|
printf "%s: Please set \$PLAYER to your preferred video player." \
|
||||||
"$argv0" 1>&2
|
"$argv0" 1>&2
|
||||||
@ -183,6 +211,8 @@ play() { # play a video after caching its title
|
|||||||
}
|
}
|
||||||
|
|
||||||
queue() {
|
queue() {
|
||||||
|
test -z "$1" && usage 'queue playlist...'
|
||||||
|
|
||||||
if test -z "$PLAYER"; then
|
if test -z "$PLAYER"; then
|
||||||
printf "%s: Please set \$PLAYER to your preferred video player." \
|
printf "%s: Please set \$PLAYER to your preferred video player." \
|
||||||
"$argv0" 1>&2
|
"$argv0" 1>&2
|
||||||
@ -196,6 +226,8 @@ queue() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
search() {
|
search() {
|
||||||
|
test -z "$1" && usage 'search term [count]'
|
||||||
|
|
||||||
if test -z "$YTPICK"; then
|
if test -z "$YTPICK"; then
|
||||||
printf "%s: Please set \$YTPICK to your preferred picking tool." \
|
printf "%s: Please set \$YTPICK to your preferred picking tool." \
|
||||||
"$argv0" 1>&2
|
"$argv0" 1>&2
|
||||||
@ -229,12 +261,40 @@ search() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sync() {
|
||||||
|
if test -z "$1"
|
||||||
|
then
|
||||||
|
set -- "$YT_PL_DIR"/*.m3u
|
||||||
|
else
|
||||||
|
P="$(printf '%s\n' "$YT_PL_DIR" | sed 's;\/;\\/;g')"
|
||||||
|
set -- $(printf "$@" | sed "s/.* /$P\/&\.m3u/g")
|
||||||
|
fi
|
||||||
|
|
||||||
|
while test -n "$1"; do
|
||||||
|
file="$1"
|
||||||
|
|
||||||
|
if test -f "$file"; then
|
||||||
|
URL="$(sed -n 's/^\#PLAYLIST:.* \[//p' <"$file" | tr -d ']')"
|
||||||
|
|
||||||
|
if test -n "$URL"; then
|
||||||
|
mv "$file" "$file.old"
|
||||||
|
|
||||||
|
clone "$URL" || mv "$file.old" "$file"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
printf "Usage: %s %s\n" "$argv0" "$@" 1>&2
|
printf "Usage: %s %s\n" "$argv0" "$@" 1>&2
|
||||||
exit 64 # sysexits.h(3) EX_USAGE
|
exit 64 # sysexits.h(3) EX_USAGE
|
||||||
}
|
}
|
||||||
|
|
||||||
verify() { # replaces videos with archived versions if they are not available
|
verify() { # replaces videos with archived versions if they are not available
|
||||||
|
test -z "$1" && usage 'verify playlist...'
|
||||||
|
|
||||||
while test -f "$YT_PL_DIR/$1.m3u"; do
|
while test -f "$YT_PL_DIR/$1.m3u"; do
|
||||||
file="$YT_PL_DIR/$1.m3u"
|
file="$YT_PL_DIR/$1.m3u"
|
||||||
|
|
||||||
@ -284,57 +344,11 @@ for dep in \
|
|||||||
do
|
do
|
||||||
if ! command -v "$dep" >/dev/null
|
if ! command -v "$dep" >/dev/null
|
||||||
then
|
then
|
||||||
printf "%s: %s: Missing dependency." "$argv0" "$dep" 1>&2
|
printf "%s: %s: Missing dependency.\n" "$0" "$dep" 1>&2
|
||||||
exit 69 # syexits.h(3) EX_UNAVAILABLE
|
exit 69 # syexits.h(3) EX_UNAVAILABLE
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
case "$com" in
|
test -n "$com" && shift || usage "$u"
|
||||||
add)
|
|
||||||
shift 2>/dev/null || usage 'add uri file'
|
"$com" "$@"
|
||||||
add "$@"
|
|
||||||
;;
|
|
||||||
archive)
|
|
||||||
shift 2>/dev/null || usage 'archive uri...'
|
|
||||||
archive "$@"
|
|
||||||
;;
|
|
||||||
cache)
|
|
||||||
shift 2>/dev/null || usage 'cache uri...'
|
|
||||||
cache "$@"
|
|
||||||
;;
|
|
||||||
clone)
|
|
||||||
shift 2>/dev/null || usage 'clone uri [file]'
|
|
||||||
clone "$@"
|
|
||||||
;;
|
|
||||||
list)
|
|
||||||
shift 2>/dev/null || usage 'list'
|
|
||||||
list "$@"
|
|
||||||
;;
|
|
||||||
music)
|
|
||||||
shift 2>/dev/null || usage 'music uri...'
|
|
||||||
music "$@"
|
|
||||||
;;
|
|
||||||
pick)
|
|
||||||
shift 2>/dev/null || usage 'pick file...'
|
|
||||||
pick "$@"
|
|
||||||
;;
|
|
||||||
play)
|
|
||||||
shift 2>/dev/null || usage 'play uri...'
|
|
||||||
play "$@"
|
|
||||||
;;
|
|
||||||
queue)
|
|
||||||
shift 2>/dev/null || usage 'queue file...'
|
|
||||||
queue "$@"
|
|
||||||
;;
|
|
||||||
search)
|
|
||||||
shift 2>/dev/null || usage 'search term [count]'
|
|
||||||
search "$@"
|
|
||||||
;;
|
|
||||||
verify)
|
|
||||||
shift 2>/dev/null || usage 'file...'
|
|
||||||
verify "$@"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
usage 'add | clone | list | music | pick | play | verify'
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
Loading…
Reference in New Issue
Block a user