Skip to content

Instantly share code, notes, and snippets.

@hut8
Created November 24, 2015 00:35
Show Gist options
  • Save hut8/557b23561d444819cde4 to your computer and use it in GitHub Desktop.
Save hut8/557b23561d444819cde4 to your computer and use it in GitHub Desktop.
Download this american life
import requests
import os
DEST_PATH = os.path.join(
os.path.expanduser('~'), 'TAM')
os.makedirs(DEST_PATH, exist_ok=True)
print("enter episode number:")
episode = int(input())
def url(episode_number, ix):
params = { 'ep': episode_number, 'ix': ix }
return "http://stream.thisamericanlife.org/{ep}/stream/{ep}_64k_{ix:03d}.ts".format(
**params
)
chunk = 0
sink_path = os.path.join(DEST_PATH, "{0}.mpeg".format(str(episode)))
if os.path.exists(sink_path):
print("looks like you already have that file at", sink_path)
exit(1)
with open(sink_path, 'wb') as f:
while True:
chunk_url = url(episode, chunk)
res = requests.get(chunk_url)
if res.status_code != 200:
print("got response code", res.status_code, chunk_url)
if res.status_code == 404:
print(" ... so probably done")
break
print("got chunk", int(chunk))
f.write(res.content)
chunk += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment