]> Git — Sourcephile - julm/julm-nix.git/blob - nixpkgs/overlays/podl.sh
nix: update setting
[julm/julm-nix.git] / nixpkgs / overlays / podl.sh
1 #!/usr/bin/env bash
2 # shellcheck disable=SC1004
3 # shellcheck disable=SC2016
4 # shellcheck disable=SC2086
5 # shellcheck disable=SC2155
6 # Name: podl - podcast downloader with caching
7 # Version: 2021-10-22
8 # Last version: https://git.code.sourcephile.fr/~julm/julm-nix/tree/main/item/nixpkgs/overlays/podl.sh
9 # Synopsis:
10 # $ mkdir LaMéthodeScientifique 3Blue1Brown
11 # $ echo >LaMéthodeScientifique/.feed http://radiofrance-podcast.net/podcast09/rss_14312.xml
12 # $ echo >3Blue1Brown/.feed https://youtube.com/channel/UCYO_jab_esuFRV4b17AJtAw
13 # $ echo >>3Blue1Brown/.feed https://www.youtube.com/feeds/videos.xml?channel_id=UCYO_jab_esuFRV4b17AJtAw
14 # $ podl
15 # Description:
16 # podl is a wrapper around yt-dlp(1) <https://yt-dlp.org/>
17 # to download podcasts from feeds whose URI(s) you write
18 # in ".feed" files in or below the current working directory.
19 # The feed formats currently supported are:
20 # - RSS
21 # - Youtube's Atom
22 # - Every input format supported by yt-dlp,
23 # when using a ".page" instead of a ".feed" file.
24 # It downloads much more quickly than simply
25 # running those commands directly on the feed
26 # or on each entries of the feed, because
27 # to decide whether a podcast has already been downloaded or not,
28 # it relies only on the feed's content and on a ".downloaded" file
29 # it creates next to those ".feed" files;
30 # avoiding the cost of network activity when not necessary.
31 # Environment:
32 # - $SKIP_DOWNLOAD: if set, skip the download command
33 # but still register the entry in ".downloaded".
34 # Useful when adding the feed if you only want
35 # a few entries from the feed:
36 # run SKIP_DOWNLOAD=set podl, then edit ".downloaded"
37 # to remove the entries you want, then run podl again.
38 # This trick does not work with ".page" downloads.
39 # - $YT: options passed to yt-dlp.
40 # - $XTRACE: if set, enables set -x on the generated commands.
41 # Files:
42 # - ".yt-dlp": optional yt-dlp config,
43 # (looked up in parent directories).
44 # - ".url.xpath": custom XPath selector for the URL
45 # (looked up in parent directories).
46 # SPDX-License-Identifier: GPL-3.0-or-later
47 # Bugs: Julien Moutinho <julm+podl@sourcephile.fr>
48 set -eu
49
50 look_up() {
51 local key=$1; shift
52 (
53 while test "$PWD" != / -a "$PWD" != //
54 do
55 test ! -f "$key" || {
56 "$@" "$PWD/$key"
57 return
58 }
59 cd ..
60 done
61 )
62 }
63
64 find -H "$@" -type f '(' -name .feed -o -name .page ')' |
65 sort |
66 while IFS= read -r found; do
67 IFS=$(printf ' \n\r')
68 src="$(readlink -e "$found")"
69 dst="$(dirname "$found")"
70 dst="$(readlink -e "$dst")"
71 export dst
72 echo >&2 "$dst"
73 (
74 cd "$dst"
75 export YT="$(look_up .yt-dlp printf -- '--config-location %s') ${YT-}"
76 case $found in
77 (*/.page)
78 yt-dlp $YT \
79 ${SKIP_DOWNLOAD:+--skip-download} \
80 --download-archive .downloaded \
81 --batch-file "$src"
82 ;;
83 (*/.feed)
84 feeds=$(sed "$src" \
85 -e 's@.*youtube\.com/channel/\([^/]\+\).*@https://www.youtube.com/feeds/videos.xml?channel_id=\1@' \
86 -e 's@.*youtube\.com/user/\([^/]\+\).*@https://www.youtube.com/feeds/videos.xml?user=\1@' \
87 -e 's@.*youtube\.com.*list=\([^&]\+\).*@https://www.youtube.com/feeds/videos.xml?playlist_id=\1@' \
88 )
89 for feed in $feeds; do
90 export url_xpath="$(look_up .url.xpath cat)"
91 curl -Ls "$feed" |
92 xml select -T \
93 -N atom="http://www.w3.org/2005/Atom" \
94 -N yt="http://www.youtube.com/xml/schemas/2015" \
95 -N mrss="http://search.yahoo.com/mrss/" \
96 -t -m "/rss/channel/item" \
97 -o "title='" -v "translate(translate(title,'\"','_'),\"'/:?&|$IFS\",\"’_____ \")" -o "'" -n \
98 -o "guid='" -v "translate(translate(guid,'\"','_'),\"'$IFS\",\" \")" -o "'" -n \
99 -o "url='" -v "translate(${url_xpath:-"enclosure[1]/@url"},\"'$IFS\",\" \")" -o "'" -n \
100 -o "published='" -v "translate(pubDate,\"'$IFS\",\" \")" -o "'" -n \
101 -o '
102 file=${url##*/}
103 file=${file%%\#*}
104 file=${file%%\?*}
105 # remove leading whitespace characters
106 title="${title#"${title%%[![:space:]]*}"}"
107 # remove trailing whitespace characters
108 title="${title%"${title##*[![:space:]]}"}"
109 test -z "$url" ||
110 grep -qxF -e "url $url" -e "guid $guid" .downloaded || {
111 published=$(date +%Y-%m-%d -d "$published")
112 echo >&2 "$dst/$published - $title"
113 if test ! "${SKIP_DOWNLOAD:+set}"
114 then
115 yt-dlp $YT \
116 --output "$published - ${title//%/%%}.%(ext)s" \
117 "$url"
118 fi
119 { flock --exclusive 3
120 echo >&3 "guid $guid"
121 echo >&3 "url $url"
122 } 3>>.downloaded
123 }
124 ' -n -b \
125 -t -m "/atom:feed/atom:entry[yt:videoId]" \
126 -o "title='" -v "translate(translate(atom:title,'\"','_'),\"'/:?&|$IFS\",\"’_____ \")" -o "'" -n \
127 -o "url='" -v "translate(${url_xpath:-"atom:link[@rel='alternate']/@href"},\"'$IFS\",\" \")" -o "'" -n \
128 -o "published='" -v "translate(atom:published,\"'$IFS\",\" \")" -o "'" -n \
129 -o "id='" -v "translate(yt:videoId,\"'$IFS\",\" \")" -o "'" -n \
130 -o '
131 # remove leading whitespace characters
132 title="${title#"${title%%[![:space:]]*}"}"
133 # remove trailing whitespace characters
134 title="${title%"${title##*[![:space:]]}"}"
135 grep -qxF "youtube $id" .downloaded || {
136 published=$(date +%Y-%m-%d -d "$published")
137 echo >&2 "$dst/$published - $title.$id"
138 if test "${SKIP_DOWNLOAD:+set}"
139 then
140 { flock --exclusive 3
141 echo >&3 "youtube $id"
142 } 3>>.downloaded
143 else
144 yt-dlp $YT \
145 --download-archive .downloaded \
146 --output "$published - ${title//%/%%}.%(id)s.%(format_id)s.%(ext)s" \
147 "$url"
148 fi
149 }
150 ' |
151 "$SHELL" -seu${XTRACE:+x}
152 done;;
153 esac
154 )
155 done