Help with a path problem
#1
I wrote a simple python script. The script is intended to take a screenshot and save it to the appropriate "extrafanart" folder (determined by the path of the now-playing video item). The script works (running it in windows/win32, anyway), but when I run it on a video in a path with unicode, my Kodi log shows an error even though the screen shot is created. So far this is pretty much a "go path" script I realize. So any ideas of how to improve this script would be greatly appreciated. The script is simply:

Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#

import xbmc
import os

current_folder = xbmc.getInfoLabel('Player.Folderpath')
current_folder = unicode(current_folder,'utf-8').replace('\\', '/')
current_content = xbmc.getCondVisibility('VideoPlayer.Content(episodes)')
if current_content :
    shot_folder = current_folder + '../extrafanart/'
else: shot_folder = current_folder + 'extrafanart/'
if not os.path.exists(shot_folder):
    os.mkdir(shot_folder)
shot_folder =  '\"' + shot_folder.encode('utf-8') + '\"'
xbmc.log(shot_folder)
response = xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue","params": {"setting": "debug.screenshotpath", "value":  %s}, "id":1}' %shot_folder)
#  if (response['result']) xbmc.executebuiltin(TakeScreenshot)
xbmc.executebuiltin('TakeScreenshot')

I have a log here:

http://xbmclogs.com/pujgq2ahu

Of interest is at line 1325 where I set the screenshot path to a windows file path with unicode and get the error at 1339 and then again at line 1832 where the path is just ascii. But again, the screenshot is properly saved in both instances.

TIA

scott s.
.
Reply
#2
I would use os.path.join instead of adding to test strings together when dealing with paths. Otherwise the string might not won't work on other platforms. I don't know that that will fix your unicode problem, but it's good practice. I constantly want to tear my eyes out when having to figure out the encoding and decoding dark magic required to avoid unicode errors. I ended up finding a chunk of code either here or on Stackoverflow that seems to mostly convert things the right way. I stuck it into a common function so I can call it like this when dealing with file/folder paths:
Code:
from resources.common.fix_utf8 import smartUTF8

smartUTF8(NAME).decode('utf-8')
Please don't ask my how it works. It just does, and I've decided I can live with that. Wink

Here's the code from the fix_utf8 library:
Code:
#v.0.1.1

import unicodedata

def smartUnicode(s):
    if not s:
        return ''
    try:
        if not isinstance(s, basestring):
            if hasattr(s, '__unicode__'):
                s = unicode(s)
            else:
                s = unicode(str(s), 'UTF-8')
        elif not isinstance(s, unicode):
            s = unicode(s, 'UTF-8')
    except:
        if not isinstance(s, basestring):
            if hasattr(s, '__unicode__'):
                s = unicode(s)
            else:
                s = unicode(str(s), 'ISO-8859-1')
        elif not isinstance(s, unicode):
            s = unicode(s, 'ISO-8859-1')
    return s

def smartUTF8(s):
    return smartUnicode(s).encode('utf-8')
Reply
#3
Thanks. I know from following a couple of your add-ons you've had to do a lot to get unicode working well.

scott s.
.
Reply

Logout Mark Read Team Forum Stats Members Help
Help with a path problem0