Check if other clients are playing before...
#1
I got a simple question i think.
I have a script that performs certain actions when playback starts and stops. (checks for certain words in filename/path)
When playback stops it triggers a downloader to resume actions.
There is a reason i start and stop downloader tasks and the problem now is that when multiple clients are watching media they could have a negative effect when an other client stops playback and triggers downloader.

So my idea was to build a extra function in script to perform a isPlaying check on all other clients before sending resume command to downloader.
And i also need to keep in mind that some clients could be powered off.

Is there anyone that could help me create such function or kick me in the right direction?
It would be nice if it also could check for certain words/paths for other clients like i do now for active one but that's not important now.
For now i only want to do a check on all clients if they are playing something (and possible return back what they are playing), if other clients are playing media the script should stop.

This is the script i'm talking about https://www.dropbox.com/s/29o8i8r1o02pvr...nzbget.rar
Reply
#2
When you say "clients" do you mean other machines running xbmc?

If so, I'd imagine that a simple JSON request for "Player.GetActivePlayers" should be sufficient for your needs.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#3
Yes clients are other machines running XBMC.
Playing with JSON is rather new for me and not sure how to create such.

Going to test with this addon as suggested in xbmc wiki to see what happens
https://chrome.google.com/webstore/detai...bgofnpcjmb

Edit:
Tested it with Simple REST Client but it seems it doesn't return the needed info.
http://pastebin.com/4wGkJH7r
Reply
#4
That's not a JSON response - not sure how you tried testing it but it's definitely not the right response.

You could try a short python script using the code here (from this post)

Something as simple as:
Code:
from xbmcjson import XBMC

xbmc = XBMC("http://YOURCLIENTIP:PORT/jsonrpc")
print xbmc.Player.GetActivePlayers()
should be enough to show you the response you can get.

Given that you want to look at a number of clients, I think you'll need to use something like this, rather than xbmc's builtin executeJSONROC method.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#5
Yeah my JSON request was not correct. After reading most of the wiki and seeing some examples I figured out the right command.
I also noticed executeJSONROC but al the scripts/examples I found gave me the impression it's for local use only and not a remote host or am I wrong?

For example this:
json_query = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "AudioLibrary.GetAlbums", "params": {"properties": ["title", "year", "albumlabel", "playcount", "thumbnail"], "sort": { "method": "label" }, "filter": {"artistid": %s} }, "id": 1}' % dbid)

Anyway, thanks for you hints and tips Wink
Reply
#6
Using xbmc.executeJSONRPC is for local instances. However, you can use the module I linked to for testing remote clients.

For example, this should work within XBMC (untested code):
Code:
from xbmcjson import XBMC

remoteclients = ["http://192.168.0.10:8080/jsonrpc",
                 "http://192.168.0.20:8080/jsonrpc",
                 "http://192.168.0.30:8080/jsonrpc"]

for client in remoteclients:

    xbmcclient = XBMC(client)
    if xbmcclient.Player.GetActivePlayers()["result"]:
        # if "result" is not an empty list it will be True for the purposes of this test
        # Found an active player on one of your clients
        # so do something here...
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#7
Thanks, this will definitely help me.
Reply
#8
If you have any problems, just come back and ask for some more help.

Good luck!
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#9
I ran in a few issues with link you posted. It seems I need to add extra module to XBMC to get it running. And didn't get it working on a Windows system yet.
Nevertheless, you helped me a lot.

For now i'll just hardcode it for personal use and going to improve it later on to expand my knowledge.
I think this is the best way for me to play with the results from a JSON request now.

result = simplejson.load(urllib2.urlopen(url))

source: http://forum.xbmc.org/showthread.php?tid...pid1409354
Reply
#10
I would have thought you could have just dumped the module (https://github.com/jcsaaddupuy/python-xb...bmcjson.py) in your script folder to get it to run.

(I know the better way to do it is by having a resources folder but I'd worry about folder structure later and focus on getting your script working).

Ultimately, the module just makes the JSON request using urllib2 similar to the example you gave above.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#11
Yeah your right about putting file in script dir.
Why didn't I think about that myself.
Another attempt tomorrow, thanks again.
Reply
#12
I got it working, but want it to have it more specific.

This is the code I currently have for testing purpose:
Code:
xbmc_json = XBMC("http://localhost/jsonrpc")
            file_json = xbmc_json.Player.GetItem()

            print xbmc_json.JSONRPC.Ping()
        print xbmc_json.Player.GetItem({"playerid":1})
            print xbmc_json.Player.GetActivePlayers()

            item_json = xbmc_json.Player.GetItem({'playerid':1})
            Debug("item_json '%s'"% item_json)
        
            if item_json and 'movie' in item_json:
                Debug("###Movie found in JSON")
                return

It works and I see the results in xbmc.log:
Code:
15:18:32 T:5104   DEBUG: JSONRPC: Incoming request: {"params": {}, "jsonrpc": "2.0", "id": 1, "method": "JSONRPC.Ping"}
15:18:32 T:3948  NOTICE: {u'jsonrpc': u'2.0', u'id': 1, u'result': u'pong'}
15:18:33 T:5104   DEBUG: webserver: request received for /jsonrpc
15:18:33 T:5104   DEBUG: JSONRPC: Incoming request: {"params": {"playerid": 1}, "jsonrpc": "2.0", "id": 2, "method": "Player.GetItem"}
15:18:33 T:5104   DEBUG: CVideoDatabase::GetMovieId (D:\video\The.Bag.Man.2014.720p.BluRay.DTS.x264-PublicHD\The.Bag.Man.2014.720p.BluRay.DTS.x264-PublicHD.mp4), query = select idMovie from movie where idFile=1
15:18:33 T:3948  NOTICE: {u'jsonrpc': u'2.0', u'id': 2, u'result': {u'item': {u'type': u'movie', u'id': 1, u'label': u'The Bag Man'}}}
15:18:34 T:5104   DEBUG: webserver: request received for /jsonrpc
15:18:34 T:5104   DEBUG: JSONRPC: Incoming request: {"params": {}, "jsonrpc": "2.0", "id": 3, "method": "Player.GetActivePlayers"}
15:18:34 T:3948  NOTICE: {u'jsonrpc': u'2.0', u'id': 3, u'result': [{u'playerid': 1, u'type': u'video'}]}

15:18:35 T:5104   DEBUG: JSONRPC: Incoming request: {"params": {"playerid": 1, "properties": "movie"}, "jsonrpc": "2.0", "id": 4, "method": "Player.GetItem"}
15:18:35 T:3948  NOTICE: [Control Service NZBGet] item_json '{u'jsonrpc': u'2.0', u'id': 4, u'error': {u'message': u'Invalid params.', u'code': -32602, u'data': {u'method': u'Player.GetItem', u'stack': {u'message': u'Invalid type string received', u'type': u'array', u'name': u'properties'}}}}'

Only issue I have now is to check if movie or channel (LiveTV) is in results.
This is probably a Python thing but when I check other example codes like used in trakt.tv service addon it seems to be so simple except that it didn't work here.

if 'movie' in item_json:

Any tips/suggestions?
Reply
#13
I'm a little bit confused as to why the first GetItem request works and the second doesn't as the only difference seems to be single or double inverted commas, which shouldn't matter here.

However, it also seems that you're just passing the "playerid" parameter yet the second request received by XBMC also shows "properties": "movie" and I don't see how that's getting into the request.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#14
Yeah that's my bad. Shouldn't have pasted that much info.
Only relevant info is this:
15:18:34 T:3948 NOTICE: {u'jsonrpc': u'2.0', u'id': 3, u'result': [{u'playerid': 1, u'type': u'video'}]}

And now i wanted to do a check on the type field, is it movie, channel (liveTV) etc etc
I tried to handle it as a dictionary, array and string but it didn't worked to filter if there is type: movie or not.
Reply
#15
That's the result of the GetActivePlayers which is what my code in post #6 is testing.

I think you then need to use the Player.GetItem method with the playerID (which you can get from the above request). This seems to be what's being shown in this line:
Code:
15:18:33 T:3948  NOTICE: {u'jsonrpc': u'2.0', u'id': 2, u'result': {u'item': {u'type': u'movie', u'id': 1, u'label': u'The Bag Man'}}}
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply

Logout Mark Read Team Forum Stats Members Help
Check if other clients are playing before...0