Subscribing to iTunes Podcasts without iTunes

Update: I’ve released an open source version of this script in php. Be sure to try out the fully functioning demo.


I often find myself discovering podcasts that are only provided as an iTunes link. Content providers risk excluding a significant number of users by not providing a standard link to the original podcast feed.

The following bash script (itx.sh) will extract the podcast’s feed url so that you may subscribe using an rss client of your choice:

#!/bin/sh

# extract the podcast id from the first argument
id=$(echo $1 | sed -r 's/^(http.+\/id([0-9]+)|([0-9]+)$).*/\2\3/')

# the podcast id was not supplied if $id isn't an integer
c=$(echo $id | tr -d 0-9)
if [ $# == 0 ] || [ -n "${c}" ]; then
  echo The podcast ID must be in one of the following formats:
  echo "- itx https://itunes.apple.com/gb/podcast/above-beyond-group-therapy/id286889904"
  echo "- itx 286889904"
  return
fi

# curl a request to iTunes and extract the feedURL attribute
url="https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/com.apple.jingle.app.finance.DirectAction/subscribePodcast?id=${id}&wasWarnedAboutPodcasts=true"
feedUrl=$(curl -s -A "iTunes/9.1.1" "${url}" | grep "feedURL" | sed -r "s/.+<string>([^<]+).+/\1/")

# return the source podcast url if it was found
if [ -n "${feedUrl}" ]; then
  echo $feedUrl
fi

Reference: get the latest podcasts from itunes store with link by RSS, JSON or something

Find and Replace Text Recursively with Bash

Linux/Unix based platforms have a variety of CLI tools which are incredibly useful. I had a scenario where I needed to update a path reference in various configuration files, fortunately this can be accomplished using a single command:

grep -rl 'foo' . | xargs -l1 sed -i 's/foo/bar/g'

Note: Forward slashes must be escaped from your find and replace strings passed to sed, e.g.

cd /etc/apache2
grep -rl '/etc/apache' conf | xargs sed -i 's/\/etc\/apache/\/etc\/apache2/g'

The above command omits the -l1 argument to xargs. This executes slightly faster as all matching paths are sent to sed at once rather than constructing a foreach loop. If you expect a lot of files to match your search string you’ll want to pass the -l1 argument to avoid a too many arguments error from sed.