예제 #1
0
def normalize_string(string, charset=None, replacing=False):
    """
    Decode and Convert to Unicode any string
    :param charset: encoding
    :type charset: str
    :param string: string to convert
    :type string: str or unicode
    :param replacing: Whether is ' is replaced
    :type replacing: bool
    :return: converted unicode
    :rtype: unicode
    """
    if not isinstance(string, unicode):
        try:
            if re.search(u'=[0-9a-fA-F]{2}', string):
                string = py2_decode(string, 'Quoted-printable')

            string = json.loads(u'%s' % string, encoding=charset)

        except ValueError:
            try:
                string = unicode(eval(string), 'raw_unicode_escape')

            except (SyntaxError, NameError):
                string = py2_decode(string, 'latin-1')
                pass

            except TypeError:
                string = unicode(string, errors='ignore')
                pass

        except LookupError:
            return u''

        except TypeError:
            string = unicode(string, errors='ignore')
            pass

    try:
        string = remove_control_chars(string)
        string = fix_bad_unicode(string)
        string = unquote(string)
        string = string.replace(u'<![CDATA[', u'').replace(u']]', u'')

        if PY3:
            string = html.unescape(string)
        else:
            string = HTMLParser().unescape(string)

        if replacing:
            string = string.replace(u"'", '')

        string = string.lower()
    except:
        pass

    return string
예제 #2
0
 def urlencode(query):
     # Dict[str, str] -> str
     return py2_decode(
         _urlencode({
             py2_encode(param): py2_encode(arg)
             for param, arg in query.items()
         }))
예제 #3
0
def user_playlist_import():
    path = settings.import_export_path
    if len(path) == 0 or not session.is_logged_in:
        return
    files = xbmcvfs.listdir(path)[1]
    files = [
        py2_decode(name) for name in files
        if py2_decode(name).startswith('playlist_')
    ]
    selected = xbmcgui.Dialog().select(path, files)
    if selected < 0:
        return
    name = os.path.join(path, files[selected])
    try:
        session.user.import_playlists(name)
    except Exception as e:
        log.logException(e)
예제 #4
0
def reinterpret_latin1_as_windows1252(wrong_text):
    """
        Maybe this was always meant to be in a single-byte encoding, and it
        makes the most sense in Windows-1252.
    :param wrong_text: text with problems
    :type: str or unicode
    :return: corrected text
    :rtype: str or unicode
    """
    return py2_decode(py2_encode(wrong_text, 'latin-1'), 'WINDOWS_1252', 'replace')
예제 #5
0
def favorites_import(what):
    path = settings.import_export_path
    if len(path) == 0 or not session.is_logged_in:
        return
    files = xbmcvfs.listdir(path)[1]
    files = [
        py2_decode(name) for name in files
        if py2_decode(name).startswith('favo_%s' % what)
    ]
    selected = xbmcgui.Dialog().select(path, files)
    if selected < 0:
        return
    name = os.path.join(path, files[selected])
    ok = False
    if what == 'playlists':
        ok = session.user.favorites.import_ids(
            what=_T('Playlists'),
            filename=name,
            action=session.user.favorites.add_playlist)
    elif what == 'artists':
        ok = session.user.favorites.import_ids(
            what=_T('Artists'),
            filename=name,
            action=session.user.favorites.add_artist)
    elif what == 'albums':
        ok = session.user.favorites.import_ids(
            what=_T('Albums'),
            filename=name,
            action=session.user.favorites.add_album)
    elif what == 'tracks':
        ok = session.user.favorites.import_ids(
            what=_T('Tracks'),
            filename=name,
            action=session.user.favorites.add_track)
    elif what == 'videos':
        ok = session.user.favorites.import_ids(
            what=_T('Videos'),
            filename=name,
            action=session.user.favorites.add_video)
    return ok
예제 #6
0
def search_field_edit():
    try:
        params = eval(settings.getSetting('search_parameters_field'))
        params.update({'search': ''})
    except:
        log.error('Search Parameters not set !')
        return
    s_text = params.get('text')
    keyboard = xbmc.Keyboard(s_text, _S(Msg.i30401))
    keyboard.doModal()
    if keyboard.isConfirmed():
        value = py2_decode(keyboard.getText())
        params.update({'text': value})
        settings.setSetting('search_parameters_field', repr(params))
        xbmc.executebuiltin('Container.Refresh()')
예제 #7
0
def homepage_item(item_type, path):
    path = py2_decode(unquote_plus(path)).strip()
    rettype = HOMEPAGE_ITEM_TYPES.get(item_type, 'NONE')
    if rettype != 'NONE':
        params = {
            'locale': session._config.locale,
            'deviceType': 'BROWSER',
            'offset': 0,
            'limit': 50
        }
        items = session._map_request(url=path,
                                     method='GET',
                                     params=params,
                                     ret=rettype)
        session.add_list_items(items,
                               content=CONTENT_FOR_TYPE.get(rettype, 'files'),
                               end=True)
예제 #8
0
def reinterpret_windows1252_as_utf8(wrong_text):
    """
        Maybe this was always meant to be in a single-byte encoding, and it
        makes the most sense in utf-8.
    :param wrong_text: text with problems
    :type: str or unicode
    :return: corrected text
    :rtype: str or unicode
    """
    altered_bytes = []
    for char in wrong_text:
        if ord(char) in WINDOWS_1252_GREMLINS:
            altered_bytes.append(py2_encode(char, 'WINDOWS_1252'))

        else:
            altered_bytes.append(py2_encode(char, 'latin-1', 'replace'))

    return py2_decode(''.join(altered_bytes), 'utf-8', 'replace')
예제 #9
0
def reinterpret_latin1_as_utf8(wrong_text):
    new_bytes = py2_encode(wrong_text, 'latin-1', 'replace')
    return py2_decode(new_bytes, 'utf-8', 'replace')
예제 #10
0
 def strftime(format, t=None):
     return py2_decode(_strftime(py2_encode(format)))
예제 #11
0
 def parse_qs(qs):
     # str -> Dict[str, List[str]]
     return {py2_decode(param): [py2_decode(a) for a in args]
             for param, args in _parse_qs(py2_encode(qs)).items()}