Kodi Community Forum

Full Version: Need a bit of help regarding unicode please.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So, I've been messing with the CULRC lyrics script to try and get it to support streaming radio better. I've got to the point where it correctly identifies the stream and grabs the artist/title data via json. The problem I have is that there is a routine that gets called later to de-accent the strings, and this fails with a "TypeError: decoding Unicode is not supported". I don't want to mess with the rest of the script, just the bit that parses the stream info.

The code I'm using to get the stream info is as follows

PHP Code:
if not xbmc.getInfoLabel("MusicPlayer.TimeRemaining") and song.title == "":
               
my_json_query xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "Player.GetItem", "params": { "properties": [ "title"], "playerid": '"0"' }, "id": "AudioGetItem"}')
            
my_json_response=simplejson.loads(my_json_query)
            if 
my_json_response.has_key('result') and (my_json_response['result'] != None) and (my_json_response['result'].has_key('item')):
                
song.title my_json_response['result']['item']['title']
            
sep song.title.find("-")
            if 
sep 1:
               
song.artist song.title[:sep 1].strip()
                
song.title song.title[sep 1:].strip()
                
log ('Artist : %s' %(song.artist))
                
log ('Title : %s' %(song.title))
                
song.title re.sub(r'\([^\)]*\)$'''song.title)
            return 
song 

Although this works, the script fails later with the aforementioned error at this routine
PHP Code:
def deAccent(str):
    return 
unicodedata.normalize('NFKD'unicode(str'utf-8')) 

Relevant error from my debug log is
Code:
ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.TypeError'>
                                            Error Contents: decoding Unicode is not supported
                                            Traceback (most recent call last):
                                              File "/home/xbmc/.xbmc/addons/script.cu.lrclyrics/default.py", line 40, in <module>
                                                culrc_run('service')
                                              File "/home/xbmc/.xbmc/addons/script.cu.lrclyrics/default.py", line 27, in culrc_run
                                                gui.MAIN(mode=mode)
                                              File "/home/xbmc/.xbmc/addons/script.cu.lrclyrics/resources/lib/gui.py", line 25, in __init__
                                                self.main_loop()
                                              File "/home/xbmc/.xbmc/addons/script.cu.lrclyrics/resources/lib/gui.py", line 52, in main_loop
                                                self.myPlayerChanged()
                                              File "/home/xbmc/.xbmc/addons/script.cu.lrclyrics/resources/lib/gui.py", line 210, in myPlayerChanged
                                                if ( song and ( self.current_lyrics.song != song ) ):
                                              File "/home/xbmc/.xbmc/addons/script.cu.lrclyrics/resources/lib/utilities.py", line 99, in __cmp__
                                                return cmp(deAccent(self.artist), deAccent(song.artist))
                                              File "/home/xbmc/.xbmc/addons/script.cu.lrclyrics/resources/lib/utilities.py", line 34, in deAccent
                                                return unicodedata.normalize('NFKD', unicode(str, 'utf-8'))
                                            TypeError: decoding Unicode is not supported
                                            -->End of Python script error report<--

Some help with this would be really appreciated.

Thanks.
Figured it !!

Needed to do
PHP Code:
song.title song.title.encode('utf-8'

Just to add, the detection of a live stream currently relies on xbmc.getInfoLabel("MusicPlayer.TimeRemaining") returning zero. However, this is not always the case, as buffering causes it to be between 0~2 seconds. I can change the test for that easily though.

Hardest part is detecting change of song !!
You get that error because you pass a unicode string to unicode(). What you "need" to do is lose all intermediate encode/decode steps and use unicode everywhere. You can thank me later..
Ummm, yeah !! Thanks for that, I was aware that using unicode everywhere would negate the issue. Sadly, that would mean re-writing the addon from scratch, which I don't want to do. I just want it to work correctly with a live radio stream. Clearly, the json request returns a unicode string, which then has to be encoded to utf-8 as this is what the rest of the addon expects.

Just as a thought, a code example speaks volumes (at least to me !).

Cheers anyway Smile