From eae4fea76a5123a345cc66d6952b98e70ba1545b Mon Sep 17 00:00:00 2001
From: marcelveldt <m.vanderveldt@outlook.com>
Date: Mon, 18 Jan 2016 12:05:05 +0100
Subject: [PATCH] more unicode fixes: os.path functions use unicode depending
 on the OS

---
 resources/lib/artwork.py       |  5 ++++-
 resources/lib/clientinfo.py    |  5 ++++-
 resources/lib/downloadutils.py |  2 +-
 resources/lib/entrypoint.py    |  5 ++++-
 resources/lib/itemtypes.py     |  5 +++--
 resources/lib/kodimonitor.py   |  2 +-
 resources/lib/musicutils.py    | 19 +++++++++++++------
 7 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/resources/lib/artwork.py b/resources/lib/artwork.py
index ef562af0..6d42a45f 100644
--- a/resources/lib/artwork.py
+++ b/resources/lib/artwork.py
@@ -185,7 +185,10 @@ class Artwork():
                 for dir in allDirs:
                     allDirs, allFiles = xbmcvfs.listdir(path+dir)
                     for file in allFiles:
-                        xbmcvfs.delete(os.path.join(path+dir.decode('utf-8'),file.decode('utf-8')))
+                        if os.path.supports_unicode_filenames:
+                            xbmcvfs.delete(os.path.join(path+dir.decode('utf-8'),file.decode('utf-8')))
+                        else:
+                            xbmcvfs.delete(os.path.join(path.encode('utf-8')+dir,file))
             
             # remove all existing data from texture DB
             textureconnection = utils.kodiSQL('texture')
diff --git a/resources/lib/clientinfo.py b/resources/lib/clientinfo.py
index b86c8a7b..44ed98f2 100644
--- a/resources/lib/clientinfo.py
+++ b/resources/lib/clientinfo.py
@@ -76,7 +76,10 @@ class ClientInfo():
             return clientId
 
         addon_path = self.addon.getAddonInfo('path').decode('utf-8')
-        GUID_file = xbmc.translatePath(os.path.join(addon_path, "machine_guid")).decode('utf-8')
+        if os.path.supports_unicode_filenames:
+            GUID_file = xbmc.translatePath(os.path.join(addon_path, "machine_guid")).decode('utf-8')
+        else:
+            GUID_file = xbmc.translatePath(os.path.join(addon_path.encode("utf-8"), "machine_guid")).decode('utf-8')
 
         try:
             GUID = xbmcvfs.File(GUID_file)
diff --git a/resources/lib/downloadutils.py b/resources/lib/downloadutils.py
index 5c47144d..1c244e74 100644
--- a/resources/lib/downloadutils.py
+++ b/resources/lib/downloadutils.py
@@ -326,7 +326,7 @@ class DownloadUtils():
             elif r.status_code == requests.codes.ok:
                
                 try: 
-                    # UTF-8 - JSON object
+                    # UNICODE - JSON object
                     r = r.json()
                     self.logMsg("====== 200 Success ======", 2)
                     self.logMsg("Response: %s" % r, 2)
diff --git a/resources/lib/entrypoint.py b/resources/lib/entrypoint.py
index 6c323f0f..e901df18 100644
--- a/resources/lib/entrypoint.py
+++ b/resources/lib/entrypoint.py
@@ -990,7 +990,10 @@ def getExtraFanArt():
                     for backdrop in backdrops:
                         # Same ordering as in artwork
                         tag = tags[count]
-                        fanartFile = os.path.join(fanartDir, "fanart%s.jpg" % tag)
+                        if os.path.supports_unicode_filenames:
+                            fanartFile = os.path.join(fanartDir, "fanart%s.jpg" % tag)
+                        else:
+                            fanartFile = os.path.join(fanartDir.encode("utf-8"), "fanart%s.jpg" % tag.encode("utf-8"))
                         li = xbmcgui.ListItem(tag, path=fanartFile)
                         xbmcplugin.addDirectoryItem(
                                             handle=int(sys.argv[1]),
diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py
index 92a94a25..7559d28c 100644
--- a/resources/lib/itemtypes.py
+++ b/resources/lib/itemtypes.py
@@ -2208,8 +2208,10 @@ class Music(Items):
         filename = API.getFilePath()
         rating = 0
         emby_rating = int(round(emby_rating, 0))
-        file_rating, comment = musicutils.getSongTags(filename)
         
+        #get file rating and comment tag from file itself.
+        #TODO: should we make this an optional setting if it impacts sync speed too much ?
+        file_rating, comment = musicutils.getSongTags(filename)
 
         emby_dbitem = self.emby_db.getItem_byId(embyid)
         try:
@@ -2228,7 +2230,6 @@ class Music(Items):
         elif file_rating is None and not currentvalue:
             return (emby_rating, comment)
         
-        file_rating = int(round(file_rating,0))
         self.logMsg("getSongRatingAndComment --> embyid: %s - emby_rating: %s - file_rating: %s - current rating in kodidb: %s" %(embyid, emby_rating, file_rating, currentvalue))
         
         updateFileRating = False
diff --git a/resources/lib/kodimonitor.py b/resources/lib/kodimonitor.py
index 80e664f6..e4f25a6f 100644
--- a/resources/lib/kodimonitor.py
+++ b/resources/lib/kodimonitor.py
@@ -72,7 +72,7 @@ class KodiMonitor(xbmc.Monitor):
             self.logMsg("Method: %s Data: %s" % (method, data), 1)
             
         if data:
-            data = json.loads(data)
+            data = json.loads(data,'utf-8')
 
 
         if method == "Player.OnPlay":
diff --git a/resources/lib/musicutils.py b/resources/lib/musicutils.py
index f807fe57..c77f75ee 100644
--- a/resources/lib/musicutils.py
+++ b/resources/lib/musicutils.py
@@ -19,15 +19,21 @@ def logMsg(msg, lvl=1):
 def getRealFileName(filename):
     #get the filename path accessible by python if possible...
     isTemp = False
-    
+
     if not xbmcvfs.exists(filename):
         logMsg( "File does not exist! %s" %(filename), 0)
         return (False, "")
     
+    #if we use os.path method on older python versions (sunch as some android builds), we need to pass arguments as string
+    if os.path.supports_unicode_filenames:
+        checkfile = filename
+    else:
+        checkfile = file.encode("utf-8")
+    
     # determine if our python module is able to access the file directly...
-    if os.path.exists(filename):
+    if os.path.exists(checkfile):
         filename = filename
-    elif os.path.exists(filename.replace("smb://","\\\\").replace("/","\\")):
+    elif os.path.exists(checkfile.replace("smb://","\\\\").replace("/","\\")):
         filename = filename.replace("smb://","\\\\").replace("/","\\")
     else:
         #file can not be accessed by python directly, we copy it for processing...
@@ -57,7 +63,7 @@ def getEmbyRatingFromKodiRating(rating):
     
 def getSongTags(file):
     # Get the actual ID3 tags for music songs as the server is lacking that info
-    rating = None
+    rating = 0
     comment = ""
     
     isTemp,filename = getRealFileName(file)
@@ -84,12 +90,13 @@ def getSongTags(file):
         else:
             logMsg( "Not supported fileformat or unable to access file: %s" %(filename))
         
-        if rating:
-            rating = int(round(rating,0))
+        #the rating must be a round value
+        rating = int(round(rating,0))
     
     except Exception as e:
         #file in use ?
         logMsg("Exception in getSongTags %s" %e,0)
+        return (None,"")
         
     #remove tempfile if needed....
     if isTemp: xbmcvfs.delete(filename)