add episode watched push to MBS from Kodi

This commit is contained in:
faush01 2015-03-18 13:45:15 +11:00
parent bc8037d686
commit 7bd7c10a0f
4 changed files with 60 additions and 44 deletions

View file

@ -39,9 +39,9 @@ class CreateFiles():
if item_type == "Episode": if item_type == "Episode":
itemPath = os.path.join(tvLibrary,parentId) itemPath = os.path.join(tvLibrary,parentId)
if str(item.get("IndexNumber")) != None: if str(item.get("IndexNumber")) != None:
filenamestr = self.CleanName(item.get("SeriesName")).encode('utf-8') + " S" + str(item.get("ParentIndexNumber")) + "E" + str(item.get("IndexNumber")) + ".strm" filenamestr = self.CleanName(item.get("SeriesName")).encode('utf-8') + " S" + str(item.get("ParentIndexNumber")) + "E" + str(item.get("IndexNumber")) + " (" + item["Id"] + ").strm"
else: else:
filenamestr = self.CleanName(item.get("SeriesName")).encode('utf-8') + " S0E0 " + item["Name"].decode('utf-8') + ".strm" filenamestr = self.CleanName(item.get("SeriesName")).encode('utf-8') + " S0E0 " + self.CleanName(item["Name"].decode('utf-8')) + " (" + item["Id"] + ").strm"
strmFile = os.path.join(itemPath,filenamestr) strmFile = os.path.join(itemPath,filenamestr)
changes = False changes = False
@ -81,12 +81,13 @@ class CreateFiles():
if item_type == "Episode": if item_type == "Episode":
itemPath = os.path.join(tvLibrary,parentId) itemPath = os.path.join(tvLibrary,parentId)
if str(item.get("ParentIndexNumber")) != None: if str(item.get("ParentIndexNumber")) != None:
filenamestr = self.CleanName(item.get("SeriesName")).encode('utf-8') + " S" + str(item.get("ParentIndexNumber")) + "E" + str(item.get("IndexNumber")) + ".nfo" filenamestr = self.CleanName(item.get("SeriesName")).encode('utf-8') + " S" + str(item.get("ParentIndexNumber")) + "E" + str(item.get("IndexNumber")) + " (" + item["Id"] + ").nfo"
else: else:
filenamestr = self.CleanName(item.get("SeriesName")).encode('utf-8') + " S0E0 " + item["Name"].decode('utf-8') + ".nfo" filenamestr = self.CleanName(item.get("SeriesName")).encode('utf-8') + " S0E0 " + self.CleanName(item["Name"].decode('utf-8')) + " (" + item["Id"] + ").nfo"
nfoFile = os.path.join(itemPath,filenamestr) nfoFile = os.path.join(itemPath,filenamestr)
rootelement = "episodedetails" rootelement = "episodedetails"
changes = False changes = False
if not xbmcvfs.exists(nfoFile): if not xbmcvfs.exists(nfoFile):
changes = True changes = True
@ -212,4 +213,6 @@ class CreateFiles():
def CleanName(self, name): def CleanName(self, name):
name = name.replace(":", "-") name = name.replace(":", "-")
name = name.replace("\\", "-")
name = name.replace("/", "-")
return name return name

View file

@ -9,11 +9,7 @@ import xbmcaddon
import json import json
import Utils as utils import Utils as utils
from LibrarySync import LibrarySync from WriteKodiDB import WriteKodiDB
librarySync = LibrarySync()
WINDOW = xbmcgui.Window( 10000 )
class Kodi_Monitor(xbmc.Monitor): class Kodi_Monitor(xbmc.Monitor):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -27,14 +23,16 @@ class Kodi_Monitor(xbmc.Monitor):
if method == "VideoLibrary.OnUpdate": if method == "VideoLibrary.OnUpdate":
#check windowprop if the sync is busy to prevent any false updates #check windowprop if the sync is busy to prevent any false updates
WINDOW = xbmcgui.Window( 10000 )
if WINDOW.getProperty("librarysync") != "busy": if WINDOW.getProperty("librarysync") != "busy":
xbmc.log("Kodi_Monitor -> onNotification -> VideoLibrary.OnUpdate : " + str(data))
jsondata = json.loads(data) jsondata = json.loads(data)
if jsondata != None: if jsondata != None:
playcount = None playcount = None
playcount = jsondata.get("playcount") playcount = jsondata.get("playcount")
item = jsondata.get("item").get("id") item = jsondata.get("item").get("id")
type = jsondata.get("item").get("type")
if playcount != None: if playcount != None:
librarySync.updatePlayCountFromKodi(item, playcount) WriteKodiDB().updatePlayCountFromKodi(item, type, playcount)

View file

