/
yt_podoff
executable file
·58 lines (41 loc) · 1.7 KB
/
yt_podoff
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
#!/bin/bash
# yt_podoff - Create offline caches of YouTube podcast feeds
#
# USAGE: yt_podoff [<youtube-dl_options>... --] <YouTube_url_file> ...
#
# EXAMPLE:
# $ mkdir -p ${HOME}/podcasts/Hak5
# $ echo "https://www.youtube.com/playlist?list=PLW5y1tjAOzI0w_GbtiEbYS5PGJ2pmxAIX" > ${HOME}/podcasts/Hak5/.url
# $ yt_podoff --write-info-json -- ${HOME}/podcasts/Hak5/.url
# Save any youtube-dl options
YOUTUBE_DL_OPTS=()
if [[ "$1" == -* ]]; then
while [[ "$1" != -- ]]; do
YOUTUBE_DL_OPTS+=("$1")
shift
done
fi
for F in "$@"; do
if [ -s "$F" ] ; then
cd "$(dirname "$F")"
# Read YouTube page URLs...
FEED_URLS=(); while read U; do
# ...and transform them into RSS feed URLs (ref: https://stackoverflow.com/a/31535120)
if [[ "$U" =~ .*youtube\.com/channel/([^/]+).* ]]; then
FEED_URLS+=("https://www.youtube.com/feeds/videos.xml?channel_id=${BASH_REMATCH[1]}")
elif [[ "$U" =~ .*youtube\.com/user/([^/]+).* ]]; then
FEED_URLS+=("https://www.youtube.com/feeds/videos.xml?user=${BASH_REMATCH[1]}")
elif [[ "$U" =~ .*youtube\.com.*list=([^&]+).* ]]; then
FEED_URLS+=("https://www.youtube.com/feeds/videos.xml?playlist_id=${BASH_REMATCH[1]}")
else
echo "ERROR: Unrecognized URL '$U', skipping." >&2
fi
done <"$(basename "$F")"
# Carve out the video URLs from the RSS feeds...
curl -s "${FEED_URLS[@]}" | grep 'link rel="alternate"' | sed 's/^.*href="\([^"]*\)".*/\1/' | while read VID_URL; do
# ...but check if youtube-dl's already downloaded it before
grep -q "youtube ${VID_URL##*=}" .done.lst || youtube-dl "${YOUTUBE_DL_OPTS[@]}" --download-archive .done.lst "${VID_URL}"
done
cd - 2>/dev/null
fi
done