From 4458fa6fda2dd43b1d7aea34718e2e40d57b7183 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Mon, 30 Mar 2015 00:43:53 +0200 Subject: [PATCH] FINAL fix for the doubled episodes. Yeah for real now :-) --- resources/lib/CreateFiles.py | 15 ++++++--------- resources/lib/LibrarySync.py | 1 + resources/lib/Utils.py | 7 ++++++- resources/lib/WriteKodiDB.py | 35 ++++++++++++++++++++++++----------- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/resources/lib/CreateFiles.py b/resources/lib/CreateFiles.py index 2faef192..b7168902 100644 --- a/resources/lib/CreateFiles.py +++ b/resources/lib/CreateFiles.py @@ -48,13 +48,13 @@ class CreateFiles(): if item_type == "Episode": itemPath = os.path.join(tvLibrary,item["SeriesId"]) if str(item.get("IndexNumber")) != None: - filenamestr = self.CleanName(utils.convertEncoding(item.get("SeriesName"))) + " S" + str(item.get("ParentIndexNumber")) + "E" + str(item.get("IndexNumber")) + " (" + item["Id"] + ").strm" + filenamestr = utils.CleanName(utils.convertEncoding(item.get("SeriesName"))) + " S" + str(item.get("ParentIndexNumber")) + "E" + str(item.get("IndexNumber")) + " (" + item["Id"] + ").strm" else: - filenamestr = self.CleanName(utils.convertEncoding(item.get("SeriesName"))) + " S0E0 " + self.CleanName(utils.convertEncoding(item.get("Name"))) + " (" + item["Id"] + ").strm" + filenamestr = utils.CleanName(utils.convertEncoding(item.get("SeriesName"))) + " S0E0 " + utils.CleanName(utils.convertEncoding(item.get("Name"))) + " (" + item["Id"] + ").strm" strmFile = os.path.join(itemPath,filenamestr) changes = False - if not xbmcvfs.exists(strmFile) and not xbmcvfs.exists(strmFile.replace(".strm",".emby")): + if not xbmcvfs.exists(strmFile): changes = True xbmcvfs.mkdir(itemPath) text_file = open(strmFile, "w") @@ -108,9 +108,9 @@ class CreateFiles(): if item_type == "Episode": itemPath = os.path.join(tvLibrary,item["SeriesId"]) if str(item.get("ParentIndexNumber")) != None: - filenamestr = self.CleanName(utils.convertEncoding(item.get("SeriesName"))) + " S" + str(item.get("ParentIndexNumber")) + "E" + str(item.get("IndexNumber")) + " (" + item["Id"] + ").nfo" + filenamestr = utils.CleanName(utils.convertEncoding(item.get("SeriesName"))) + " S" + str(item.get("ParentIndexNumber")) + "E" + str(item.get("IndexNumber")) + " (" + item["Id"] + ").nfo" else: - filenamestr = self.CleanName(utils.convertEncoding(item.get("SeriesName"))) + " S0E0 " + self.CleanName(utils.convertEncoding(item["Name"])) + " (" + item["Id"] + ").nfo" + filenamestr = utils.CleanName(utils.convertEncoding(item.get("SeriesName"))) + " S0E0 " + utils.CleanName(utils.convertEncoding(item["Name"])) + " (" + item["Id"] + ").nfo" nfoFile = os.path.join(itemPath,filenamestr) rootelement = "episodedetails" @@ -333,8 +333,5 @@ class CreateFiles(): fanartFile = os.path.join(fanartDir,"fanart" + str(index) + ".jpg") xbmcvfs.copy(backgroundUrl,fanartFile) - def CleanName(self, filename): - validFilenameChars = "-_.() %s%s" % (string.ascii_letters, string.digits) - cleanedFilename = unicodedata.normalize('NFKD', filename).encode('ASCII', 'ignore') - return ''.join(c for c in cleanedFilename if c in validFilenameChars) + diff --git a/resources/lib/LibrarySync.py b/resources/lib/LibrarySync.py index 32b55484..b18702d3 100644 --- a/resources/lib/LibrarySync.py +++ b/resources/lib/LibrarySync.py @@ -541,6 +541,7 @@ class LibrarySync(): episodeData = ReadEmbyDB().getEpisodes(tvshow,True) kodiEpisodes = None + allKodiTVShows = ReadKodiDB().getKodiTvShows(False) if allKodiTVShows != None: kodishow = allKodiTVShows.get(tvshow,None) if kodishow != None: diff --git a/resources/lib/Utils.py b/resources/lib/Utils.py index bbc6a26c..bb599574 100644 --- a/resources/lib/Utils.py +++ b/resources/lib/Utils.py @@ -13,6 +13,8 @@ import pstats import time import inspect import sqlite3 +import string +import unicodedata from xml.etree.ElementTree import Element, SubElement, Comment, tostring from xml.etree import ElementTree from xml.dom import minidom @@ -243,6 +245,9 @@ def stopProfiling(pr, profileName): f.write(str(ncalls) + "\t" + "{0}".format(total_time) + "\t" + "{0}".format(cumulative_time) + "\t" + func_name + "\t" + filename + "\r\n") f.close() - +def CleanName(filename): + validFilenameChars = "-_.() %s%s" % (string.ascii_letters, string.digits) + cleanedFilename = unicodedata.normalize('NFKD', filename).encode('ASCII', 'ignore') + return ''.join(c for c in cleanedFilename if c in validFilenameChars) diff --git a/resources/lib/WriteKodiDB.py b/resources/lib/WriteKodiDB.py index 69a8ee07..8d79759f 100644 --- a/resources/lib/WriteKodiDB.py +++ b/resources/lib/WriteKodiDB.py @@ -407,9 +407,15 @@ class WriteKodiDB(): #update/check all artwork changes |= self.updateArtWork(KodiItem,MBitem) - #set Filename + #set Filename (also submit strmfilepath to rename that if path has changed to original location) playurl = PlayUtils().getPlayUrl(server, MBitem["Id"], MBitem) - self.setKodiFilename(KodiItem["episodeid"], KodiItem["file"], playurl, "episode") + itemPath = os.path.join(tvLibrary,MBitem["SeriesId"]) + if str(MBitem.get("IndexNumber")) != None: + filenamestr = utils.CleanName(utils.convertEncoding(MBitem.get("SeriesName"))) + " S" + str(MBitem.get("ParentIndexNumber")) + "E" + str(MBitem.get("IndexNumber")) + " (" + MBitem["Id"] + ").strm" + else: + filenamestr = utils.CleanName(utils.convertEncoding(MBitem.get("SeriesName"))) + " S0E0 " + utils.CleanName(utils.convertEncoding(MBitem.get("Name"))) + " (" + MBitem["Id"] + ").strm" + strmFile = os.path.join(itemPath,filenamestr) + self.setKodiFilename(KodiItem["episodeid"], KodiItem["file"], playurl, "episode", strmFile) #update common properties duration = (int(timeInfo.get('Duration'))*60) @@ -775,8 +781,7 @@ class WriteKodiDB(): xbmc.sleep(100) utils.logMsg("episode deleted succesfully!",episodeid) else: - utils.logMsg("episode not found in kodi DB",episodeid) - + utils.logMsg("episode not found in kodi DB",episodeid) def addTVShowToKodiLibrary( self, item ): itemPath = os.path.join(tvLibrary,item["Id"]) @@ -866,8 +871,7 @@ class WriteKodiDB(): connection.commit() cursor.close() - - def setKodiFilename(self, id, oldFileName, newFileName, fileType): + def setKodiFilename(self, id, oldFileName, newFileName, fileType, strmFile=None): #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 #todo --> extend support for musicvideos @@ -887,6 +891,10 @@ class WriteKodiDB(): cursor = connection.cursor( ) utils.logMsg("MB3 Sync","setting filename in kodi db..." + fileType + ": " + str(id)) + try: + print oldFileName + except: + pass if fileType == "tvshow": #for tvshows we only store the path in DB @@ -942,11 +950,16 @@ class WriteKodiDB(): finally: cursor.close() - #rename the old strmfile to prevent Kodi from scanning it again - if oldFileName.endswith(".strm"): - if xbmcvfs.exists(oldFileName): - oldFileName_renamed = oldFileName.replace(".strm",".emby") - xbmcvfs.rename(oldFileName,oldFileName_renamed) + #rename the old strmfile to prevent Kodi from scanning it again + if strmFile != None: + filename = strmFile + else: + filename = oldFileName + + if filename.endswith(".strm"): + if xbmcvfs.exists(filename): + oldFileName_renamed = filename.replace(".strm",".emby") + xbmcvfs.rename(filename,oldFileName_renamed) def AddActorsToMedia(self, KodiItem, people, mediatype):