move get setting and creation of Download Utils to where they are used

to allow them to pick up the values at the time they are needed
This commit is contained in:
shaun 2015-03-15 09:33:16 +11:00
parent 4f6e7c2233
commit f7aa1d4310
2 changed files with 54 additions and 21 deletions

View file

@ -22,25 +22,18 @@ import xml.etree.cElementTree as ET
from API import API from API import API
import Utils as utils import Utils as utils
from DownloadUtils import DownloadUtils from DownloadUtils import DownloadUtils
downloadUtils = DownloadUtils()
addon = xbmcaddon.Addon(id='plugin.video.mb3sync') addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
addondir = xbmc.translatePath( addon.getAddonInfo('profile') ) addondir = xbmc.translatePath(addon.getAddonInfo('profile'))
dataPath = os.path.join(addondir,"library") dataPath = os.path.join(addondir,"library")
movieLibrary = os.path.join(dataPath,'movies') movieLibrary = os.path.join(dataPath,'movies')
tvLibrary = os.path.join(dataPath,'tvshows') tvLibrary = os.path.join(dataPath,'tvshows')
WINDOW = xbmcgui.Window( 10000 )
port = addon.getSetting('port')
host = addon.getSetting('ipaddress')
server = host + ":" + port
userid = downloadUtils.getUserId()
class LibrarySync(): class LibrarySync():
def syncDatabase(self): def syncDatabase(self):
WINDOW = xbmcgui.Window( 10000 )
WINDOW.setProperty("librarysync", "busy") WINDOW.setProperty("librarysync", "busy")
updateNeeded = False updateNeeded = False
@ -48,7 +41,7 @@ class LibrarySync():
movieData = self.getMovies(True) movieData = self.getMovies(True)
if(movieData == None): if(movieData == None):
return return False
for item in movieData: for item in movieData:
if not item.get('IsFolder'): if not item.get('IsFolder'):
@ -76,15 +69,18 @@ class LibrarySync():
xbmc.executebuiltin("UpdateLibrary(video)") xbmc.executebuiltin("UpdateLibrary(video)")
WINDOW.clearProperty("librarysync") WINDOW.clearProperty("librarysync")
return True
def updatePlayCounts(self): def updatePlayCounts(self):
#update all playcounts from MB3 to Kodi library #update all playcounts from MB3 to Kodi library
WINDOW = xbmcgui.Window( 10000 )
WINDOW.setProperty("librarysync", "busy") WINDOW.setProperty("librarysync", "busy")
movieData = self.getMovies(True) movieData = self.getMovies(True)
if(movieData == None): if(movieData == None):
return return False
for item in movieData: for item in movieData:
if not item.get('IsFolder'): if not item.get('IsFolder'):
@ -102,9 +98,20 @@ class LibrarySync():
self.setKodiResumePoint(kodiItem['movieid'],resume,total) self.setKodiResumePoint(kodiItem['movieid'],resume,total)
WINDOW.clearProperty("librarysync") WINDOW.clearProperty("librarysync")
return True
def getMovies(self, fullinfo = False): def getMovies(self, fullinfo = False):
result = None 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: 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=Movie&format=json&ImageTypeLimit=1' 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'
else: else:
@ -120,6 +127,14 @@ class LibrarySync():
def updatePlayCountFromKodi(self, id, playcount=0): def updatePlayCountFromKodi(self, id, playcount=0):
#when user marks item watched from kodi interface update this to MB3 #when user marks item watched from kodi interface update this to MB3
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
port = addon.getSetting('port')
host = addon.getSetting('ipaddress')
server = host + ":" + port
downloadUtils = DownloadUtils()
userid = downloadUtils.getUserId()
json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovieDetails", "params": { "movieid": ' + str(id) + ', "properties" : ["playcount", "file"] }, "id": "1"}') json_response = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovieDetails", "params": { "movieid": ' + str(id) + ', "properties" : ["playcount", "file"] }, "id": "1"}')
if json_response != None: if json_response != None:
jsonobject = json.loads(json_response.decode('utf-8','replace')) jsonobject = json.loads(json_response.decode('utf-8','replace'))
@ -131,7 +146,7 @@ class LibrarySync():
filename = moviedetails.get("file").rpartition('\\')[2] filename = moviedetails.get("file").rpartition('\\')[2]
mb3Id = filename.replace(".strm","") mb3Id = filename.replace(".strm","")
watchedurl = 'http://' + host + ':' + port + '/mediabrowser/Users/' + userid + '/PlayedItems/' + mb3Id watchedurl = 'http://' + server + '/mediabrowser/Users/' + userid + '/PlayedItems/' + mb3Id
print "watchedurl -->" + watchedurl print "watchedurl -->" + watchedurl
if playcount != 0: if playcount != 0:
downloadUtils.downloadUrl(watchedurl, postBody="", type="POST") downloadUtils.downloadUrl(watchedurl, postBody="", type="POST")
@ -140,6 +155,12 @@ class LibrarySync():
def updateMovieToKodiLibrary( self, MBitem, KodiItem ): def updateMovieToKodiLibrary( 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) timeInfo = API().getTimeInfo(MBitem)
userData=API().getUserData(MBitem) userData=API().getUserData(MBitem)
people = API().getPeople(MBitem) people = API().getPeople(MBitem)
@ -383,6 +404,8 @@ class LibrarySync():
#use sqlite to set add the actors while json api doesn't support this yet #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 #todo --> submit PR to kodi team to get this added to the jsonrpc api
downloadUtils = DownloadUtils()
id = KodiItem["movieid"] id = KodiItem["movieid"]
dbPath = xbmc.translatePath("special://userdata/Database/MyVideos90.db") dbPath = xbmc.translatePath("special://userdata/Database/MyVideos90.db")
connection = sqlite3.connect(dbPath) connection = sqlite3.connect(dbPath)

View file

@ -84,18 +84,28 @@ class Service():
#full sync #full sync
if(cur_seconds_fullsync >= interval_FullSync): if(cur_seconds_fullsync >= interval_FullSync):
librarySync.syncDatabase() xbmc.log("Doing_Db_Sync: syncDatabase")
cur_seconds_fullsync = 0 worked = librarySync.syncDatabase()
if(worked):
cur_seconds_fullsync = 0
else:
cur_seconds_fullsync = interval_FullSync - 10
else: else:
cur_seconds_fullsync += 1 cur_seconds_fullsync += 1
#incremental sync #incremental sync
if(cur_seconds_incrsync >= interval_IncrementalSync): if(cur_seconds_incrsync >= interval_IncrementalSync):
librarySync.updatePlayCounts() xbmc.log("Doing_Db_Sync: updatePlayCounts")
cur_seconds_incrsync = 0 worked = librarySync.updatePlayCounts()
if(worked):
cur_seconds_incrsync = 0
else:
cur_seconds_incrsync = interval_IncrementalSync - 10
else: else:
cur_seconds_incrsync += 1 cur_seconds_incrsync += 1
else:
xbmc.log("Not authenticated yet")
utils.logMsg("MB3 Sync Service" "stopping Service",0) utils.logMsg("MB3 Sync Service" "stopping Service",0)