Move jsonrpc function
This commit is contained in:
parent
34cd0fadb4
commit
dfdc6eefd0
2 changed files with 69 additions and 67 deletions
|
@ -2,7 +2,42 @@
|
||||||
Collection of functions using the Kodi JSON RPC interface.
|
Collection of functions using the Kodi JSON RPC interface.
|
||||||
See http://kodi.wiki/view/JSON-RPC_API
|
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():
|
def get_players():
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
###############################################################################
|
###############################################################################
|
||||||
import logging
|
import logging
|
||||||
from cProfile import Profile
|
from cProfile import Profile
|
||||||
from json import loads, dumps
|
|
||||||
from pstats import Stats
|
from pstats import Stats
|
||||||
from sqlite3 import connect, OperationalError
|
from sqlite3 import connect, OperationalError
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
@ -1083,70 +1082,38 @@ class Lock_Function:
|
||||||
# UNUSED METHODS
|
# UNUSED METHODS
|
||||||
|
|
||||||
|
|
||||||
def changePlayState(itemType, kodiId, playCount, lastplayed):
|
# def changePlayState(itemType, kodiId, playCount, lastplayed):
|
||||||
"""
|
# """
|
||||||
YET UNUSED
|
# YET UNUSED
|
||||||
|
|
||||||
kodiId: int or str
|
# kodiId: int or str
|
||||||
playCount: int or str
|
# playCount: int or str
|
||||||
lastplayed: str or int unix timestamp
|
# lastplayed: str or int unix timestamp
|
||||||
"""
|
# """
|
||||||
lastplayed = DateToKodi(lastplayed)
|
# lastplayed = DateToKodi(lastplayed)
|
||||||
|
|
||||||
kodiId = int(kodiId)
|
# kodiId = int(kodiId)
|
||||||
playCount = int(playCount)
|
# playCount = int(playCount)
|
||||||
method = {
|
# method = {
|
||||||
'movie': ' VideoLibrary.SetMovieDetails',
|
# 'movie': ' VideoLibrary.SetMovieDetails',
|
||||||
'episode': 'VideoLibrary.SetEpisodeDetails',
|
# 'episode': 'VideoLibrary.SetEpisodeDetails',
|
||||||
'musicvideo': ' VideoLibrary.SetMusicVideoDetails', # TODO
|
# 'musicvideo': ' VideoLibrary.SetMusicVideoDetails', # TODO
|
||||||
'show': 'VideoLibrary.SetTVShowDetails', # TODO
|
# 'show': 'VideoLibrary.SetTVShowDetails', # TODO
|
||||||
'': 'AudioLibrary.SetAlbumDetails', # TODO
|
# '': 'AudioLibrary.SetAlbumDetails', # TODO
|
||||||
'': 'AudioLibrary.SetArtistDetails', # TODO
|
# '': 'AudioLibrary.SetArtistDetails', # TODO
|
||||||
'track': 'AudioLibrary.SetSongDetails'
|
# 'track': 'AudioLibrary.SetSongDetails'
|
||||||
}
|
# }
|
||||||
params = {
|
# params = {
|
||||||
'movie': {
|
# 'movie': {
|
||||||
'movieid': kodiId,
|
# 'movieid': kodiId,
|
||||||
'playcount': playCount,
|
# 'playcount': playCount,
|
||||||
'lastplayed': lastplayed
|
# 'lastplayed': lastplayed
|
||||||
},
|
# },
|
||||||
'episode': {
|
# 'episode': {
|
||||||
'episodeid': kodiId,
|
# 'episodeid': kodiId,
|
||||||
'playcount': playCount,
|
# 'playcount': playCount,
|
||||||
'lastplayed': lastplayed
|
# 'lastplayed': lastplayed
|
||||||
}
|
# }
|
||||||
}
|
# }
|
||||||
query = {
|
# result = jsonrpc(method[itemType]).execute(params[itemType])
|
||||||
"jsonrpc": "2.0",
|
# log.debug("JSON result was: %s" % result)
|
||||||
"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()))
|
|
||||||
|
|
Loading…
Reference in a new issue