Skip to content

Instantly share code, notes, and snippets.

@ga2arch
Created August 24, 2011 13:46
Show Gist options
  • Select an option

  • Save ga2arch/1168100 to your computer and use it in GitHub Desktop.

Select an option

Save ga2arch/1168100 to your computer and use it in GitHub Desktop.
Simple icecast radio recorder
import urllib2
save_folder = ''
stream_url = ''
def parse_metaint():
line = resp.readline()
while line:
line = resp.readline()
if 'icy-metaint' in line:
metaint = line.split(': ')[1].strip()
if line == '\r\n':
return int(metaint)
def read_stream(resp):
metaint = parse_metaint()
f = None
while True:
data = resp.read(metaint)
if f: f.write(data)
length = ord(resp.read(1)) * 16
if length == 0: continue
title = parse_metadata(resp.read(length))
title.replace('/', '\\/')
if f: f.close()
f = open(save_folder+title+'.mp3', 'wb')
print title
def parse_metadata(data):
title = data.split('StreamTitle=\'')[1].split('\';')[0]
return title
headers = {'Icy-MetaData':1}
req = urllib2.Request(stream_url, None, headers)
resp = urllib2.urlopen(req)
read_stream(resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment