Index: channels/radiotray.py ================================================================== --- channels/radiotray.py +++ channels/radiotray.py @@ -1,9 +1,9 @@ -# api: dbus +# api: streamtuner2 # title: RadioTray hook # description: Allows to bookmark stations to RadioTray/NG -# version: 0.5 +# version: 0.6 # type: feature # category: bookmarks # depends: deb:python-dbus, deb:streamtuner2, deb:python-xdg # config: # { name: radiotray_map, type: select, value: "group", select: 'root|group|asis|play', description: 'Map genres to default RadioTray groups, or just "root".' } @@ -12,22 +12,25 @@ # id: streamtuner2-radiotray # extraction-method: xml # pack: radiotray.py # fpm-prefix: /usr/share/streamtuner2/channels/ # -# Adds a context menu "Keep in RadioTray.." for bookmarking. -# Until a newer version exposes addRadio(), this plugin -# will fall back to just playUrl(). +# Adds a context menu "Add in RadioTray.." for bookmarking. +# Should work with old Radiotray and Radiotray-NG. It will +# now probe for RTNG first and prefers to edit its bookmark +# list. # +# With the old Radiotry this plugin will fall back to just +# playUrl() until the addRadio remote call is added. # The patch for radiotray/DbusFacade.py would be: # + # + @dbus.service.method('net.sourceforge.radiotray') # + def addRadio(self, title, url, group="root"): # + self.dataProvider.addRadio(title, url, group) # -# Displays existing radiotray stations in ST2 bookmarks -# category as read from ~/.local/share/radiotray/bookmarks.xml. +# Displays existing radiotray stations in ST2 bookmarks (from +# either old bookmarks.xml or RT-NG bookmarks.json) # # This plugin may be packaged up separately. # from config import * @@ -124,21 +127,58 @@ def share(self, *w): row = self.parent.row() if row: group = self.map_group(row.get("genre")) log.PROC("mapping genre '%s' to RT group '%s'" % (row["genre"], group)) + # Radiotray-NG try: self.radiotray_ng().add_radio(row["title"], row["url"], group) except: - log.ERR("rtng dbus inactive") - # RadioTray doesn't have an addRadio method yet, so just fall back to play the stream URL + #try: + cfg = self.radiotray_ng().get_config() + self.save_rtng_json(cfg, row, group) + self.parent.status("Updated RT-NG bookmarks.json. Use Preferences>Reload Bookmarks..") + #except: + # log.ERR("radiotray-ng not active") + + # RadioTray doesn't have an addRadio method yet, so just fall back to play the stream URL + try: + self.radiotray().addRadio(row["title"], row["url"], group) + except: try: - self.radiotray().addRadio(row["title"], row["url"], group) - except: self.radiotray().playUrl(row["url"]) + except: + log.ERR("radiotry-old not active") pass + + # manually add to RTNG bookmarks.json + def save_rtng_json(self, cfg, row, group): + fn = json.loads(cfg)["bookmarks"] + j = json.load(open(fn, "r")) + found = None + # find existing group + for g in j: + if g["group"] == group or g["group"] == row["genre"]: + found = g + break + # else add new group + if not found: + found = { + "group": row["genre"], + "image": None, + "stations": [] + } + j.append(found) + # overwrite bookmarks.json + if found: + g["stations"].append({ + "image": None, + "name": row["title"], + "url": row["url"] + }) + json.dump(j, open(fn, "w"), indent=4) # match genre to RT groups def map_group(self, genre): if not genre or not len(genre) or conf.radiotray_map == "root": return "root"