Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: make XML compatible #33

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions MetaTube.bundle/Contents/Code/api_client.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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')

Expand Down
29 changes: 29 additions & 0 deletions MetaTube.bundle/Contents/Code/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import os
import re
import sys
from base64 import b64decode
from datetime import datetime

Expand All @@ -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
Expand Down