Skip to content

Instantly share code, notes, and snippets.

@PauloBoaventura
Forked from ivan/grab-anchor-episode.sh
Created December 29, 2020 13:40
Show Gist options
  • Save PauloBoaventura/04bfd7d1be502db74c510cc24bc7f66c to your computer and use it in GitHub Desktop.
Save PauloBoaventura/04bfd7d1be502db74c510cc24bc7f66c to your computer and use it in GitHub Desktop.
Download a podcast episode from anchor.fm
#!/usr/bin/env bash
# Download a podcast episode from anchor.fm
#
# Usage:
# grab-anchor-episode https://anchor.fm/emerge/episodes/Robert-MacNaughton---Learnings-from-the-Life-and-Death-of-the-Integral-Center-e31val
#
# anchor.fm serves a list of m4a files that need to be concatenated with ffmpeg.
set -eu -o pipefail
url=$1
json=$(curl -sL "$url" | grep -P -o 'window.__STATE__ = .*' | cut -d ' ' -f 3- | sed -r 's/;$//g')
ymd=$(echo -E $json | jq -r '.episodePreview.publishOn' | cut -d 'T' -f 1)
output_basename=$ymd-$(basename -- "$url").m4a
if [[ -f "$output_basename" ]]; then
echo "$output_basename already exists; skipping download"
exit
fi
temp_dir="$(mktemp -d)"
cd "$temp_dir"
audio_urls=$(echo -E $json | jq -r '.station.audios|map(.audioUrl)|.[]')
for i in $audio_urls; do
output_file=$(basename -- "$i")
wget "$i" -O "$output_file"
echo "file '$output_file'" >> .copy_list
done
ffmpeg -f concat -safe 0 -i .copy_list -c copy "$output_basename"
cd -
mv "$temp_dir/$output_basename" ./
rm -rf "$temp_dir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment