fivebanger / kodi_addons

4 stars 0 forks source link

Easy access to radiostation via jsonrpc call? #3

Open Henriettel opened 4 months ago

Henriettel commented 4 months ago

UseCase: Trigger random radiostation from a custom set of radiostations.

Is it possible to have a simple call to the jsonrpc API like the old radio.de plugin which is not working anymore? The content of the body was: {"jsonrpc":"2.0","method":"Player.Open","params": { "item": { "file": "plugin://plugin.audio.radio_de/station/4284" } }, "id": 1}

where 4284 was the stationID from radio.de

However in this plugin the same call would have this body content: {"jsonrpc":"2.0","method":"Player.Open","params": { "item": { "file": "plugin://plugin.audio.radiode/?mode=play_stream&data=%7B%22id%22%3A+%22suedtirol1%22%2C+%22name%22%3A+%22S%5Cu00fcdtirol+1%22%2C+%22icon_url%22%3A+%22https%3A%2F%2Fd3kle7qwymxpcy.cloudfront.net%2Fimages%2Fbroadcasts%2F41%2Fb0%2F4284%2F1%2Fc300.png%22%2C+%22stream_url%22%3A+%22https%3A%2F%2Fice.creacast.com%2Fsudtirol1a%22%7D" } }, "id": 1}

Which is quite hard to parse and the url can change over time. Can you provide an easy way for the trigger via the stationID?

fivebanger commented 3 months ago

The new add-on gets called always using two parameters: "mode" and "data" whereas "data" is a html quoted JSON string. Calling the add-on in the same manner as the old one is not supported. The most recent stream_url is always fetched from radio.net right before calling the Kodi player, based on the given "id" (in order to prevent outdated stream_urls). Since v1.0.4 of this add-on only the "id" is needed when calling "play_stream", "name" and "icon_url" is also fetched from the most recent data set pulled from radio.net.

You can build the html quoted query part of the add-on call in many ways.

Most generic one would be:

import json
from urllib.parse import urlencode

data = {'id': 'suedtirol1'}
data = json.dumps(data)
mode = 'play_stream'
query = {'mode': mode, 'data': data}
encoded = urlencode(query)

Easiest one would be (including the full path to add-on):

station_id = 'suedtirol1'
addon_call = f'plugin://plugin.audio.radiode/?mode=play_stream&data=%7B%22id%22%3A+%22{station_id}%22%7D'

or simply write: addon_call = 'plugin://plugin.audio.radiode/?mode=play_stream&data=%7B%22id%22%3A+%22suedtirol1%22%7D'

For the proper "station_id" to call a station, pls. refer to the "stations.jsn" file located in the userdata space of Kodi and check "id" of a given station.

I have never checked to call the add-on using JSON rpc, but the above mentioned approaches should do the trick. Pls. give it a try.

Edit: It's not possible to call the play_stream() method by just providing "id". This raises an exception. I'll change the code in the next version accordingly.

fivebanger commented 3 months ago

New function "play_station" added in v1.1.2: Function can be called by just providing the station ID. Example for assembling the corresponding JSON-rpc call:

station_id = 'swr3'
addon_call = f'plugin://plugin.audio.radiode/?mode=play_station&data=%7B%22id%22%3A+%22{station_id}%22%7D'

The corresponding JSON-rpc call is:


{"jsonrpc":"2.0","method":"Player.Open","params": { "item": { "file": addon_call } }, "id": 1}
Henriettel commented 3 months ago

I'll give it a try.

Edit: Nice, this works great! Many thanks! 😍

What i found is

.kodi/userdata/addon_data/plugin.audio.radiode/recent.jsn

but no stations.jsn

fivebanger commented 3 months ago

You can also open the"recent.jsn" to get a valid "id". The "station.jsn" is the file that holds your "My stations" list and is placed under

.kodi/userdata/addonn_data/plugin.audio.radiode/.radiode"

by default (if you don't have specified a custom path in the add-on settings). Or play a station in the browser and check the address bar, last parameter of the URL represents the "station_id".