diff --git a/MetaTube.bundle/Contents/Code/api_client.py b/MetaTube.bundle/Contents/Code/api_client.py index 7219926..f08b340 100644 --- a/MetaTube.bundle/Contents/Code/api_client.py +++ b/MetaTube.bundle/Contents/Code/api_client.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- +import utils from constants import * -from utils import parse_date try: # Python 2 import httplib as http_status @@ -48,8 +48,8 @@ def __init__(self, **data): self.blood_type = data['blood_type'] # type: str self.measurements = data['measurements'] # type: str self.nationality = data['nationality'] # type: str - self.birthday = parse_date(data['birthday']) # type: datetime - self.debut_date = parse_date(data['debut_date']) # type: datetime + self.birthday = utils.parse_date(data['birthday']) # type: datetime + self.debut_date = utils.parse_date(data['debut_date']) # type: datetime class MovieSearchResult(BaseInfoObject): @@ -61,7 +61,7 @@ def __init__(self, **data): self.thumb_url = data['thumb_url'] # type: str self.score = float(data['score']) # type: float self.actors = data.get('actors', []) # type: list[str] - self.release_date = parse_date(data['release_date']) # type: datetime + self.release_date = utils.parse_date(data['release_date']) # type: datetime class MovieInfoObject(MovieSearchResult): @@ -87,7 +87,7 @@ def __init__(self, **data): self.author = data['author'] # type: str self.comment = data['comment'] # type: str self.score = float(data['score']) # type: float - self.date = parse_date(data['date']) # type: datetime + self.date = utils.parse_date(data['date']) # type: datetime class TranslationInfoObject(object): @@ -137,7 +137,7 @@ def get_json(self, url, require_auth=False): headers['Authorization'] = 'Bearer {token}'.format(token=Prefs[KEY_API_TOKEN]) with self.session.get(url=url, headers=headers) as r: - info = r.json() + info = utils.safe_unicode(r.json()) data = info.get('data') error = info.get('error') diff --git a/MetaTube.bundle/Contents/Code/utils.py b/MetaTube.bundle/Contents/Code/utils.py index 0bf615f..e15ee56 100644 --- a/MetaTube.bundle/Contents/Code/utils.py +++ b/MetaTube.bundle/Contents/Code/utils.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import os import re +import sys from base64 import b64decode from datetime import datetime @@ -11,6 +12,34 @@ except ImportError: # Python 3 from urllib.parse import unquote +# Python 3 compatible code +if sys.version_info.major == 3: + unicode = str + + +# Based on an answer by John Machin on Stack Overflow: +# - http://stackoverflow.com/questions/8733233/filtering-out-certain-bytes-in-python +def filter_invalid_xml_chars(s): + def is_valid_xml_char(i): + c = ord(i) + return (0x20 <= c <= 0xD7FF or + 0xE000 <= c <= 0xFFFD or + 0x10000 <= c <= 0x10FFFF or + c in (0x9, 0xA, 0xD)) + + return u''.join(c for c in s if is_valid_xml_char(c)) + + +def safe_unicode(o): + if isinstance(o, unicode): + return filter_invalid_xml_chars(o) + elif isinstance(o, list): + return [safe_unicode(v) for v in o] + elif isinstance(o, dict): + return dict((k, safe_unicode(v)) for k, v in o.items()) + else: + return o + def average(a): x = 0.0