@ -94,11 +94,12 @@ class ReadEmbyDB():
viewsUrl = server + "/mediabrowser/Users/" + userid + "/Views?format=json&ImageTypeLimit=1" viewsUrl = server + "/mediabrowser/Users/" + userid + "/Views?format=json&ImageTypeLimit=1"
jsonData = DownloadUtils().downloadUrl(viewsUrl, suppress=True, popup=0 ) jsonData = DownloadUtils().downloadUrl(viewsUrl, suppress=True, popup=0 )
collections=[]
if(jsonData != ""): if(jsonData != ""):
views = json.loads(jsonData) views = json.loads(jsonData)
views = views.get("Items") views = views.get("Items")
collections=[]
for view in views: for view in views:
if view.get("Type") == 'UserView': # Need to grab the real main node if view.get("Type") == 'UserView': # Need to grab the real main node
newViewsUrl = server + '/mediabrowser/Users/' + userid + '/items?ParentId=' + view.get("Id") + '&SortBy=SortName&SortOrder=Ascending&format=json&ImageTypeLimit=1' newViewsUrl = server + '/mediabrowser/Users/' + userid + '/items?ParentId=' + view.get("Id") + '&SortBy=SortName&SortOrder=Ascending&format=json&ImageTypeLimit=1'

View file

@ -26,36 +26,50 @@ tvLibrary = os.path.join(dataPath,'tvshows')
sleepVal = 10 sleepVal = 10
class WriteKodiDB(): class WriteKodiDB():
def updatePlayCountFromKodi(self, id, playcount=0):
#when user marks item watched from kodi interface update this to MB3
addon = xbmcaddon.Addon(id='plugin.video.mb3sync') def updatePlayCountFromKodi(self, id, type, playcount=0):
port = addon.getSetting('port') #when user marks item watched from kodi interface update this in MB3
host = addon.getSetting('ipaddress') xbmc.log("WriteKodiDB -> updatePlayCountFromKodi Called")
server = host + ":" + port
downloadUtils = DownloadUtils()
userid = downloadUtils.getUserId()
print "updateplaycount called!" mb3Id = None
if(type == "movie"):
mb3Id = None
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovieDetails", "params": { "movieid": ' + str(id) + ', "properties" : ["playcount", "file"] }, "id": "1"}')
if json_response != None:
jsonobject = json.loads(json_response.decode('utf-8','replace'))
if(jsonobject.has_key('result')):
result = jsonobject['result']
if(result.has_key('moviedetails')):
moviedetails = result['moviedetails']
filename = moviedetails.get("file").rpartition('\\')[2]
mb3Id = filename.replace(".strm","")
# TODO --> extend support for episodes elif(type == "episode"):
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovieDetails", "params": { "movieid": ' + str(id) + ', "properties" : ["playcount", "file"] }, "id": "1"}') mb3Id = None
if json_response != None: json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetEpisodeDetails", "params": { "episodeid": ' + str(id) + ', "properties" : ["playcount", "file"] }, "id": "1"}')
jsonobject = json.loads(json_response.decode('utf-8','replace')) if json_response != None:
movie = None jsonobject = json.loads(json_response.decode('utf-8','replace'))
if(jsonobject.has_key('result')): if(jsonobject.has_key('result')):
result = jsonobject['result'] result = jsonobject['result']
if(result.has_key('moviedetails')): if(result.has_key('episodedetails')):
moviedetails = result['moviedetails'] episodedetails = result['episodedetails']
filename = moviedetails.get("file").rpartition('\\')[2] filename = episodedetails.get("file").rpartition('\\')[2]
mb3Id = filename.replace(".strm","") mb3Id = filename[-38:-6]
watchedurl = 'http://' + server + '/mediabrowser/Users/' + userid + '/PlayedItems/' + mb3Id if(mb3Id != None):
utils.logMsg("MB3 Sync","watchedurl -->" + watchedurl) addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
if playcount != 0: port = addon.getSetting('port')
downloadUtils.downloadUrl(watchedurl, postBody="", type="POST") host = addon.getSetting('ipaddress')
else: server = host + ":" + port
downloadUtils.downloadUrl(watchedurl, type="DELETE") downloadUtils = DownloadUtils()
userid = downloadUtils.getUserId()
watchedurl = 'http://' + server + '/mediabrowser/Users/' + userid + '/PlayedItems/' + mb3Id
utils.logMsg("MB3 Sync","watchedurl -->" + watchedurl)
if playcount != 0:
downloadUtils.downloadUrl(watchedurl, postBody="", type="POST")
else:
downloadUtils.downloadUrl(watchedurl, type="DELETE")
def updateMovieToKodiLibrary( self, MBitem, KodiItem ): def updateMovieToKodiLibrary( self, MBitem, KodiItem ):