pythonrequestxbmckodi

Update XBMC library from Python script


I'm trying to write a script that will run the 'update library' command on my XBMC.

When I try to run:

url = 'http://root:libreelec@%IP_ADDRESS%:12345/jsonrpc?request={"jsonrpc": "2.0", "method": "VideoLibrary.Scan"}'
r = requests.get(url)

I see that the library is been updated but I'm getting the following error:

Traceback (most recent call last):
  File "<input>", line 2, in <module>
  File "C:\Python27\lib\site-packages\requests\api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Python27\lib\site-packages\requests\api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 596, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python27\lib\site-packages\requests\adapters.py", line 473, in send
    raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', BadStatusLine("''",))

I have tried to modify the code to:

    url = 'http://root:libreelec@%IP_ADDRESS%:12345/jsonrpc?'
    data = json.dumps({"request": {"jsonrpc": "2.0", "method": "VideoLibrary.Scan"}})
    r = requests.get(url, params=data)

but it doesn't seems to work, meaning I don't see the library updated.

What I'm doing wrong?


Solution

  • Who 'root' in the URL? Everything is much simple:

    url = 'http://libreelec:12345/jsonrpc'
    data = {"jsonrpc": "2.0", "method": "VideoLibrary.Scan", "id": "1"}
    r = requests.post(url, json=data)
    

    The /json endpoint accepts POST as well, and request.post will process a dict into a proper JSON request for you.