Index: channels/radiobrowser.py ================================================================== --- channels/radiobrowser.py +++ channels/radiobrowser.py @@ -1,10 +1,10 @@ # encoding: UTF-8 # api: streamtuner2 # title: RadioBrowser # description: Community collection of stations; votes, clicks, homepage links. -# version: 0.1 +# version: 0.3 # type: channel # url: http://www.radio-browser.info/ # category: radio # priority: optional # config: @@ -40,10 +40,11 @@ import re import json from config import * from channels import * +from uikit import uikit import ahttp # API endpoints: # http://www.radio-browser.info/webservice/json/countries @@ -69,11 +70,16 @@ titles = dict(listeners="Votes+", bitrate="Votes-", playing="Country") base = "http://www.radio-browser.info/webservice/json/" categories = [] pricat = ("topvote", "topclick") catmap = { "tags": "bytag", "countries": "bycountry", "languages": "bylanguage" } - + + # hook menu + def __init__(self, parent): + ChannelPlugin.__init__(self, parent) + if parent: + uikit.add_menu([parent.streammenu, parent.streamactions], "Share in Radio-Browser", self.submit, insert=5) # votes, and tags, no countries or languages def update_categories(self): self.categories = list(self.pricat) for sub in [conf.radiobrowser_cat]: @@ -114,12 +120,37 @@ )) return r # fetch multiple pages - def api(self, method, params={}): - j = ahttp.get(self.base + method, params) + def api(self, method, params={}, post=False): + j = ahttp.get(self.base + method, params, post=post) try: return json.loads(j, strict=False) # some entries contain invalid character encodings except: return [] + + # Add radio station to RBI + def submit(self, *w): + cn = self.parent.channel() + row = cn.row() + # convert row from channel + data = dict( + name = row["title"], + url = row["url"], + homepage = row["homepage"], + #favicon = self.parent.favicon.html_link_icon(row["url"]), # no longer available as module + tags = row["genre"].replace(" ", ","), + ) + # map extra fields + for _from,_val,_to in [("playing","location","country")]: + #country Austria The name of the country where the radio station is located + #state Vienna The name of the part of the country where the station is located + #language English The main language which is used in spoken text parts of the radio station. + if _from in cn.titles and cn.titles[_from].lower() == _val: + data[_to] = _from + # API submit + j = self.api("add", data, post=1) + log.SUBMIT_RBI(j) + if j and "ok" in j and j["ok"] == "true" and "id" in j: + self.parent.status("Submitted successfully to Radio-Browser.info, new station id #%s." % j["id"], timeout=15)