code organization and fix for comments

This commit is contained in:
Emma Tebibyte 2023-11-07 23:33:50 -07:00
parent 3e50582c10
commit b83408a453
Signed by: emma
GPG Key ID: 6D661C738815E7DD
1 changed files with 24 additions and 19 deletions

43
yt
View File

@ -22,7 +22,7 @@ add() { # adds a video to a playlist file
while test -n "$2"; do
if test -z "$(sed -n ";$1;p" "$2")"
then
printf "%s: %s: Video already in playlist.\n" "$argv0" "$1"
printf "%s: %s: %s: Video already in playlist.\n" "$argv0" "$2" "$1"
exit
else
archive "$1"
@ -35,14 +35,21 @@ add() { # adds a video to a playlist file
archive() { # archives a video to the Wayback Machine
while test -n "$1"; do
wayback_url="$(curl -s "$WBAPI$1" \
| jq .archived_snapshots.closest.url \
| tr -d '"' | sed 's/^http:/https:/g')"
if test -n "$(printf '%s\n' "$1" | grep -e 'web\.archive\.org')"
then
shift
continue
else
wayback_url="$(curl -s "$WBAPI$1" \
| jq .archived_snapshots.closest.url \
| tr -d '"' | sed 's/^http:/https:/g')"
if [ "$wayback_url" != "null" ]; then continue; fi
# skips archiving on the Wayback Machine if the video has a snapshot
if [ "$wayback_url" != "null" ]; then shift; continue; fi
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
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
fi
shift
done
}
@ -58,15 +65,12 @@ cache() { # cache the video title for faster retrieval
clone() { # clones a YouTube playlist to a file
yt-dlp --flat-playlist "$1" --print url > "$2"
verify "$2"
}
for line in "$(cat "$2")"; do # uuoc but necessary :3
if test -n "$(printf '%s\n' "$line" \
| grep -e 'web\.archive\.org' -e '^#')"
then
continue
else
archive "$line"
fi
lines() {
while test -n "$1"; do
sed 's/#.*$//g' "$1"
shift
done
}
@ -81,7 +85,7 @@ music() { # downloads a video, splitting by chapter and only saving the audio
pick() { # Pick a video to play from a list of videos
while test -n "$1"; do
for line in $(cat "$1"); do # see ln 62
for line in $(lines "$1"); do
if test -n "$(printf '%s\n' "$line" | sed -n '/^\#/p')"
then
continue
@ -121,9 +125,9 @@ usage() {
verify() { # replaces videos with archived versions if they are not available
while test -n "$1"; do
for video in $(cat "$1"); do # see ln 62
for video in $(lines "$1"); do
if test -n "$(yt-dlp -s "$video" 2>&1 \
| grep -i -e 'video unavailable.' -e 'private video')"
| grep -i -e 'video unavailable' -e 'private video' -e 'been removed')"
then
printf "%s: %s: Video unavailable.\n" "$argv0" "$video" 1>&2
@ -137,7 +141,7 @@ verify() { # replaces videos with archived versions if they are not available
printf "%s: %s: Video not available on the Wayback Machine.\n" \
"$argv0" "$video" 1>&2
# replace the video link with a comment containing link
content="$(sed "s;^$video;\#$video;g" "$1")"
content="$(sed "s;^$video;\# $video;g" "$1")"
# write the new content buffer to file
printf '%s\n' "$content" > "$1"
@ -152,6 +156,7 @@ verify() { # replaces videos with archived versions if they are not available
else
printf '%s: %s: Video available.\n' "$argv0" "$video" 1>&2
cache "$video"
archive "$video"
fi
done