Skip to content

Instantly share code, notes, and snippets.

@se1983
Last active November 14, 2015 11:12
Show Gist options
  • Save se1983/05b71cd5e1f46ea0fb88 to your computer and use it in GitHub Desktop.
Save se1983/05b71cd5e1f46ea0fb88 to your computer and use it in GitHub Desktop.
Generate a list of radiostations from http://wiki.ubuntuusers.de/internetradio/stationen#Radiosender-Deutschland and save it in json for a synology DSM
from __future__ import unicode_literals
from bs4 import BeautifulSoup
from urllib import urlretrieve
import re
import json
import mechanize
class Browser(mechanize.Browser):
''' Browser
Erbt von Mechanize.Browser.
'''
def __init__(self):
mechanize.Browser.__init__(self)
self.addheaders = [
('User-agent',
'Mozilla/5.0 (X11; U; Linux i686; en-US;\
rv:1.9.0.1) Gecko/2008071615 \
Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
self.set_handle_equiv(True)
self.set_handle_redirect(True)
self.set_handle_referer(True)
_conf = dict(
URL="http://wiki.ubuntuusers.de/internetradio/stationen#Radiosender-Deutschland")
## Get rid of comments
br = Browser()
def get_radiostations():
soup = BeautifulSoup(br.open(_conf['URL']).read(), 'html.parser')
for div in soup.findAll("div", { "class" : "section_2" }):
a = div.find("a", {"class": "external"})
try:
_title = a.contents
except AttributeError as e:
#print (e.message)
continue
pre = div.find("pre")
try:
_url = [line.split('#')[0].split('\n')[0]
for line in pre
if "http://" in line]
except TypeError as e:
#print (e.message)
continue
try:
yield((_title, _url))
except IndexError:
continue
stations = []
total = 0
for station in get_radiostations():
stations.append( dict(
bitrate=None,
desc = "",
name = "".join(station[0]).encode('utf-8'),
url = "".join(station[1]).encode('utf-8')
))
total += 1
with open ("/tmp/userdef.json", "w") as wfile:
wfile.write(json.dumps(dict(
total = total,
stations = stations),
sort_keys=True,
indent=4,
separators=(',', ': ')))
#print (json.dumps(dict(total = total, stations = stations),sort_keys=True, indent=4, separators=(',', ': ')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment