2015-03-17 18:41:26 +00:00
|
|
|
#################################################################################################
|
|
|
|
# ReadKodiDB
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcaddon
|
|
|
|
import json
|
2015-03-18 15:47:55 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
|
|
|
addondir = xbmc.translatePath(addon.getAddonInfo('profile'))
|
|
|
|
dataPath = os.path.join(addondir,"library")
|
|
|
|
movieLibrary = os.path.join(dataPath,'movies')
|
|
|
|
tvLibrary = os.path.join(dataPath,'tvshows')
|
2015-03-17 18:41:26 +00:00
|
|
|
|
2015-03-19 10:13:49 +00:00
|
|
|
#sleepval is used to throttle the calls to the xbmc json API
|
|
|
|
sleepVal = 15
|
|
|
|
|
2015-03-17 18:41:26 +00:00
|
|
|
class ReadKodiDB():
|
|
|
|
def getKodiMovie(self, id):
|
2015-03-18 15:47:55 +00:00
|
|
|
#returns a single movie from Kodi db selected on MB item ID
|
2015-03-19 10:13:49 +00:00
|
|
|
xbmc.sleep(sleepVal)
|
2015-03-17 18:41:26 +00:00
|
|
|
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies", "params": { "filter": {"operator": "contains", "field": "path", "value": "' + id + '"}, "properties" : ["art", "rating", "thumbnail", "resume", "runtime", "year", "genre", "cast", "trailer", "country", "studio", "set", "imdbnumber", "mpaa", "tagline", "plotoutline","plot", "sorttitle", "director", "writer", "playcount", "tag", "file"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true } }, "id": "libMovies"}')
|
|
|
|
jsonobject = json.loads(json_response.decode('utf-8','replace'))
|
|
|
|
movie = None
|
|
|
|
|
|
|
|
if(jsonobject.has_key('result')):
|
|
|
|
result = jsonobject['result']
|
|
|
|
if(result.has_key('movies')):
|
|
|
|
movies = result['movies']
|
|
|
|
movie = movies[0]
|
|
|
|
|
|
|
|
return movie
|
|
|
|
|
2015-03-18 15:47:55 +00:00
|
|
|
def getKodiMovies(self,fullInfo = False):
|
|
|
|
#returns all movies in Kodi db inserted by MB
|
2015-03-19 10:13:49 +00:00
|
|
|
xbmc.sleep(sleepVal)
|
2015-03-18 15:47:55 +00:00
|
|
|
if fullInfo:
|
|
|
|
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies", "params": { "filter": {"operator": "contains", "field": "path", "value": "plugin.video.mb3sync"}, "properties" : ["art", "rating", "thumbnail", "resume", "runtime", "year", "genre", "cast", "trailer", "country", "studio", "set", "imdbnumber", "mpaa", "tagline", "plotoutline","plot", "sorttitle", "director", "writer", "playcount", "tag", "file"] }, "id": "libMovies"}')
|
|
|
|
else:
|
|
|
|
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies", "params": { "filter": {"operator": "contains", "field": "path", "value": "plugin.video.mb3sync"}, "properties" : ["resume", "playcount", "file"] }, "id": "libMovies"}')
|
|
|
|
jsonobject = json.loads(json_response.decode('utf-8','replace'))
|
|
|
|
movies = None
|
|
|
|
|
|
|
|
if(jsonobject.has_key('result')):
|
|
|
|
result = jsonobject['result']
|
|
|
|
if(result.has_key('movies')):
|
|
|
|
movies = result['movies']
|
|
|
|
|
2015-03-20 03:04:54 +00:00
|
|
|
kodiMovieMap = None
|
|
|
|
if(movies != None and len(movies) > 0):
|
|
|
|
kodiMovieMap = {}
|
|
|
|
for kodimovie in movies:
|
|
|
|
key = kodimovie["file"][-37:-5] #extract the id from the file name
|
|
|
|
kodiMovieMap[key] = kodimovie
|
|
|
|
|
|
|
|
return kodiMovieMap
|
2015-03-18 15:47:55 +00:00
|
|
|
|
|
|
|
def getKodiMoviesIds(self,returnMB3Ids = False):
|
|
|
|
# returns a list of movieIds or MB3 Id's from all movies currently in the Kodi library
|
|
|
|
allKodiMovies = self.getKodiMovies(False)
|
|
|
|
|
2015-03-20 03:04:54 +00:00
|
|
|
if(allKodiMovies == None):
|
|
|
|
return list()
|
|
|
|
|
|
|
|
if(returnMB3Ids):
|
|
|
|
allKodiMovieIds = list(allKodiMovies.keys())
|
|
|
|
return allKodiMovieIds
|
|
|
|
else:
|
|
|
|
allKodiMovieIds = list()
|
|
|
|
for kodimovie in allKodiMovies.values():
|
|
|
|
id = str(kodimovie["movieid"])
|
2015-03-18 15:47:55 +00:00
|
|
|
allKodiMovieIds.append(id)
|
|
|
|
|
2015-03-20 03:04:54 +00:00
|
|
|
return allKodiMovieIds
|
2015-03-18 15:47:55 +00:00
|
|
|
|
2015-03-18 17:00:38 +00:00
|
|
|
def getKodiTvShowsIds(self,returnMB3Ids = False):
|
|
|
|
# returns a list of tvshowIds or MB3 Id's from all tvshows currently in the Kodi library
|
|
|
|
allKodiTvShows = self.getKodiTvShows(False)
|
|
|
|
allKodiTvShowsIds = list()
|
|
|
|
|
|
|
|
if allKodiTvShows != None:
|
|
|
|
for kodishow in allKodiTvShows:
|
|
|
|
if returnMB3Ids:
|
|
|
|
filepath = kodishow["file"]
|
|
|
|
filepath = filepath.replace(tvLibrary + os.sep, "")
|
|
|
|
filepath = filepath.replace(".strm", "")
|
|
|
|
filepath = filepath.split(os.sep)[0]
|
|
|
|
id = filepath
|
|
|
|
else:
|
2015-03-18 17:43:57 +00:00
|
|
|
id = str(kodishow["tvshowid"])
|
2015-03-18 17:00:38 +00:00
|
|
|
allKodiTvShowsIds.append(id)
|
|
|
|
|
|
|
|
return allKodiTvShowsIds
|
2015-03-20 10:08:22 +00:00
|
|
|
|
2015-03-18 17:00:38 +00:00
|
|
|
def getKodiTvShows(self,fullInfo = False):
|
|
|
|
#returns all tvshows in Kodi db inserted by MB
|
2015-03-19 10:13:49 +00:00
|
|
|
xbmc.sleep(sleepVal)
|
2015-03-18 17:00:38 +00:00
|
|
|
if fullInfo:
|
|
|
|
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetTVShows", "params": { "filter": {"operator": "contains", "field": "path", "value": "plugin.video.mb3sync"}, "properties": ["art", "genre", "plot", "mpaa", "cast", "studio", "sorttitle", "title", "originaltitle", "imdbnumber", "year", "premiered", "rating", "thumbnail", "playcount", "file", "fanart"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true } }, "id": "libTvShows"}')
|
|
|
|
else:
|
|
|
|
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetTVShows", "params": { "filter": {"operator": "contains", "field": "path", "value": "plugin.video.mb3sync"}, "properties": ["sorttitle", "title", "playcount", "file"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true } }, "id": "libTvShows"}')
|
|
|
|
jsonobject = json.loads(json_response.decode('utf-8','replace'))
|
|
|
|
tvshows = None
|
2015-03-18 17:43:57 +00:00
|
|
|
|
2015-03-18 17:00:38 +00:00
|
|
|
if(jsonobject.has_key('result')):
|
|
|
|
result = jsonobject['result']
|
|
|
|
if(result.has_key('tvshows')):
|
2015-03-18 17:43:57 +00:00
|
|
|
tvshows = result['tvshows']
|
2015-03-18 17:00:38 +00:00
|
|
|
|
|
|
|
return tvshows
|
|
|
|
|
2015-03-17 18:41:26 +00:00
|
|
|
def getKodiTVShow(self, id):
|
2015-03-19 10:13:49 +00:00
|
|
|
xbmc.sleep(sleepVal)
|
2015-03-17 18:41:26 +00:00
|
|
|
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetTVShows", "params": { "filter": {"operator": "contains", "field": "path", "value": "' + id + '"}, "properties": ["art", "genre", "plot", "mpaa", "cast", "studio", "sorttitle", "title", "originaltitle", "imdbnumber", "year", "premiered", "rating", "thumbnail", "playcount", "file", "fanart"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true } }, "id": "libTvShows"}')
|
|
|
|
jsonobject = json.loads(json_response.decode('utf-8','replace'))
|
|
|
|
tvshow = None
|
|
|
|
if(jsonobject.has_key('result')):
|
|
|
|
result = jsonobject['result']
|
|
|
|
if(result.has_key('tvshows')):
|
|
|
|
tvshows = result['tvshows']
|
|
|
|
tvshow = tvshows[0]
|
|
|
|
return tvshow
|
|
|
|
|
2015-03-19 13:18:20 +00:00
|
|
|
def getKodiEpisodes(self, id, fullInfo = True):
|
2015-03-19 10:13:49 +00:00
|
|
|
xbmc.sleep(sleepVal)
|
2015-03-17 18:41:26 +00:00
|
|
|
episodes = None
|
2015-03-19 09:52:21 +00:00
|
|
|
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetTVShows", "params": { "filter": {"operator": "contains", "field": "path", "value": "' + id + '"}, "properties": ["title", "file"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true } }, "id": "libTvShows"}')
|
2015-03-17 18:41:26 +00:00
|
|
|
jsonobject = json.loads(json_response.decode('utf-8','replace'))
|
|
|
|
tvshow = None
|
|
|
|
if(jsonobject.has_key('result')):
|
|
|
|
result = jsonobject['result']
|
|
|
|
if(result.has_key('tvshows')):
|
|
|
|
tvshows = result['tvshows']
|
|
|
|
tvshow = tvshows[0]
|
2015-03-19 09:52:21 +00:00
|
|
|
if fullInfo:
|
|
|
|
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetEpisodes", "params": {"tvshowid": %d, "properties": ["title", "playcount", "plot", "season", "episode", "showtitle", "file", "lastplayed", "rating", "resume", "art", "streamdetails", "firstaired", "runtime", "writer", "cast", "dateadded"], "sort": {"method": "episode"}}, "id": 1}' %tvshow['tvshowid'])
|
|
|
|
else:
|
|
|
|
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetEpisodes", "params": {"tvshowid": %d, "properties": ["title", "playcount", "season", "episode", "lastplayed", "resume"], "sort": {"method": "episode"}}, "id": 1}' %tvshow['tvshowid'])
|
2015-03-17 18:41:26 +00:00
|
|
|
jsonobject = json.loads(json_response.decode('utf-8','replace'))
|
|
|
|
episodes = None
|
|
|
|
if(jsonobject.has_key('result')):
|
|
|
|
result = jsonobject['result']
|
|
|
|
if(result.has_key('episodes')):
|
|
|
|
episodes = result['episodes']
|
2015-03-19 13:18:20 +00:00
|
|
|
|
|
|
|
episodeMap = None
|
|
|
|
if(episodes != None):
|
|
|
|
episodeMap = {}
|
|
|
|
for KodiItem in episodes:
|
|
|
|
key = str(KodiItem["season"]) + "-" + str(KodiItem["episode"])
|
|
|
|
episodeMap[key] = KodiItem
|
|
|
|
|
|
|
|
return episodeMap
|
2015-03-17 18:41:26 +00:00
|
|
|
|
|
|
|
def getKodiEpisodeByMbItem(self, MBitem):
|
2015-03-19 10:13:49 +00:00
|
|
|
xbmc.sleep(sleepVal)
|
2015-03-17 18:41:26 +00:00
|
|
|
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetTVShows", "params": { "filter": {"operator": "is", "field": "title", "value": "' + MBitem.get("SeriesName").encode('utf-8') + '"} }, "id": "libTvShows"}')
|
|
|
|
jsonobject = json.loads(json_response.decode('utf-8','replace'))
|
|
|
|
episode = None
|
|
|
|
if(jsonobject.has_key('result')):
|
|
|
|
result = jsonobject['result']
|
|
|
|
if(result.has_key('tvshows')):
|
|
|
|
tvshows = result['tvshows']
|
|
|
|
tvshow = tvshows[0]
|
|
|
|
|
|
|
|
# find the episode by combination of season and episode
|
|
|
|
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetEpisodes", "params": {"tvshowid": %d, "properties": ["playcount","season", "resume", "episode"], "sort": {"method": "episode"}}, "id": 1}' %tvshow['tvshowid'])
|
|
|
|
jsonobject = json.loads(json_response.decode('utf-8','replace'))
|
|
|
|
episodes = None
|
|
|
|
if(jsonobject.has_key('result')):
|
|
|
|
result = jsonobject['result']
|
|
|
|
if(result.has_key('episodes')):
|
|
|
|
episodes = result['episodes']
|
|
|
|
|
|
|
|
comparestring1 = str(MBitem.get("ParentIndexNumber")) + "-" + str(MBitem.get("IndexNumber"))
|
|
|
|
for item in episodes:
|
|
|
|
comparestring2 = str(item["season"]) + "-" + str(item["episode"])
|
|
|
|
if comparestring1 == comparestring2:
|
|
|
|
episode = item
|
|
|
|
|
|
|
|
return episode
|
|
|
|
|