Skip to content

Instantly share code, notes, and snippets.

@vrld
Created April 2, 2015 23:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vrld/84164a66be194c46f26e to your computer and use it in GitHub Desktop.
Save vrld/84164a66be194c46f26e to your computer and use it in GitHub Desktop.
Python Dirble API wrapper
import requests
from collections import namedtuple
class Dirble(object):
Category = namedtuple("Category", "id name description")
Station = namedtuple("Station", "id name streamurl country bitrate status")
StationDetails = namedtuple("StationDetails", "id name streamurl country bitrate status description added urlid website songhistory")
Song = namedtuple("Song", "artist title")
def __init__(self, key, base_url):
self.key = key
self.base_url = base_url
def api_call(self, name, *args):
url = "{}/{}/apikey/{}/{}".format(self.base_url, name, self.key,
"/".join([str(s) for s in args]))
r = requests.get(url)
json = r.json()
if type(json) == dict and "errormsg" in json:
raise RuntimeError(json["errormsg"])
elif type(json) == list and len(json) > 0 and "errormsg" in json[0]:
raise RuntimeError(json[0]["errormsg"])
return json
def getCategories(self):
return [Dirble.Category(d["id"], d["name"], d["description"])
for d in self.api_call("categories")]
def getPrimaryCategories(self):
return [Dirble.Category(d["id"], d["name"], d["description"])
for d in self.api_call("primaryCategories")]
def getChildCategories(self, category_id):
return [Dirble.Category(d["id"], d["name"], d["description"])
for d in self.api_call("childCategories", "primaryid", category_id)]
def getStations(self, category_id):
return [Dirble.Station(d["id"], d["name"], d["streamurl"], d["country"], d["bitrate"], d["status"])
for d in self.api_call("stations", "id", category_id)]
def search(self, text, genre = None, frompage = None, count = None):
args = []
if genre is not None:
args += ["genre", genre]
if frompage is not None:
args += ["from", frompage]
if count is not None:
args += ["count", count]
return [Dirble.Station(d["id"], d["name"], d["streamurl"], d["country"], d["bitrate"], d["status"])
for d in self.api_call("search", "search", text, *args)]
def getContinent(self, url_id):
return [Dirble.Station(d["id"], d["name"], d["streamurl"], d["country"], d["bitrate"], d["status"])
for d in self.api_call("continent", "continent", url_id)]
def getCountry(self, country_code):
return [Dirble.Station(d["id"], d["name"], d["streamurl"], d["country"], d["bitrate"], d["status"])
for d in self.api_call("country", "country", country_code)]
def getStationDetails(self, station_id):
d = self.api_call("station", "id", station_id)
return Dirble.StationDetails(d["id"], d["name"], d["streamurl"], d["country"],
d["bitrate"], d["status"], d["description"], d["added"],
d["urlid"], d["website"],
[Dirble.Song(s["name"], s["title"]) for s in d["songhistory"]])
API_KEY = "<YOUR API KEY HERE>"
BASE_URL = "http://api.dirble.com/v1"
d = Dirble(API_KEY, BASE_URL)
d.search("Foo")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment