Hi!
I'm trying to use JSON-RPC to monitor the current playing song and display it as
Desktop Notification.
I wrote a simple python script, using socket to send/recv JSON, which works, but I get a lot of socket.timeout errors, and I don't know why...
Also, I would like to avoid polling, and want to use notifications, as described
here. But I don't understand how to do that.
Does somebody have examples, based on socket or websocket, in python?
Thanks,
Either retry on the timeout and/or set the socket timeout to be disabled/infinite/very long time. There is a default:
from "socket.create_connection":
"Passing the optional timeout parameter will set the timeout on the socket instance before attempting to connect. If no timeout is supplied, the global default timeout setting returned by getdefaulttimeout() is used."
Yes, but I would like to know why it sometimes takes more than 20s to answer, and other times it answers immediatly...
(2015-05-07, 07:17)fmafma Wrote: [ -> ]Yes, but I would like to know why it sometimes takes more than 20s to answer, and other times it answers immediatly...
put up an example script and logs when it doesn't respond. TBH It sounds like a routing issue to me
Here is the basic script I'm using:
Code:
# -*- coding: utf-8 -*-
"""
"""
import time
import socket
import json
import notify2
JSON_REQUEST = {
"jsonrpc": "2.0",
"method": "Player.GetItem",
"params": {
"properties": [
"title"
],
"playerid": 0
},
"id": "AudioGetItem"
}
NOTIFIER_TIMEOUT = 30000 # ms
def main():
notify2.init("kodi notifier")
notifier = notify2.Notification("Kodi now playing:")
notifier.timeout = NOTIFIER_TIMEOUT
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
s.connect(("localhost", 9090))
s.settimeout(30)
oldTitle = None
while True:
try:
# get current playing title
start = time.time()
s.send(json.dumps(JSON_REQUEST))
ans = json.loads(s.recv(1024))
print "got answer after %ds" % (time.time() - start)
title = ans["result"]["item"]["title"]
if title != oldTitle:
oldTitle = title
parts = title.split(" - ")
notifier.update("Kodi now playing:", '\n'.join(parts))
notifier.show()
time.sleep(1)
except KeyError:
print "KeyError: ans=%s" % ans
except ValueError:
print "ValueError:"
except socket.timeout:
print "socket.timeout"
if __name__ == "__main__":
main()
About routing, I'm on the same machine, for my tests, so I don't think it is the problem.
But... this morning, it seems to works fine! Never have more than 3s.
I'm testing this script while listening to a web radio, using the "Radio" plugin; could it be related? I noticed that when Kodi is filling buffer, all the app hangs (which is annoying, I have to say!).
Even if it plays fine, it maybe the issue? Sometime the buffer is waiting for the stream, and Kofi hangs a few seconds...
While writing, I made a simple test by disconnecting the ethernet cable and I can reproduce the issue! So, it is the 'normal' hang of Kodi
Ok, so, now I would like to use notifications. Any idea how to do that?