2015-03-13 21:24:59 +00:00
|
|
|
#################################################################################################
|
|
|
|
# LibrarySync
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcaddon
|
|
|
|
import xbmcvfs
|
|
|
|
import json
|
2015-03-14 00:46:54 +00:00
|
|
|
import sqlite3
|
2015-03-13 21:24:59 +00:00
|
|
|
import threading
|
|
|
|
import urllib
|
|
|
|
from datetime import datetime, timedelta, time
|
|
|
|
import urllib2
|
|
|
|
import os
|
2015-03-13 22:39:35 +00:00
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
|
|
|
|
from xml.etree import ElementTree
|
|
|
|
from xml.dom import minidom
|
|
|
|
import xml.etree.cElementTree as ET
|
|
|
|
|
|
|
|
from API import API
|
|
|
|
import Utils as utils
|
|
|
|
from DownloadUtils import DownloadUtils
|
|
|
|
|
2015-03-14 22:33:16 +00:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
|
|
|
addondir = xbmc.translatePath(addon.getAddonInfo('profile'))
|
2015-03-13 21:24:59 +00:00
|
|
|
dataPath = os.path.join(addondir,"library")
|
2015-03-14 22:33:16 +00:00
|
|
|
movieLibrary = os.path.join(dataPath,'movies')
|
|
|
|
tvLibrary = os.path.join(dataPath,'tvshows')
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
class LibrarySync():
|
|
|
|
|
|
|
|
def syncDatabase(self):
|
2015-03-14 22:33:16 +00:00
|
|
|
|
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
2015-03-13 21:24:59 +00:00
|
|
|
WINDOW.setProperty("librarysync", "busy")
|
|
|
|
updateNeeded = False
|
|
|
|
|
|
|
|
allMovies = list()
|
2015-03-14 21:25:08 +00:00
|
|
|
movieData = self.getMovies(True)
|
|
|
|
|
|
|
|
if(movieData == None):
|
2015-03-14 22:33:16 +00:00
|
|
|
return False
|
2015-03-14 21:25:08 +00:00
|
|
|
|
|
|
|
for item in movieData:
|
2015-03-13 21:24:59 +00:00
|
|
|
if not item.get('IsFolder'):
|
|
|
|
kodiItem = self.getKodiMovie(item["Id"])
|
|
|
|
allMovies.append(item["Id"])
|
|
|
|
if kodiItem == None:
|
|
|
|
self.addMovieToKodiLibrary(item)
|
|
|
|
updateNeeded = True
|
|
|
|
else:
|
|
|
|
self.updateMovieToKodiLibrary(item, kodiItem)
|
2015-03-14 22:43:36 +00:00
|
|
|
|
|
|
|
allTVShows = list()
|
|
|
|
tvShowData = self.getTVShows(True)
|
|
|
|
|
|
|
|
if (tvShowData == None):
|
|
|
|
return
|
|
|
|
for item in tvShowData:
|
|
|
|
if item.get('IsFolder'):
|
|
|
|
kodiItem = self.getKodiTVShow(item["Id"])
|
|
|
|
allTVShows.append(item["Id"])
|
|
|
|
if kodiItem == None:
|
|
|
|
self.addTVShowToKodiLibrary(item)
|
|
|
|
updateNeeded = True
|
|
|
|
else:
|
|
|
|
self.updateTVShowToKodiLibrary(item, kodiItem)
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
cleanNeeded = False
|
|
|
|
# process deletes
|
|
|
|
allLocaldirs, filesMovies = xbmcvfs.listdir(movieLibrary)
|
|
|
|
allMB3Movies = set(allMovies)
|
|
|
|
for dir in allLocaldirs:
|
|
|
|
if not dir in allMB3Movies:
|
|
|
|
self.deleteMovieFromKodiLibrary(dir)
|
|
|
|
cleanneeded = True
|
|
|
|
|
2015-03-14 22:43:36 +00:00
|
|
|
allLocaldirs, filesTVShows = xbmcvfs.listdir(tvLibrary)
|
|
|
|
allMB3TVShows = set(allTVShows)
|
|
|
|
for dir in allLocaldirs:
|
|
|
|
if not dir in allMB3TVShows:
|
|
|
|
self.deleteTVShowFromKodiLibrary(dir)
|
|
|
|
cleanneeded = True
|
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
if cleanNeeded:
|
|
|
|
xbmc.executebuiltin("CleanLibrary(video)")
|
|
|
|
|
|
|
|
if updateNeeded:
|
|
|
|
xbmc.executebuiltin("UpdateLibrary(video)")
|
|
|
|
|
|
|
|
WINDOW.clearProperty("librarysync")
|
2015-03-14 22:33:16 +00:00
|
|
|
|
|
|
|
return True
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
def updatePlayCounts(self):
|
|
|
|
#update all playcounts from MB3 to Kodi library
|
|
|
|
|
2015-03-14 22:33:16 +00:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
2015-03-13 21:24:59 +00:00
|
|
|
WINDOW.setProperty("librarysync", "busy")
|
|
|
|
|
2015-03-14 21:25:08 +00:00
|
|
|
movieData = self.getMovies(True)
|
|
|
|
if(movieData == None):
|
2015-03-14 22:33:16 +00:00
|
|
|
return False
|
2015-03-14 21:25:08 +00:00
|
|
|
|
|
|
|
for item in movieData:
|
2015-03-13 21:24:59 +00:00
|
|
|
if not item.get('IsFolder'):
|
|
|
|
kodiItem = self.getKodiMovie(item["Id"])
|
|
|
|
userData=API().getUserData(item)
|
|
|
|
timeInfo = API().getTimeInfo(item)
|
|
|
|
if kodiItem != None:
|
|
|
|
if kodiItem['playcount'] != int(userData.get("PlayCount")):
|
|
|
|
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.SetMovieDetails", "params": { "movieid": %i, "playcount": %i}, "id": 1 }' %(kodiItem['movieid'], int(userData.get("PlayCount"))))
|
2015-03-14 17:23:45 +00:00
|
|
|
|
2015-03-14 00:46:54 +00:00
|
|
|
kodiresume = int(round(kodiItem['resume'].get("position")))
|
|
|
|
resume = int(round(float(timeInfo.get("ResumeTime"))))*60
|
|
|
|
total = int(round(float(timeInfo.get("TotalTime"))))*60
|
|
|
|
if kodiresume != resume:
|
|
|
|
self.setKodiResumePoint(kodiItem['movieid'],resume,total)
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
WINDOW.clearProperty("librarysync")
|
2015-03-14 22:33:16 +00:00
|
|
|
|
|
|
|
return True
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
def getMovies(self, fullinfo = False):
|
|
|
|
result = None
|
2015-03-14 22:33:16 +00:00
|
|
|
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
|
|
|
port = addon.getSetting('port')
|
|
|
|
host = addon.getSetting('ipaddress')
|
|
|
|
server = host + ":" + port
|
|
|
|
|
|
|
|
downloadUtils = DownloadUtils()
|
|
|
|
userid = downloadUtils.getUserId()
|
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
if fullinfo:
|
2015-03-14 17:23:45 +00:00
|
|
|
url = server + '/mediabrowser/Users/' + userid + '/Items?&SortBy=SortName&Fields=Path,Genres,SortName,Studios,Writer,ProductionYear,Taglines,CommunityRating,OfficialRating,CumulativeRunTimeTicks,Metascore,AirTime,DateCreated,MediaStreams,People,Overview&Recursive=true&SortOrder=Ascending&IncludeItemTypes=Movie&format=json&ImageTypeLimit=1'
|
2015-03-13 21:24:59 +00:00
|
|
|
else:
|
|
|
|
url = server + '/mediabrowser/Users/' + userid + '/Items?&SortBy=SortName&Fields=CumulativeRunTimeTicks&Recursive=true&SortOrder=Ascending&IncludeItemTypes=Movie&format=json&ImageTypeLimit=1'
|
|
|
|
|
|
|
|
jsonData = downloadUtils.downloadUrl(url, suppress=True, popup=0)
|
2015-03-14 21:25:08 +00:00
|
|
|
if jsonData != None and jsonData != "":
|
2015-03-13 21:24:59 +00:00
|
|
|
result = json.loads(jsonData)
|
|
|
|
if(result.has_key('Items')):
|
|
|
|
result = result['Items']
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2015-03-14 22:43:36 +00:00
|
|
|
def getTVShows(self, fullinfo = False):
|
|
|
|
result = None
|
|
|
|
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
|
|
|
port = addon.getSetting('port')
|
|
|
|
host = addon.getSetting('ipaddress')
|
|
|
|
server = host + ":" + port
|
|
|
|
|
|
|
|
downloadUtils = DownloadUtils()
|
|
|
|
userid = downloadUtils.getUserId()
|
|
|
|
|
|
|
|
if fullinfo:
|
|
|
|
url = server + '/mediabrowser/Users/' + userid + '/Items?&SortBy=SortName&Fields=Path,Genres,SortName,Studios,Writer,ProductionYear,Taglines,CommunityRating,OfficialRating,CumulativeRunTimeTicks,Metascore,AirTime,DateCreated,MediaStreams,People,Overview&Recursive=true&SortOrder=Ascending&IncludeItemTypes=Series&format=json&ImageTypeLimit=1'
|
|
|
|
else:
|
|
|
|
url = server + '/mediabrowser/Users/' + userid + '/Items?&SortBy=SortName&Fields=CumulativeRunTimeTicks&Recursive=true&SortOrder=Ascending&IncludeItemTypes=Series&format=json&ImageTypeLimit=1'
|
|
|
|
|
|
|
|
jsonData = downloadUtils.downloadUrl(url, suppress=True, popup=0)
|
|
|
|
if jsonData != None and jsonData != "":
|
|
|
|
result = json.loads(jsonData)
|
|
|
|
if(result.has_key('Items')):
|
|
|
|
result = result['Items']
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
def updatePlayCountFromKodi(self, id, playcount=0):
|
|
|
|
#when user marks item watched from kodi interface update this to MB3
|
2015-03-14 22:33:16 +00:00
|
|
|
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
|
|
|
port = addon.getSetting('port')
|
|
|
|
host = addon.getSetting('ipaddress')
|
|
|
|
server = host + ":" + port
|
|
|
|
downloadUtils = DownloadUtils()
|
|
|
|
userid = downloadUtils.getUserId()
|
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
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'))
|
|
|
|
movie = None
|
|
|
|
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","")
|
|
|
|
|
2015-03-14 22:33:16 +00:00
|
|
|
watchedurl = 'http://' + server + '/mediabrowser/Users/' + userid + '/PlayedItems/' + mb3Id
|
2015-03-13 21:24:59 +00:00
|
|
|
print "watchedurl -->" + watchedurl
|
|
|
|
if playcount != 0:
|
|
|
|
downloadUtils.downloadUrl(watchedurl, postBody="", type="POST")
|
|
|
|
else:
|
|
|
|
downloadUtils.downloadUrl(watchedurl, type="DELETE")
|
|
|
|
|
|
|
|
def updateMovieToKodiLibrary( self, MBitem, KodiItem ):
|
|
|
|
|
2015-03-14 22:33:16 +00:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
|
|
|
port = addon.getSetting('port')
|
|
|
|
host = addon.getSetting('ipaddress')
|
|
|
|
server = host + ":" + port
|
|
|
|
downloadUtils = DownloadUtils()
|
2015-03-15 01:18:29 +00:00
|
|
|
userid = downloadUtils.getUserId()
|
2015-03-14 22:33:16 +00:00
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
timeInfo = API().getTimeInfo(MBitem)
|
|
|
|
userData=API().getUserData(MBitem)
|
|
|
|
people = API().getPeople(MBitem)
|
2015-03-14 11:26:25 +00:00
|
|
|
genre = API().getGenre(MBitem)
|
2015-03-14 17:23:45 +00:00
|
|
|
studios = API().getStudios(MBitem)
|
2015-03-13 21:24:59 +00:00
|
|
|
mediaStreams=API().getMediaStreams(MBitem)
|
|
|
|
|
2015-03-14 18:10:27 +00:00
|
|
|
thumbPath = API().getArtwork(MBitem, "Primary")
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
utils.logMsg("Updating item to Kodi Library", MBitem["Id"] + " - " + MBitem["Name"])
|
|
|
|
|
|
|
|
#update artwork
|
2015-03-14 18:10:27 +00:00
|
|
|
self.updateArtWork(KodiItem,"poster", API().getArtwork(MBitem, "poster"),"movie")
|
|
|
|
self.updateArtWork(KodiItem,"clearlogo", API().getArtwork(MBitem, "Logo"),"movie")
|
|
|
|
self.updateArtWork(KodiItem,"clearart", API().getArtwork(MBitem, "Art"),"movie")
|
|
|
|
self.updateArtWork(KodiItem,"banner", API().getArtwork(MBitem, "Banner"),"movie")
|
|
|
|
self.updateArtWork(KodiItem,"landscape", API().getArtwork(MBitem, "Thumb"),"movie")
|
|
|
|
self.updateArtWork(KodiItem,"discart", API().getArtwork(MBitem, "Disc"),"movie")
|
|
|
|
self.updateArtWork(KodiItem,"fanart", API().getArtwork(MBitem, "Backdrop"),"movie")
|
2015-03-14 12:08:05 +00:00
|
|
|
|
|
|
|
#update common properties
|
2015-03-13 21:24:59 +00:00
|
|
|
duration = (int(timeInfo.get('Duration'))*60)
|
2015-03-14 12:08:05 +00:00
|
|
|
self.updateProperty(KodiItem,"runtime",duration,"movie")
|
|
|
|
self.updateProperty(KodiItem,"year",MBitem.get("ProductionYear"),"movie")
|
|
|
|
self.updateProperty(KodiItem,"mpaa",MBitem.get("OfficialRating"),"movie")
|
2015-03-14 17:23:45 +00:00
|
|
|
|
|
|
|
if MBitem.get("CriticRating") != None:
|
|
|
|
self.updateProperty(KodiItem,"rating",int(MBitem.get("CriticRating"))/10,"movie")
|
|
|
|
|
|
|
|
self.updateProperty(KodiItem,"plotoutline",MBitem.get("ShortOverview"),"movie")
|
|
|
|
self.updateProperty(KodiItem,"set",MBitem.get("TmdbCollectionName"),"movie")
|
|
|
|
self.updateProperty(KodiItem,"sorttitle",MBitem.get("SortName"),"movie")
|
|
|
|
|
|
|
|
if MBitem.get("ProviderIds") != None:
|
|
|
|
if MBitem.get("ProviderIds").get("Imdb") != None:
|
|
|
|
self.updateProperty(KodiItem,"imdbnumber",MBitem.get("ProviderIds").get("Imdb"),"movie")
|
|
|
|
|
|
|
|
# FIXME --> Taglines not returned by MB3 server !?
|
|
|
|
if MBitem.get("TagLines") != None:
|
|
|
|
self.updateProperty(KodiItem,"tagline",MBitem.get("TagLines")[0],"movie")
|
|
|
|
|
|
|
|
self.updatePropertyArray(KodiItem,"writer",people.get("Writer"),"movie")
|
|
|
|
self.updatePropertyArray(KodiItem,"director",people.get("Director"),"movie")
|
|
|
|
self.updatePropertyArray(KodiItem,"genre",MBitem.get("Genres"),"movie")
|
|
|
|
self.updatePropertyArray(KodiItem,"studio",studios,"movie")
|
|
|
|
|
|
|
|
# FIXME --> ProductionLocations not returned by MB3 server !?
|
|
|
|
self.updatePropertyArray(KodiItem,"country",MBitem.get("ProductionLocations"),"movie")
|
|
|
|
|
2015-03-14 11:26:25 +00:00
|
|
|
#trailer link
|
|
|
|
trailerUrl = None
|
|
|
|
if MBitem.get("LocalTrailerCount") != None and MBitem.get("LocalTrailerCount") > 0:
|
|
|
|
itemTrailerUrl = "http://" + server + "/mediabrowser/Users/" + userid + "/Items/" + MBitem.get("Id") + "/LocalTrailers?format=json"
|
2015-03-14 21:25:08 +00:00
|
|
|
jsonData = downloadUtils.downloadUrl(itemTrailerUrl, suppress=True, popup=0 )
|
|
|
|
if(jsonData != ""):
|
|
|
|
trailerItem = json.loads(jsonData)
|
|
|
|
trailerUrl = "plugin://plugin.video.mb3sync/?id=" + trailerItem[0].get("Id") + '&mode=play'
|
|
|
|
self.updateProperty(KodiItem,"trailer",trailerUrl,"movie")
|
2015-03-14 17:23:45 +00:00
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
#update strm file - TODO: only update strm when path has changed
|
|
|
|
self.createSTRM(MBitem["Id"])
|
|
|
|
|
2015-03-14 17:23:45 +00:00
|
|
|
#add actors
|
|
|
|
self.AddActorsToMedia(KodiItem,MBitem.get("People"),"movie")
|
|
|
|
|
2015-03-14 13:32:50 +00:00
|
|
|
#create nfo file if not exists
|
2015-03-13 21:24:59 +00:00
|
|
|
nfoFile = os.path.join(movieLibrary,MBitem["Id"],MBitem["Id"] + ".nfo")
|
|
|
|
if not xbmcvfs.exists(nfoFile):
|
|
|
|
self.createNFO(MBitem)
|
|
|
|
|
|
|
|
#update playcounts
|
|
|
|
if KodiItem['playcount'] != int(userData.get("PlayCount")):
|
|
|
|
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.SetMovieDetails", "params": { "movieid": %i, "playcount": %i}, "id": 1 }' %(KodiItem['movieid'], int(userData.get("PlayCount"))))
|
|
|
|
|
2015-03-14 22:43:36 +00:00
|
|
|
def updateTVShowToKodiLibrary( self, MBitem, KodiItem ):
|
|
|
|
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
|
|
|
port = addon.getSetting('port')
|
|
|
|
host = addon.getSetting('ipaddress')
|
|
|
|
server = host + ":" + port
|
|
|
|
downloadUtils = DownloadUtils()
|
|
|
|
|
|
|
|
timeInfo = API().getTimeInfo(MBitem)
|
|
|
|
userData=API().getUserData(MBitem)
|
|
|
|
people = API().getPeople(MBitem)
|
|
|
|
genre = API().getGenre(MBitem)
|
|
|
|
studios = API().getStudios(MBitem)
|
|
|
|
mediaStreams=API().getMediaStreams(MBitem)
|
|
|
|
|
|
|
|
thumbPath = API().getArtwork(MBitem, "Primary")
|
|
|
|
|
|
|
|
utils.logMsg("Updating item to Kodi Library", MBitem["Id"] + " - " + MBitem["Name"])
|
|
|
|
|
|
|
|
#update artwork
|
|
|
|
self.updateArtWork(KodiItem,"poster", API().getArtwork(MBitem, "poster"),"tvshow")
|
|
|
|
self.updateArtWork(KodiItem,"clearlogo", API().getArtwork(MBitem, "Logo"),"tvshow")
|
|
|
|
self.updateArtWork(KodiItem,"clearart", API().getArtwork(MBitem, "Art"),"tvshow")
|
|
|
|
self.updateArtWork(KodiItem,"banner", API().getArtwork(MBitem, "Banner"),"tvshow")
|
|
|
|
self.updateArtWork(KodiItem,"landscape", API().getArtwork(MBitem, "Thumb"),"tvshow")
|
|
|
|
self.updateArtWork(KodiItem,"discart", API().getArtwork(MBitem, "Disc"),"tvshow")
|
|
|
|
self.updateArtWork(KodiItem,"fanart", API().getArtwork(MBitem, "Backdrop"),"tvshow")
|
|
|
|
|
|
|
|
#update common properties
|
|
|
|
duration = (int(timeInfo.get('Duration'))*60)
|
|
|
|
self.updateProperty(KodiItem,"runtime",duration,"tvshow")
|
|
|
|
self.updateProperty(KodiItem,"year",MBitem.get("ProductionYear"),"tvshow")
|
|
|
|
self.updateProperty(KodiItem,"mpaa",MBitem.get("OfficialRating"),"tvshow")
|
|
|
|
|
|
|
|
if MBitem.get("CriticRating") != None:
|
|
|
|
self.updateProperty(KodiItem,"rating",int(MBitem.get("CriticRating"))/10,"tvshow")
|
|
|
|
|
|
|
|
self.updateProperty(KodiItem,"plotoutline",MBitem.get("ShortOverview"),"tvshow")
|
|
|
|
self.updateProperty(KodiItem,"sorttitle",MBitem.get("SortName"),"tvshow")
|
|
|
|
|
|
|
|
if MBitem.get("ProviderIds") != None:
|
|
|
|
if MBitem.get("ProviderIds").get("Imdb") != None:
|
|
|
|
self.updateProperty(KodiItem,"imdbnumber",MBitem.get("ProviderIds").get("Imdb"),"tvshow")
|
|
|
|
|
|
|
|
# FIXME --> Taglines not returned by MB3 server !?
|
|
|
|
if MBitem.get("TagLines") != None:
|
|
|
|
self.updateProperty(KodiItem,"tagline",MBitem.get("TagLines")[0],"tvshow")
|
|
|
|
|
|
|
|
self.updatePropertyArray(KodiItem,"writer",people.get("Writer"),"tvshow")
|
|
|
|
self.updatePropertyArray(KodiItem,"director",people.get("Director"),"tvshow")
|
|
|
|
self.updatePropertyArray(KodiItem,"genre",MBitem.get("Genres"),"tvshow")
|
|
|
|
self.updatePropertyArray(KodiItem,"studio",studios,"tvshow")
|
|
|
|
|
|
|
|
# FIXME --> ProductionLocations not returned by MB3 server !?
|
|
|
|
self.updatePropertyArray(KodiItem,"country",MBitem.get("ProductionLocations"),"tvshow")
|
|
|
|
|
|
|
|
#update strm file - TODO: only update strm when path has changed
|
|
|
|
self.createSTRM(MBitem["Id"])
|
|
|
|
|
|
|
|
#add actors
|
|
|
|
self.AddActorsToMedia(KodiItem,MBitem.get("People"),"tvshow")
|
|
|
|
|
|
|
|
#create nfo file if not exists
|
|
|
|
nfoFile = os.path.join(tvLibrary,MBitem["Id"],MBitem["Id"] + ".nfo")
|
|
|
|
if not xbmcvfs.exists(nfoFile):
|
|
|
|
self.createTVNFO(MBitem)
|
|
|
|
|
|
|
|
#update playcounts
|
|
|
|
if KodiItem['playcount'] != int(userData.get("PlayCount")):
|
|
|
|
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.SetTVShowDetails", "params": { "tvshowid": %i, "playcount": %i}, "id": 1 }' %(KodiItem['tvshowid'], int(userData.get("PlayCount"))))
|
2015-03-14 12:08:05 +00:00
|
|
|
|
|
|
|
# adds or updates artwork to the given Kodi file in database
|
|
|
|
def updateArtWork(self,KodiItem,artWorkName,artworkValue, fileType="movie"):
|
|
|
|
if fileType == "tvshow":
|
|
|
|
method = "VideoLibrary.SetTVShowDetails"
|
|
|
|
elif fileType == "episode":
|
|
|
|
method = "VideoLibrary.SetEpisodeDetails"
|
|
|
|
elif fileType == "musicvideo":
|
|
|
|
method = "VideoLibrary.SetMusicVideoDetails"
|
|
|
|
else:
|
|
|
|
method = "VideoLibrary.SetMovieDetails"
|
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
if KodiItem['art'].has_key(artWorkName):
|
2015-03-14 00:46:54 +00:00
|
|
|
curValue = urllib.unquote(KodiItem['art'][artWorkName]).decode('utf8')
|
|
|
|
if not artworkValue in curValue:
|
2015-03-14 12:08:05 +00:00
|
|
|
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "%s", "params": { "movieid": %i, "art": { "%s": "%s" }}, "id": 1 }' %(method,KodiItem['movieid'], artWorkName, artworkValue))
|
2015-03-14 00:46:54 +00:00
|
|
|
elif artworkValue != None:
|
2015-03-14 12:08:05 +00:00
|
|
|
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "%s", "params": { "movieid": %i, "art": { "%s": "%s" }}, "id": 1 }' %(method,KodiItem['movieid'], artWorkName, artworkValue))
|
|
|
|
|
|
|
|
# adds or updates the given property on the videofile in Kodi database
|
|
|
|
def updateProperty(self,KodiItem,propertyName,propertyValue,fileType="movie"):
|
|
|
|
if fileType == "tvshow":
|
|
|
|
method = "VideoLibrary.SetTVShowDetails"
|
|
|
|
elif fileType == "episode":
|
|
|
|
method = "VideoLibrary.SetEpisodeDetails"
|
|
|
|
elif fileType == "musicvideo":
|
|
|
|
method = "VideoLibrary.SetMusicVideoDetails"
|
|
|
|
else:
|
|
|
|
method = "VideoLibrary.SetMovieDetails"
|
|
|
|
|
2015-03-14 13:32:50 +00:00
|
|
|
if propertyValue != KodiItem[propertyName]:
|
2015-03-14 17:23:45 +00:00
|
|
|
if propertyValue != None:
|
|
|
|
if type(propertyValue) is int:
|
|
|
|
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "%s", "params": { "movieid": %i, "%s": %i}, "id": 1 }' %(method,KodiItem['movieid'], propertyName, propertyValue))
|
|
|
|
else:
|
|
|
|
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "%s", "params": { "movieid": %i, "%s": "%s"}, "id": 1 }' %(method,KodiItem['movieid'], propertyName, propertyValue.encode('utf-8')))
|
2015-03-14 12:08:05 +00:00
|
|
|
|
2015-03-14 17:23:45 +00:00
|
|
|
# adds or updates the property-array on the videofile in Kodi database
|
|
|
|
def updatePropertyArray(self,KodiItem,propertyName,propertyCollection,fileType="movie"):
|
2015-03-14 13:32:50 +00:00
|
|
|
if fileType == "tvshow":
|
|
|
|
method = "VideoLibrary.SetTVShowDetails"
|
|
|
|
elif fileType == "episode":
|
|
|
|
method = "VideoLibrary.SetEpisodeDetails"
|
|
|
|
elif fileType == "musicvideo":
|
|
|
|
method = "VideoLibrary.SetMusicVideoDetails"
|
|
|
|
else:
|
|
|
|
method = "VideoLibrary.SetMovieDetails"
|
|
|
|
|
|
|
|
pendingChanges = False
|
2015-03-14 17:23:45 +00:00
|
|
|
if propertyCollection != None:
|
|
|
|
currentvalues = set(KodiItem[propertyName])
|
2015-03-14 13:32:50 +00:00
|
|
|
genrestring = ""
|
2015-03-14 17:23:45 +00:00
|
|
|
for item in propertyCollection:
|
|
|
|
if not item in currentvalues:
|
2015-03-14 13:32:50 +00:00
|
|
|
pendingChanges = True
|
2015-03-14 17:23:45 +00:00
|
|
|
json_array = json.dumps(propertyCollection)
|
2015-03-14 13:32:50 +00:00
|
|
|
|
|
|
|
if pendingChanges:
|
2015-03-14 17:23:45 +00:00
|
|
|
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "%s", "params": { "movieid": %i, "%s": %s}, "id": 1 }' %(method,KodiItem['movieid'],propertyName,json_array))
|
2015-03-14 13:32:50 +00:00
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
def createSTRM(self,id):
|
|
|
|
|
|
|
|
itemPath = os.path.join(movieLibrary,id)
|
|
|
|
if not xbmcvfs.exists(itemPath):
|
|
|
|
xbmcvfs.mkdir(itemPath)
|
|
|
|
|
|
|
|
strmFile = os.path.join(itemPath,id + ".strm")
|
|
|
|
text_file = open(strmFile, "w")
|
|
|
|
|
|
|
|
playUrl = "plugin://plugin.video.mb3sync/?id=" + id + '&mode=play'
|
|
|
|
|
|
|
|
text_file.writelines(playUrl)
|
|
|
|
text_file.close()
|
|
|
|
|
|
|
|
def createNFO(self,item):
|
|
|
|
timeInfo = API().getTimeInfo(item)
|
|
|
|
userData=API().getUserData(item)
|
|
|
|
people = API().getPeople(item)
|
|
|
|
mediaStreams=API().getMediaStreams(item)
|
2015-03-15 01:18:29 +00:00
|
|
|
item_type=str(item.get("Type")).encode('utf-8')
|
|
|
|
|
|
|
|
if item_type == "Movie":
|
|
|
|
itemPath = os.path.join(movieLibrary,item["Id"])
|
|
|
|
nfoFile = os.path.join(itemPath,item["Id"] + ".nfo")
|
|
|
|
rootelement = "movie"
|
|
|
|
if item_type == "Series":
|
|
|
|
itemPath = os.path.join(tvLibrary,item["Id"])
|
|
|
|
nfoFile = os.path.join(itemPath,"tvshow.nfo")
|
|
|
|
rootelement = "tvshow"
|
|
|
|
if item_type == "Episode":
|
|
|
|
itemPath = os.path.join(tvLibrary,item["Id"])
|
|
|
|
nfoFile = os.path.join(itemPath,item["Id"] + ".nfo")
|
|
|
|
rootelement = "episodedetails"
|
|
|
|
|
|
|
|
root = Element(rootelement)
|
2015-03-13 21:24:59 +00:00
|
|
|
SubElement(root, "id").text = item["Id"]
|
2015-03-15 01:18:29 +00:00
|
|
|
#SubElement(root, "tag").text = # TODO --> use tags to assign user view
|
2015-03-14 18:10:27 +00:00
|
|
|
SubElement(root, "thumb").text = API().getArtwork(item, "poster")
|
|
|
|
SubElement(root, "fanart").text = API().getArtwork(item, "Backdrop")
|
2015-03-13 21:24:59 +00:00
|
|
|
SubElement(root, "title").text = item["Name"].encode('utf-8').decode('utf-8')
|
2015-03-13 22:39:35 +00:00
|
|
|
SubElement(root, "originaltitle").text = item["Name"].encode('utf-8').decode('utf-8')
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
SubElement(root, "year").text = str(item.get("ProductionYear"))
|
|
|
|
SubElement(root, "runtime").text = str(timeInfo.get('Duration'))
|
|
|
|
|
|
|
|
fileinfo = SubElement(root, "fileinfo")
|
|
|
|
streamdetails = SubElement(fileinfo, "streamdetails")
|
|
|
|
video = SubElement(streamdetails, "video")
|
2015-03-13 22:39:35 +00:00
|
|
|
SubElement(video, "duration").text = str(mediaStreams.get('totaltime'))
|
|
|
|
SubElement(video, "aspect").text = mediaStreams.get('aspectratio')
|
|
|
|
SubElement(video, "codec").text = mediaStreams.get('videocodec')
|
|
|
|
SubElement(video, "width").text = str(mediaStreams.get('width'))
|
|
|
|
SubElement(video, "height").text = str(mediaStreams.get('height'))
|
2015-03-13 21:24:59 +00:00
|
|
|
audio = SubElement(streamdetails, "audio")
|
2015-03-13 22:39:35 +00:00
|
|
|
SubElement(audio, "codec").text = mediaStreams.get('audiocodec')
|
|
|
|
SubElement(audio, "channels").text = mediaStreams.get('channels')
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
SubElement(root, "plot").text = API().getOverview(item).decode('utf-8')
|
|
|
|
|
|
|
|
art = SubElement(root, "art")
|
2015-03-14 18:10:27 +00:00
|
|
|
SubElement(art, "poster").text = API().getArtwork(item, "poster")
|
|
|
|
SubElement(art, "fanart").text = API().getArtwork(item, "Backdrop")
|
|
|
|
SubElement(art, "landscape").text = API().getArtwork(item, "Thumb")
|
|
|
|
SubElement(art, "clearlogo").text = API().getArtwork(item, "Logo")
|
|
|
|
SubElement(art, "discart").text = API().getArtwork(item, "Disc")
|
|
|
|
SubElement(art, "banner").text = API().getArtwork(item, "Banner")
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
ET.ElementTree(root).write(nfoFile, encoding="utf-8", xml_declaration=True)
|
|
|
|
|
|
|
|
def addMovieToKodiLibrary( self, item ):
|
|
|
|
itemPath = os.path.join(movieLibrary,item["Id"])
|
|
|
|
strmFile = os.path.join(itemPath,item["Id"] + ".strm")
|
|
|
|
|
|
|
|
utils.logMsg("Adding item to Kodi Library",item["Id"] + " - " + item["Name"])
|
|
|
|
|
|
|
|
#create path if not exists
|
2015-03-15 01:18:29 +00:00
|
|
|
if not xbmcvfs.exists(itemPath + os.sep):
|
2015-03-13 21:24:59 +00:00
|
|
|
xbmcvfs.mkdir(itemPath)
|
|
|
|
|
|
|
|
#create nfo file
|
|
|
|
self.createNFO(item)
|
|
|
|
|
|
|
|
# create strm file
|
|
|
|
self.createSTRM(item["Id"])
|
|
|
|
|
|
|
|
def deleteMovieFromKodiLibrary(self, id ):
|
|
|
|
kodiItem = self.getKodiMovie(id)
|
|
|
|
utils.logMsg("deleting movie from Kodi library",id)
|
|
|
|
if kodiItem != None:
|
|
|
|
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.RemoveMovie", "params": { "movieid": %i}, "id": 1 }' %(kodiItem["movieid"]))
|
|
|
|
|
|
|
|
path = os.path.join(movieLibrary,id)
|
|
|
|
xbmcvfs.rmdir(path)
|
2015-03-14 22:43:36 +00:00
|
|
|
|
|
|
|
def addTVShowToKodiLibrary( self, item ):
|
|
|
|
itemPath = os.path.join(tvLibrary,item["Id"])
|
|
|
|
utils.logMsg("Adding item to Kodi Library",item["Id"] + " - " + item["Name"])
|
|
|
|
|
|
|
|
#create path if not exists
|
2015-03-15 01:18:29 +00:00
|
|
|
if not xbmcvfs.exists(itemPath + os.sep):
|
2015-03-14 22:43:36 +00:00
|
|
|
xbmcvfs.mkdir(itemPath)
|
|
|
|
|
|
|
|
#create nfo file
|
2015-03-15 01:18:29 +00:00
|
|
|
self.createNFO(item)
|
2015-03-14 22:43:36 +00:00
|
|
|
|
|
|
|
def deleteTVShowFromKodiLibrary(self, id ):
|
|
|
|
kodiItem = self.getKodiTVShow(id)
|
|
|
|
utils.logMsg("deleting tvshow from Kodi library",id)
|
|
|
|
if kodiItem != None:
|
|
|
|
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.RemoveTVShow", "params": { "tvshowid": %i}, "id": 1 }' %(kodiItem["tvshowid"]))
|
|
|
|
|
|
|
|
path = os.path.join(tvLibrary,id)
|
|
|
|
xbmcvfs.rmdir(path)
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-03-14 00:46:54 +00:00
|
|
|
def setKodiResumePoint(self, id, resume_seconds, total_seconds):
|
|
|
|
#use sqlite to set the resume point while json api doesn't support this yet
|
|
|
|
#todo --> submit PR to kodi team to get this added to the jsonrpc api
|
|
|
|
dbPath = xbmc.translatePath("special://userdata/Database/MyVideos90.db")
|
|
|
|
connection = sqlite3.connect(dbPath)
|
|
|
|
cursor = connection.cursor( )
|
2015-03-15 01:18:29 +00:00
|
|
|
|
2015-03-14 00:46:54 +00:00
|
|
|
cursor.execute("delete FROM bookmark WHERE idFile = ?", (id,))
|
|
|
|
cursor.execute("select coalesce(max(idBookmark),0) as bookmarkId from bookmark")
|
|
|
|
bookmarkId = cursor.fetchone()[0]
|
|
|
|
bookmarkId = bookmarkId + 1
|
|
|
|
bookmarksql="insert into bookmark(idBookmark, idFile, timeInSeconds, totalTimeInSeconds, thumbNailImage, player, playerState, type) values(?, ?, ?, ?, ?, ?, ?, ?)"
|
|
|
|
cursor.execute(bookmarksql, (bookmarkId,id,resume_seconds,total_seconds,None,"DVDPlayer",None,1))
|
|
|
|
connection.commit()
|
|
|
|
cursor.close()
|
|
|
|
|
2015-03-14 17:23:45 +00:00
|
|
|
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
|
|
|
|
|
2015-03-14 22:33:16 +00:00
|
|
|
downloadUtils = DownloadUtils()
|
|
|
|
|
2015-03-14 17:23:45 +00:00
|
|
|
id = KodiItem["movieid"]
|
|
|
|
dbPath = xbmc.translatePath("special://userdata/Database/MyVideos90.db")
|
|
|
|
connection = sqlite3.connect(dbPath)
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
|
|
currentcast = list()
|
|
|
|
if KodiItem["cast"] != None:
|
|
|
|
for cast in KodiItem["cast"]:
|
|
|
|
currentcast.append(cast["name"])
|
|
|
|
|
|
|
|
if(people != None):
|
|
|
|
for person in people:
|
|
|
|
if(person.get("Type") == "Actor"):
|
|
|
|
if person.get("Name") not in currentcast:
|
|
|
|
Name = person.get("Name")
|
|
|
|
Role = person.get("Role")
|
|
|
|
actorid = None
|
|
|
|
Thumb = downloadUtils.imageUrl(person.get("Id"), "Primary", 0, 400, 400)
|
|
|
|
cursor.execute("SELECT idActor as actorid FROM actors WHERE strActor = ?",(Name,))
|
|
|
|
result = cursor.fetchone()
|
|
|
|
if result != None:
|
|
|
|
actorid = result[0]
|
|
|
|
if actorid == None:
|
|
|
|
cursor.execute("select coalesce(max(idActor),0) as actorid from actors")
|
|
|
|
actorid = cursor.fetchone()[0]
|
|
|
|
actorid = actorid + 1
|
|
|
|
peoplesql="insert into actors(idActor, strActor, strThumb) values(?, ?, ?)"
|
|
|
|
cursor.execute(peoplesql, (actorid,Name,Thumb))
|
|
|
|
|
|
|
|
if mediatype == "movie":
|
|
|
|
peoplesql="INSERT OR REPLACE into actorlinkmovie(idActor, idMovie, strRole, iOrder) values(?, ?, ?, ?)"
|
|
|
|
if mediatype == "tvshow":
|
|
|
|
peoplesql="INSERT OR REPLACE into actorlinktvshow(idActor, idShow, strRole, iOrder) values(?, ?, ?, ?)"
|
|
|
|
if mediatype == "episode":
|
|
|
|
peoplesql="INSERT OR REPLACE into actorlinkepisode(idActor, idEpisode, strRole, iOrder) values(?, ?, ?, ?)"
|
|
|
|
cursor.execute(peoplesql, (actorid,id,Role,None))
|
|
|
|
|
|
|
|
connection.commit()
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
def getKodiMovie(self, id):
|
2015-03-14 17:23:45 +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", "file"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true } }, "id": "libMovies"}')
|
2015-03-13 21:24:59 +00:00
|
|
|
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-14 22:43:36 +00:00
|
|
|
|
|
|
|
def getKodiTVShow(self, id):
|
|
|
|
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetTVShows", "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", "file"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true } }, "id": "libMovies"}')
|
|
|
|
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
|