Skip to content

Instantly share code, notes, and snippets.

@evanlong
Created March 25, 2012 07:07
Show Gist options
  • Save evanlong/2192044 to your computer and use it in GitHub Desktop.
Save evanlong/2192044 to your computer and use it in GitHub Desktop.
Used to create a single mp3 file for the various NPR news shows that are not on iTunes as a podcast.
"""
Used to create a single mp3 file for the various NPR news shows that are not on
iTunes as a podcast.
Dependency on:
mp3wrap which can be installed with your favorite package manager
"""
import os
import os.path
from datetime import date, timedelta
import sys
import urllib2
import shutil
BASE_URL = "http://public.npr.org/anon.npr-mp3/npr/"
SHOW_URL = os.path.join(BASE_URL, "{show}/{y}/{m}/{y}{m}{d}_{show}_{idx}.mp3")
def downloadShow(name, offset=0):
d = date.today() - timedelta(days=offset)
year = str(d.year)
month = str(d.month).zfill(2)
day = str(d.day).zfill(2)
opener = urllib2.build_opener(urllib2.HTTPRedirectHandler())
files = []
for i in range(1,50):
idx = str(i).zfill(2)
requestUrl = SHOW_URL.format(show=name,
y=year,
m=month,
d=day,
idx=idx)
try:
resp = opener.open(requestUrl)
fname = "%s_%s.mp3" % (name, idx)
with open(fname,"w") as outputFile:
shutil.copyfileobj(resp, outputFile)
files.append(fname)
except urllib2.HTTPError:
break
if len(files) > 0:
join = " ".join(files)
outName = "%s_%s_%s_%s.mp3" % (name, year, month, day)
realOutName = "%s_%s_%s_%s_MP3WRAP.mp3" % (name, year, month, day)
r = os.system("mp3wrap %s %s" % (outName, " ".join(files)))
if r == 0:
os.rename(realOutName, outName)
def main():
if len(sys.argv) < 2:
print "Usage: %s show_name [day_offset]" % (sys.argv[0])
print " show_name examples: 'atc', 'me', 'wesat', 'wesun'"
print " day_offset is number of days in past to attempt a download"
sys.exit(1)
showName = sys.argv[1]
dayOffset = 0
if len(sys.argv) > 2:
dayOffset = sys.argv[2]
downloadShow(showName, int(dayOffset))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment