From dfdc6eefd0b4703c1eeb6724ebb2ca76733e2281 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Fri, 8 Dec 2017 20:32:10 +0100 Subject: [PATCH] Move jsonrpc function --- resources/lib/json_rpc.py | 37 ++++++++++++++- resources/lib/utils.py | 99 +++++++++++++-------------------------- 2 files changed, 69 insertions(+), 67 deletions(-) diff --git a/resources/lib/json_rpc.py b/resources/lib/json_rpc.py index 5782dd4a..a965881d 100644 --- a/resources/lib/json_rpc.py +++ b/resources/lib/json_rpc.py @@ -2,7 +2,42 @@ Collection of functions using the Kodi JSON RPC interface. See http://kodi.wiki/view/JSON-RPC_API """ -from utils import jsonrpc, milliseconds_to_kodi_time +from json import loads, dumps +from utils import milliseconds_to_kodi_time +from xbmc import executeJSONRPC + + +class jsonrpc(object): + """ + Used for all Kodi JSON RPC calls. + """ + id_ = 1 + jsonrpc = "2.0" + + def __init__(self, method, **kwargs): + """ + Initialize with the Kodi method + """ + self.method = method + for arg in kwargs: # id_(int), jsonrpc(str) + self.arg = arg + + def _query(self): + query = { + 'jsonrpc': self.jsonrpc, + 'id': self.id_, + 'method': self.method, + } + if self.params is not None: + query['params'] = self.params + return dumps(query) + + def execute(self, params=None): + """ + Pass any params as a dict. Will return Kodi's answer as a dict. + """ + self.params = params + return loads(executeJSONRPC(self._query())) def get_players(): diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 52a39991..5184b4d2 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -3,7 +3,6 @@ ############################################################################### import logging from cProfile import Profile -from json import loads, dumps from pstats import Stats from sqlite3 import connect, OperationalError from datetime import datetime, timedelta @@ -1083,70 +1082,38 @@ class Lock_Function: # UNUSED METHODS -def changePlayState(itemType, kodiId, playCount, lastplayed): - """ - YET UNUSED +# def changePlayState(itemType, kodiId, playCount, lastplayed): +# """ +# YET UNUSED - kodiId: int or str - playCount: int or str - lastplayed: str or int unix timestamp - """ - lastplayed = DateToKodi(lastplayed) +# kodiId: int or str +# playCount: int or str +# lastplayed: str or int unix timestamp +# """ +# lastplayed = DateToKodi(lastplayed) - kodiId = int(kodiId) - playCount = int(playCount) - method = { - 'movie': ' VideoLibrary.SetMovieDetails', - 'episode': 'VideoLibrary.SetEpisodeDetails', - 'musicvideo': ' VideoLibrary.SetMusicVideoDetails', # TODO - 'show': 'VideoLibrary.SetTVShowDetails', # TODO - '': 'AudioLibrary.SetAlbumDetails', # TODO - '': 'AudioLibrary.SetArtistDetails', # TODO - 'track': 'AudioLibrary.SetSongDetails' - } - params = { - 'movie': { - 'movieid': kodiId, - 'playcount': playCount, - 'lastplayed': lastplayed - }, - 'episode': { - 'episodeid': kodiId, - 'playcount': playCount, - 'lastplayed': lastplayed - } - } - query = { - "jsonrpc": "2.0", - "id": 1, - } - query['method'] = method[itemType] - query['params'] = params[itemType] - result = xbmc.executeJSONRPC(dumps(query)) - result = loads(result) - result = result.get('result') - log.debug("JSON result was: %s" % result) - - -class jsonrpc(object): - id_ = 1 - jsonrpc = "2.0" - - def __init__(self, method, **kwargs): - self.method = method - for arg in kwargs: # id_(int), jsonrpc(str) - self.arg = arg - - def _query(self): - query = { - 'jsonrpc': self.jsonrpc, - 'id': self.id_, - 'method': self.method, - } - if self.params is not None: - query['params'] = self.params - return dumps(query) - - def execute(self, params=None): - self.params = params - return loads(xbmc.executeJSONRPC(self._query())) +# kodiId = int(kodiId) +# playCount = int(playCount) +# method = { +# 'movie': ' VideoLibrary.SetMovieDetails', +# 'episode': 'VideoLibrary.SetEpisodeDetails', +# 'musicvideo': ' VideoLibrary.SetMusicVideoDetails', # TODO +# 'show': 'VideoLibrary.SetTVShowDetails', # TODO +# '': 'AudioLibrary.SetAlbumDetails', # TODO +# '': 'AudioLibrary.SetArtistDetails', # TODO +# 'track': 'AudioLibrary.SetSongDetails' +# } +# params = { +# 'movie': { +# 'movieid': kodiId, +# 'playcount': playCount, +# 'lastplayed': lastplayed +# }, +# 'episode': { +# 'episodeid': kodiId, +# 'playcount': playCount, +# 'lastplayed': lastplayed +# } +# } +# result = jsonrpc(method[itemType]).execute(params[itemType]) +# log.debug("JSON result was: %s" % result)