Kodi Community Forum
JSON-RPC (JSON Remote Procedure Call) interface protocol in development for XBMC - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Development (https://forum.kodi.tv/forumdisplay.php?fid=32)
+--- Forum: Kodi Application (https://forum.kodi.tv/forumdisplay.php?fid=93)
+---- Forum: JSON-RPC (https://forum.kodi.tv/forumdisplay.php?fid=174)
+---- Thread: JSON-RPC (JSON Remote Procedure Call) interface protocol in development for XBMC (/showthread.php?tid=68263)



- marksoccer - 2010-07-18

Is there a way to play a single song? When I try to use XBMC.Play using a songid, nothing happens. When I looked at the code for XBMC.Play, it didn't look like songids are supported. Is there a workaround to play one song?


- topfs2 - 2010-07-18

marksoccer Wrote:Is there a way to get a sorted list of artists based on artist name (rather than artistid) without manually sorting after receiving the json string?

Also, if I decided to add some features and submit them in a patch (and stuff worked correctly), would my patch be accepted?

I don't have the code atm but you should use sortmethod and sortorder, so it should be something like this

Code:
{ "jsonrpc": "2.0", "method": "AudioLibrary.GetArtists", "params": { "sortmethod": "artist" }, "id": 1 }

If you have the code you can check out FileItemHandler for the sorting code, should be in Parsesomething IIRC.


marksoccer Wrote:Is there a way to play a single song? When I try to use XBMC.Play using a songid, nothing happens. When I looked at the code for XBMC.Play, it didn't look like songids are supported. Is there a workaround to play one song?

Nope, songid's aren't supported and nothing should give you that id even IIRC. You can use the real path though. i.e. XBMC.Play("smb://foo/bar.mp3")


- dstruktiv - 2010-07-18

Thanks for the help on getting images via http Smile

New question, is there a way to enable more indepth logging on XBMC's end? I'm trying to build a tree view of artists/albums/songs but when I query albums based on artist it ignores the parameters and returns all albums.

My app log outputs the JSON as it sends it (+44 is name of artist):

Code:
7/18/2010 10:39:41 PM - {
  "jsonrpc": "2.0",
  "method": "AudioLibrary.GetAlbums",
  "params": {
    "artist": "+44"
  },
  "id": 32
}

And XBMC Log - It doesn't fail to parse it, just seems to ignore the param:

Code:
22:39:39 T:1324 M:2024378368  NOTICE: WebServer: POST | /jsonrpc

It'd be interesting to see what exactly xbmc is receiving.

edit nvm I'm an idiot I needed to pass artistid not name Smile


- topfs2 - 2010-07-18

jsonrpc does not support searching (which getting albums based on an artist name is). You need to give GetAlbum an Artist ID. The Artist ID can be gotten from GetArtists.


- marksoccer - 2010-07-18

topfs2 Wrote:Nope, songid's aren't supported and nothing should give you that id even IIRC. You can use the real path though. i.e. XBMC.Play("smb://foo/bar.mp3")

Hmm. I got songids using AudioLibrary.GetSongs and providing an artistid and albumid. Thanks for the workaround though.


- Johnsel - 2010-07-20

Could anyone help me with implementing this in C#?
I currently do a WebRequest to the jsonrpc server and i do receive an answer, but how to parse it? I found json .net library (NewtonSoft), but how can i decode the JSON answer to say an array? Or should i parse them manually?


- Tolriq - 2010-07-20

Well perhaps just start reading the entire thread Smile

But for the moment the best json implementation i found is Jayrock


- Johnsel - 2010-07-20

Got it working, couldn't use Jayrock as i need it to be .net Compact compatible.

anyways, here's an example for usage with the NewtonSoft Json library

maybe it'll help someone else to start

Code:
//json webrequest

            WebRequest request = WebRequest.Create("http://192.168.1.71/jsonrpc");
            request.Method = "POST";
            string postData = "{\"jsonrpc\": \"2.0\", \"method\": \"AudioLibrary.GetArtists\", \"id\": 1}";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/json";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            WebResponse response = request.GetResponse();
            dataStream = response.GetResponseStream();

            // read the response
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();

            // parse it into a JObject

            JObject o = JObject.Parse(responseFromServer);


            //query the JObject to get all labels
            var artistLabels =
                from p in o.Value<JObject>("result")
                    .Value<JArray>("artists")
                    .Children<JObject>()
                select p.Value<string>("label");
            
            foreach (var label in artistLabels)
            {
                MessageBox.Show(label);
            }


            reader.Close();
            dataStream.Close();
            response.Close();



- dstruktiv - 2010-07-22

Started coding a .NET library to interface with the JSON-RPC API Smile

It's a mess and not everything's implemented but it's a start - Cheers Tolriq for the initial help:

http://code.google.com/p/xbmc-json/


- Bram77 - 2010-07-22

dstruktiv Wrote:Started coding a .NET library to interface with the JSON-RPC API Smile

It's a mess and not everything's implemented but it's a start - Cheers Tolriq for the initial help:

http://code.google.com/p/xbmc-json/

Looking great! Nice and clean coding. DO you think you can keep it Mono compatible so it can be used to create Linux/osX clients?


- Johnsel - 2010-07-22

goddammit, i just wrote a .net library too Angry

Yours is cleaner though Smile


- topfs2 - 2010-07-22

marksoccer Wrote:Is there a way to play a single song? When I try to use XBMC.Play using a songid, nothing happens. When I looked at the code for XBMC.Play, it didn't look like songids are supported. Is there a workaround to play one song?

I've fixed this now. You have the possibility to use songid when you add, play or whatever you wish to do.

Also I've made playlists be more of use, specifically virtual ones. The add of virtual files now is able to add anything xbmc.play is able to take.

So this is now possible:
playlist-virtual = Playlist.Create();
Playlist.Add(playlist-file = "/foo/bar.m3u", songid = 31, artistid = 1);
Playlist.Shuffle(playlist-virtual);
XBMC.Play(playlist-virtual);
Playlist.Destroy(playlist-virtual);

This code would create a virtual one which would have items from the playlistfile, every song from artistid=1 and the songid=31

Hopefully this will help abit. Note that the virtual playlists aren't linked to any client and isn't managed. i.e. it won't be destroyed when a client disconnect but when xbmc quits or destroy is called. Also its possible to share these playlists between clients.

After dharma I might look into virtual playlists which either timeout or are tied to a client and add a shareable playlist incase this is something useful?

Cheers,
Tobias


- dstruktiv - 2010-07-23

Bram77 Wrote:Looking great! Nice and clean coding. DO you think you can keep it Mono compatible so it can be used to create Linux/osX clients?

Cheers Smile It should be Mono compatible I can't think of any reason why it wouldn't be, but it relies on the Jayrock JSON Lib so if that's not compatible with Mono then Sad but the source is available for that too so we could look at modifying it if need be.

Doesn't get much easier than this Smile

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XbmcJson;
using Jayrock.Json;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            Xbmc xbmc = new Xbmc("127.0.0.1", 80, "", "");

            JsonObject query = xbmc.VideoLibrary.GetMovies();

            foreach (JsonObject item in (JsonArray)query["movies"])
            {
                Console.WriteLine(item["label"].ToString());
            }

            Console.ReadLine();
        }
    }
}



json rpc and remote comands - noem - 2010-07-23

Hello,
Is there any way to do a UP/DOWN PLAY NEXT events thru the json-rpc or do you have to do it by event server ?


- dstruktiv - 2010-07-23

noem Wrote:Hello,
Is there any way to do a UP/DOWN PLAY NEXT events thru the json-rpc or do you have to do it by event server ?

Yes. AudioPlayer.PlayPause, AudioPlayer.Stop, AudioPlayer.SkipPrevious and AudioPlayer.SkipNext. Also works for VideoPlayer.