mirror of
https://git.linux-kernel.at/oliver/ivatar.git
synced 2025-11-16 13:08:03 +00:00
Some safety measures to avoid breaking old/new export and reformat with black
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
'''
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
Reading libravatar export
|
Reading libravatar export
|
||||||
'''
|
"""
|
||||||
|
|
||||||
import binascii
|
import binascii
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
@@ -10,13 +11,13 @@ import base64
|
|||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
SCHEMAROOT = 'https://www.libravatar.org/schemas/export/0.2'
|
SCHEMAROOT = "https://www.libravatar.org/schemas/export/0.2"
|
||||||
|
|
||||||
|
|
||||||
def read_gzdata(gzdata=None):
|
def read_gzdata(gzdata=None):
|
||||||
'''
|
"""
|
||||||
Read gzipped data file
|
Read gzipped data file
|
||||||
'''
|
"""
|
||||||
emails = [] # pylint: disable=invalid-name
|
emails = [] # pylint: disable=invalid-name
|
||||||
openids = [] # pylint: disable=invalid-name
|
openids = [] # pylint: disable=invalid-name
|
||||||
photos = [] # pylint: disable=invalid-name
|
photos = [] # pylint: disable=invalid-name
|
||||||
@@ -26,59 +27,82 @@ def read_gzdata(gzdata=None):
|
|||||||
if not gzdata:
|
if not gzdata:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
fh = gzip.open(BytesIO(gzdata), 'rb') # pylint: disable=invalid-name
|
fh = gzip.open(BytesIO(gzdata), "rb") # pylint: disable=invalid-name
|
||||||
content = fh.read()
|
content = fh.read()
|
||||||
fh.close()
|
fh.close()
|
||||||
root = xml.etree.ElementTree.fromstring(content)
|
root = xml.etree.ElementTree.fromstring(content)
|
||||||
if not root.tag == '{%s}user' % SCHEMAROOT:
|
if not root.tag == "{%s}user" % SCHEMAROOT:
|
||||||
print('Unknown export format: %s' % root.tag)
|
print("Unknown export format: %s" % root.tag)
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
|
||||||
# Username
|
# Username
|
||||||
for item in root.findall('{%s}account' % SCHEMAROOT)[0].items():
|
for item in root.findall("{%s}account" % SCHEMAROOT)[0].items():
|
||||||
if item[0] == 'username':
|
if item[0] == "username":
|
||||||
username = item[1]
|
username = item[1]
|
||||||
if item[0] == 'password':
|
if item[0] == "password":
|
||||||
password = item[1]
|
password = item[1]
|
||||||
|
|
||||||
# Emails
|
# Emails
|
||||||
for email in root.findall('{%s}emails' % SCHEMAROOT)[0]:
|
for email in root.findall("{%s}emails" % SCHEMAROOT)[0]:
|
||||||
if email.tag == '{%s}email' % SCHEMAROOT:
|
if email.tag == "{%s}email" % SCHEMAROOT:
|
||||||
emails.append({'email': email.text, 'photo_id': email.attrib['photo_id']})
|
emails.append({"email": email.text, "photo_id": email.attrib["photo_id"]})
|
||||||
|
|
||||||
# OpenIDs
|
# OpenIDs
|
||||||
for openid in root.findall('{%s}openids' % SCHEMAROOT)[0]:
|
for openid in root.findall("{%s}openids" % SCHEMAROOT)[0]:
|
||||||
if openid.tag == '{%s}openid' % SCHEMAROOT:
|
if openid.tag == "{%s}openid" % SCHEMAROOT:
|
||||||
openids.append({'openid': openid.text, 'photo_id': openid.attrib['photo_id']})
|
openids.append(
|
||||||
|
{"openid": openid.text, "photo_id": openid.attrib["photo_id"]}
|
||||||
|
)
|
||||||
|
|
||||||
# Photos
|
# Photos
|
||||||
for photo in root.findall('{%s}photos' % SCHEMAROOT)[0]:
|
for photo in root.findall("{%s}photos" % SCHEMAROOT)[0]:
|
||||||
if photo.tag == '{%s}photo' % SCHEMAROOT:
|
if photo.tag == "{%s}photo" % SCHEMAROOT:
|
||||||
try:
|
try:
|
||||||
data = base64.decodebytes(bytes(photo.text, 'utf-8'))
|
# Safty measures to make sure we do not try to parse
|
||||||
|
# a binary encoded string
|
||||||
|
photo.text = photo.text.strip("'")
|
||||||
|
photo.text = photo.text.strip("\\n")
|
||||||
|
photo.text = photo.text.lstrip("b'")
|
||||||
|
data = base64.decodebytes(bytes(photo.text, "utf-8"))
|
||||||
except binascii.Error as exc:
|
except binascii.Error as exc:
|
||||||
print('Cannot decode photo; Encoding: %s, Format: %s, Id: %s: %s' % (
|
print(
|
||||||
photo.attrib['encoding'], photo.attrib['format'], photo.attrib['id'], exc))
|
"Cannot decode photo; Encoding: %s, Format: %s, Id: %s: %s"
|
||||||
|
% (
|
||||||
|
photo.attrib["encoding"],
|
||||||
|
photo.attrib["format"],
|
||||||
|
photo.attrib["id"],
|
||||||
|
exc,
|
||||||
|
)
|
||||||
|
)
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
Image.open(BytesIO(data))
|
Image.open(BytesIO(data))
|
||||||
except Exception as exc: # pylint: disable=broad-except
|
except Exception as exc: # pylint: disable=broad-except
|
||||||
print('Cannot decode photo; Encoding: %s, Format: %s, Id: %s: %s' % (
|
print(
|
||||||
photo.attrib['encoding'], photo.attrib['format'], photo.attrib['id'], exc))
|
"Cannot decode photo; Encoding: %s, Format: %s, Id: %s: %s"
|
||||||
|
% (
|
||||||
|
photo.attrib["encoding"],
|
||||||
|
photo.attrib["format"],
|
||||||
|
photo.attrib["id"],
|
||||||
|
exc,
|
||||||
|
)
|
||||||
|
)
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
# If it is a working image, we can use it
|
# If it is a working image, we can use it
|
||||||
photo.text.replace('\n', '')
|
photo.text.replace("\n", "")
|
||||||
photos.append({
|
photos.append(
|
||||||
'data': photo.text,
|
{
|
||||||
'format': photo.attrib['format'],
|
"data": photo.text,
|
||||||
'id': photo.attrib['id'],
|
"format": photo.attrib["format"],
|
||||||
})
|
"id": photo.attrib["id"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'emails': emails,
|
"emails": emails,
|
||||||
'openids': openids,
|
"openids": openids,
|
||||||
'photos': photos,
|
"photos": photos,
|
||||||
'username': username,
|
"username": username,
|
||||||
'password': password,
|
"password": password,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user