]>
Git — Sourcephile - julm/julm-nix.git/blob - nixpkgs/overlays/podl.sh
2 # shellcheck disable=SC1004
3 # shellcheck disable=SC2016
4 # shellcheck disable=SC2086
5 # shellcheck disable=SC2155
6 # Name: podl - podcast downloader with caching
8 # Last version: https://git.sourcephile.fr/julm/home-julm.git/blob_plain/HEAD:/.config/nixpkgs/overlays/podl.sh
10 # $ mkdir LaMéthodeScientifique 3Blue1Brown
11 # $ echo http://radiofrance-podcast.net/podcast09/rss_14312.xml >LaMéthodeScientifique/.feed
12 # $ echo https://youtube.com/channel/UCYO_jab_esuFRV4b17AJtAw >3Blue1Brown/.feed
15 # podl is a wrapper around youtube-dl(1) <https://youtube-dl.org/>
16 # to download podcasts from feeds whose URI(s) you write
17 # in ".feed" files in or below the current working directory.
18 # The feed formats currently supported are:
21 # - Every input format supported by youtube-dl,
22 # when using a ".page" instead of a ".feed" file.
23 # It downloads much more quickly than simply
24 # running those commands directly on the feed
25 # or on each entries of the feed, because
26 # to decide whether a podcast has already been downloaded or not,
27 # it relies only on the feed's content and on a ".downloaded" file
28 # it creates next to those ".feed" files;
29 # avoiding the cost of network activity when not necessary.
31 # - $SKIP_DOWNLOAD: if set, skip the download command
32 # but still register the entry in ".downloaded".
33 # Useful when adding the feed if you only want
34 # a few entries from the feed:
35 # run SKIP_DOWNLOAD=set podl, then edit ".downloaded"
36 # to remove the entries you want, then run podl again.
37 # This trick does not work with ".page" downloads.
38 # - $YT: options passed to youtube-dl.
39 # - $XTRACE: if set, enables set -x on the generated commands.
41 # - ".youtube-dl": optional youtube-dl config,
42 # (looked up in parent directories).
43 # - ".url.xpath": custom XPath selector for the URL
44 # (looked up in parent directories).
46 # Bugs: Julien Moutinho <julm+podl@sourcephile.fr>
52 while test "$PWD" != / -a "$PWD" != //
63 find -H "$@" -type f
'(' -name .feed
-o -name .page
')' |
65 while IFS
= read -r found
; do
67 src
="$(readlink -e "$found")"
68 dst
="$(dirname "$found")"
69 dst
="$(readlink -e "$dst")"
74 export YT
="$(look_up .youtube-dl printf -- '--config-location %s') ${YT-}"
78 ${SKIP_DOWNLOAD:+--skip-download} \
79 --download-archive .downloaded \
84 -e 's@.*youtube\.com/channel/\([^/]\+\).*@https://www.youtube.com/feeds/videos.xml?channel_id=\1@' \
85 -e 's@.*youtube\.com/user/\([^/]\+\).*@https://www.youtube.com/feeds/videos.xml?user=\1@' \
86 -e 's@.*youtube\.com.*list=\([^&]\+\).*@https://www.youtube.com/feeds/videos.xml?playlist_id=\1@' \
88 for feed
in $feeds; do
89 export url_xpath
="$(look_up .url.xpath cat)"
92 -N atom
="http://www.w3.org/2005/Atom" \
93 -N yt
="http://www.youtube.com/xml/schemas/2015" \
94 -N mrss
="http://search.yahoo.com/mrss/" \
95 -t -m "/rss/channel/item" \
96 -o "title='" -v "translate(translate(title,'\"','_'),\"'/:?&|$IFS\",\"’_____ \")" -o "'" -n \
97 -o "guid='" -v "translate(translate(guid,'\"','_'),\"'$IFS\",\"’ \")" -o "'" -n \
98 -o "url='" -v "translate(${url_xpath:-"enclosure[1]/@url"},\"'$IFS\",\"’ \")" -o "'" -n \
99 -o "published='" -v "translate(pubDate,\"'$IFS\",\"’ \")" -o "'" -n \
104 # remove leading whitespace characters
105 title="${title#"${title%%[![:space:]]*}"}"
106 # remove trailing whitespace characters
107 title="${title%"${title##*[![:space:]]}"}"
109 grep -qxF -e "url $url" -e "guid $guid" .downloaded || {
110 published=$(date +%Y-%m-%d -d "$published")
111 echo >&2 "$dst/$published - $title"
112 if test ! "${SKIP_DOWNLOAD:+set}"
115 --output "$published - ${title//%/%%}.%(ext)s" \
118 { flock --exclusive 3
119 echo >&3 "guid $guid"
124 -t -m "/atom:feed/atom:entry[yt:videoId]" \
125 -o "title='" -v "translate(translate(atom:title,'\"','_'),\"'/:?&|$IFS\",\"’_____ \")" -o "'" -n \
126 -o "url='" -v "translate(${url_xpath:-"atom:link[@rel='alternate']/@href"},\"'$IFS\",\"’ \")" -o "'" -n \
127 -o "published='" -v "translate(atom:published,\"'$IFS\",\"’ \")" -o "'" -n \
128 -o "id='" -v "translate(yt:videoId,\"'$IFS\",\"’ \")" -o "'" -n \
130 # remove leading whitespace characters
131 title="${title#"${title%%[![:space:]]*}"}"
132 # remove trailing whitespace characters
133 title="${title%"${title##*[![:space:]]}"}"
134 grep -qxF "youtube $id" .downloaded || {
135 published=$(date +%Y-%m-%d -d "$published")
136 echo >&2 "$dst/$published - $title.$id"
137 if test "${SKIP_DOWNLOAD:+set}"
139 { flock --exclusive 3
140 echo >&3 "youtube $id"
144 --download-archive .downloaded \
145 --output "$published - ${title//%/%%}.%(id)s.%(format_id)s.%(ext)s" \
150 "$SHELL" -seu${XTRACE:+x}