diff --git a/resources/lib/CreateFiles.py b/resources/lib/CreateFiles.py index 095a6157..e05bf84f 100644 --- a/resources/lib/CreateFiles.py +++ b/resources/lib/CreateFiles.py @@ -59,9 +59,12 @@ class CreateFiles(): xbmcvfs.mkdir(itemPath) text_file = open(strmFile, "w") - playUrl = "plugin://plugin.video.emby/?id=" + item["Id"] + '&mode=play' + port = addon.getSetting('port') + host = addon.getSetting('ipaddress') + server = host + ":" + port + playurl = PlayUtils().getPlayUrl(server, item["Id"], item) - text_file.writelines(playUrl) + text_file.writelines(playurl) text_file.close() #set timestamp on file - this will make sure that the dateadded field is properly set @@ -116,7 +119,7 @@ class CreateFiles(): root = Element(rootelement) SubElement(root, "id").text = item["Id"] SubElement(root, "uniqueid").text = item["Id"] - + if item.get("Tag") != None: for tag in item.get("Tag"): SubElement(root, "tag").text = tag @@ -149,7 +152,7 @@ class CreateFiles(): SubElement(root, "album").text = item["Album"] if item.has_key("Artist"): - SubElement(root, "artist").text = item["Artist"][0] + SubElement(root, "artist").text = utils.convertEncoding(item["Artist"][0]) if item.has_key("OfficialRating"): SubElement(root, "mpaa").text = item["OfficialRating"] diff --git a/resources/lib/KodiMonitor.py b/resources/lib/KodiMonitor.py index b29194b5..785b4be9 100644 --- a/resources/lib/KodiMonitor.py +++ b/resources/lib/KodiMonitor.py @@ -10,6 +10,8 @@ import json import Utils as utils from WriteKodiDB import WriteKodiDB +from ReadKodiDB import ReadKodiDB +from PlayUtils import PlayUtils from DownloadUtils import DownloadUtils class Kodi_Monitor(xbmc.Monitor): @@ -26,6 +28,62 @@ class Kodi_Monitor(xbmc.Monitor): port = addon.getSetting('port') host = addon.getSetting('ipaddress') server = host + ":" + port + downloadUtils = DownloadUtils() + + #player started playing an item - + if "Playlist.OnAdd" in method: + print "playlist onadd is called" + jsondata = json.loads(data) + if jsondata != None: + if jsondata.has_key("item"): + if jsondata.get("item").has_key("id") and jsondata.get("item").has_key("type"): + id = jsondata.get("item").get("id") + type = jsondata.get("item").get("type") + embyid = ReadKodiDB().getEmbyIdByKodiId(id,type) + + if embyid != None: + + WINDOW = xbmcgui.Window( 10000 ) + + userid = downloadUtils.getUserId() + jsonData = downloadUtils.downloadUrl("http://" + server + "/mediabrowser/Users/" + userid + "/Items/" + embyid + "?format=json&ImageTypeLimit=1", suppress=False, popup=1 ) + result = json.loads(jsonData) + userData = result.get("UserData") + + playurl = PlayUtils().getPlayUrl(server, embyid, result) + + watchedurl = 'http://' + server + '/mediabrowser/Users/'+ userid + '/PlayedItems/' + embyid + positionurl = 'http://' + server + '/mediabrowser/Users/'+ userid + '/PlayingItems/' + embyid + deleteurl = 'http://' + server + '/mediabrowser/Items/' + embyid + + # set the current playing info + WINDOW.setProperty(playurl+"watchedurl", watchedurl) + WINDOW.setProperty(playurl+"positionurl", positionurl) + WINDOW.setProperty(playurl+"deleteurl", "") + WINDOW.setProperty(playurl+"deleteurl", deleteurl) + if result.get("Type")=="Episode": + WINDOW.setProperty(playurl+"refresh_id", result.get("SeriesId")) + else: + WINDOW.setProperty(playurl+"refresh_id", embyid) + + WINDOW.setProperty(playurl+"runtimeticks", str(result.get("RunTimeTicks"))) + WINDOW.setProperty(playurl+"type", result.get("Type")) + WINDOW.setProperty(playurl+"item_id", embyid) + + if PlayUtils().isDirectPlay(result) == True: + playMethod = "DirectPlay" + else: + playMethod = "Transcode" + + WINDOW.setProperty(playurl+"playmethod", playMethod) + + mediaSources = result.get("MediaSources") + if(mediaSources != None): + if mediaSources[0].get('DefaultAudioStreamIndex') != None: + WINDOW.setProperty(playurl+"AudioStreamIndex", str(mediaSources[0].get('DefaultAudioStreamIndex'))) + if mediaSources[0].get('DefaultSubtitleStreamIndex') != None: + WINDOW.setProperty(playurl+"SubtitleStreamIndex", str(mediaSources[0].get('DefaultSubtitleStreamIndex'))) + if method == "VideoLibrary.OnUpdate": jsondata = json.loads(data) diff --git a/resources/lib/ReadKodiDB.py b/resources/lib/ReadKodiDB.py index 65766d2f..8bbad2b2 100644 --- a/resources/lib/ReadKodiDB.py +++ b/resources/lib/ReadKodiDB.py @@ -23,7 +23,7 @@ class ReadKodiDB(): def getKodiMovie(self, id): #returns a single movie from Kodi db selected on MB item ID xbmc.sleep(sleepVal) - 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", "lastplayed", "writer", "playcount", "tag", "file"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true } }, "id": "libMovies"}') + json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies", "params": { "filter": {"operator": "contains", "field": "imdbnumber ", "value": "' + id + '"}, "properties" : ["art", "rating", "thumbnail", "resume", "runtime", "year", "genre", "cast", "trailer", "country", "studio", "set", "imdbnumber", "mpaa", "tagline", "plotoutline","plot", "sorttitle", "director", "lastplayed", "writer", "playcount", "tag", "file"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true } }, "id": "libMovies"}') jsonobject = json.loads(json_response.decode('utf-8','replace')) movie = None @@ -35,13 +35,43 @@ class ReadKodiDB(): return movie + def getEmbyIdByKodiId(self, kodiid, type): + #returns the emby id by search on kodi id + xbmc.sleep(sleepVal) + + embyId = None + + if type == "movie": + json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovieDetails", "params": { "movieid": %d, "properties" : ["imdbnumber","file"] }, "id": "libMovies"}' %kodiid) + if type == "episode": + json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetEpisodeDetails", "params": {"episodeid": %d, "properties": ["file","uniqueid"]}, "id": 1}' %kodiid) + + if json_response != None: + jsonobject = json.loads(json_response.decode('utf-8','replace')) + if(jsonobject.has_key('result')): + result = jsonobject['result'] + resulttype = type + "details" + if(result.has_key(resulttype)): + item = result[resulttype] + if type == "movie": + if item.has_key('imdbnumber'): + embyId = item['imdbnumber'] + if type == "episode": + if item.has_key('uniqueid'): + if item['uniqueid'].has_key('unknown'): + embyId = item["uniqueid"]["unknown"] + + return embyId + + + def getKodiMovies(self,fullInfo = False): - #returns all movies in Kodi db inserted by MB + #returns all movies in Kodi db xbmc.sleep(sleepVal) if fullInfo: - json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies", "params": { "filter": {"operator": "contains", "field": "path", "value": "plugin.video.emby"}, "properties" : ["art", "rating", "thumbnail", "resume", "runtime", "year", "genre", "cast", "trailer", "country", "lastplayed", "studio", "set", "imdbnumber", "mpaa", "tagline", "plotoutline","plot", "sorttitle", "director", "writer", "playcount", "tag", "file"] }, "id": "libMovies"}') + json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies", "params": { "properties" : ["art", "rating", "thumbnail", "resume", "runtime", "year", "genre", "cast", "trailer", "country", "lastplayed", "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.emby"}, "properties" : ["resume", "playcount", "lastplayed", "file"] }, "id": "libMovies"}') + json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies", "params": { "properties" : ["resume", "playcount", "imdbnumber", "lastplayed", "file"] }, "id": "libMovies"}') jsonobject = json.loads(json_response.decode('utf-8','replace')) movies = None @@ -54,7 +84,7 @@ class ReadKodiDB(): if(movies != None and len(movies) > 0): kodiMovieMap = {} for kodimovie in movies: - key = kodimovie["file"][-37:-5] #extract the id from the file name + key = kodimovie["imdbnumber"] #extract the id from the imdbnumber kodiMovieMap[key] = kodimovie return kodiMovieMap diff --git a/resources/lib/WriteKodiDB.py b/resources/lib/WriteKodiDB.py index 9ede4d54..6bc8e7bd 100644 --- a/resources/lib/WriteKodiDB.py +++ b/resources/lib/WriteKodiDB.py @@ -13,6 +13,7 @@ import sqlite3 import os from DownloadUtils import DownloadUtils +from PlayUtils import PlayUtils from CreateFiles import CreateFiles from ReadKodiDB import ReadKodiDB from ReadEmbyDB import ReadEmbyDB @@ -95,7 +96,12 @@ class WriteKodiDB(): params = list() self.getArtworkParam_Batched(KodiItem, MBitem, params) - + + #set Filename + playurl = PlayUtils().getPlayUrl(server, MBitem["Id"], MBitem) + if playurl != KodiItem["file"]: + self.setKodiFilename(KodiItem["movieid"], playurl, "movie") + #update common properties duration = (int(timeInfo.get('Duration'))*60) self.getPropertyParam_Batched(KodiItem, "runtime", duration, params) @@ -312,6 +318,10 @@ class WriteKodiDB(): #add actors changes |= self.AddActorsToMedia(KodiItem,MBitem.get("People"),"movie") + #set Filename + playurl = PlayUtils().getPlayUrl(server, MBitem["Id"], MBitem) + self.setKodiFilename(KodiItem["movieid"], playurl, "movie") + CreateFiles().createSTRM(MBitem) CreateFiles().createNFO(MBitem) @@ -420,6 +430,10 @@ class WriteKodiDB(): #update/check all artwork changes |= self.updateArtWork(KodiItem,MBitem) + #set Filename + playurl = PlayUtils().getPlayUrl(server, MBitem["Id"], MBitem) + if playurl != KodiItem["file"]: + self.setKodiFilename(KodiItem["episodeid"], playurl, "episode") #update common properties duration = (int(timeInfo.get('Duration'))*60) @@ -443,7 +457,7 @@ class WriteKodiDB(): #add actors changes |= self.AddActorsToMedia(KodiItem,MBitem.get("People"),"episode") - + CreateFiles().createNFO(MBitem) CreateFiles().createSTRM(MBitem) @@ -868,6 +882,59 @@ class WriteKodiDB(): connection.commit() cursor.close() + + def setKodiFilename(self, id, filenameAndPath, fileType): + #use sqlite to set the filename in DB -- needed to avoid problems with resumepoints etc + #todo --> submit PR to kodi team to get this added to the jsonrpc api + + print "set filepath for id " + str(id) + " - " + filenameAndPath + + + + if "\\" in filenameAndPath: + filename = filenameAndPath.rsplit("\\",1)[-1] + path = filenameAndPath.replace(filename,"") + elif "/" in filenameAndPath: + filename = filenameAndPath.rsplit("/",1)[-1] + path = filenameAndPath.replace(filename,"") + else: + filename = filenameAndPath + path = None + + utils.logMsg("MB3 Sync","setting filename in kodi db..." + fileType + ": " + str(id)) + xbmc.sleep(sleepVal) + connection = utils.KodiSQL() + cursor = connection.cursor( ) + + if path != None: + cursor.execute("SELECT idPath as pathid FROM path WHERE strPath = ?",(path,)) + result = cursor.fetchone() + if result != None: + pathid = result[0] + if result == None: + cursor.execute("select coalesce(max(idPath),0) as pathid from path") + pathid = cursor.fetchone()[0] + pathid = pathid + 1 + pathsql="insert into path(idPath, strPath) values(?, ?)" + cursor.execute(pathsql, (pathid,path)) + + if fileType == "episode": + cursor.execute("SELECT idFile as fileidid FROM episode WHERE idEpisode = ?",(id,)) + result = cursor.fetchone() + fileid = result[0] + if fileType == "movie": + cursor.execute("SELECT idFile as fileidid FROM movie WHERE idMovie = ?",(id,)) + result = cursor.fetchone() + fileid = result[0] + + cursor.execute("UPDATE files SET strFilename = ? WHERE idFile = ?", (filename,fileid)) + cursor.execute("UPDATE files SET idPath = ? WHERE idFile = ?", (pathid,fileid)) + + connection.commit() + cursor.close() + + + def AddActorsToMedia(self, KodiItem, people, mediatype): #use sqlite to set add the actors while json api doesn't support this yet #todo --> submit PR to kodi team to get this added to the jsonrpc api