Skip to content

Instantly share code, notes, and snippets.

@mikluko
Last active August 29, 2015 14:17
Show Gist options
  • Save mikluko/88131d6a7fda82009c63 to your computer and use it in GitHub Desktop.
Save mikluko/88131d6a7fda82009c63 to your computer and use it in GitHub Desktop.
#!/usr/bin/sh python
import collections
import itertools
import os.path
import requests
Item = collections.namedtuple('Item', ['label', 'data'])
DIR = 'playlists'
SOURCES = (
Item('di', 'http://listen.di.fm'),
Item('jazzradio', 'http://listen.jazzradio.com'),
Item('radiotunes', 'http://listen.radiotunes.com'),
Item('rockradio', 'http://listen.rockradio.com'),
)
SECTIONS = (
Item('aac', 'public1'),
Item('mp3', 'public3'),
)
def index_url(source, section):
return source.data + '/' + section.data
def playlist_filename(source, section, station):
basename = '{}-{}-{}.m3u'.format(
source.label, station['key'], section.label)
return os.path.join(DIR, basename)
def playlist_urls(lines):
for line in lines:
if line.startswith(b'File'):
try:
url = line.split(b'=')[1]
except IndexError:
pass
else:
yield url + b'\n'
def main():
for source, section in itertools.product(SOURCES, SECTIONS):
index = requests.get(index_url(source, section))
for station in index.json():
print(source.label, station['name'])
playlist = requests.get(station['playlist'])
filename = playlist_filename(source, section, station)
with open(filename, 'wb') as fobj:
fobj.writelines(playlist_urls(playlist.iter_lines()))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment