add support for extracting relations between emails, openids, and photos from libravatar's export data

* support to export script needs to be added too
This commit is contained in:
clime
2018-12-30 16:35:39 +01:00
parent e0e33251a6
commit 3917f3530c
2 changed files with 25 additions and 16 deletions

View File

@@ -37,17 +37,10 @@ for file in os.listdir(PATH):
items = libravatar_read_gzdata(fh.read())
print('Adding user "%s"' % items['username'])
(user, created) = User.objects.get_or_create(username=items['username'])
for email in items['emails']:
try:
ConfirmedEmail.objects.get_or_create(email=email, user=user)
except django.db.utils.IntegrityError:
print('%s not unique?' % email)
for openid in items['openids']:
try:
ConfirmedOpenId.objects.get_or_create(openid=openid, user=user) # pylint: disable=no-member
except django.db.utils.IntegrityError:
print('%s not unique?' % openid)
saved_photos = {}
for photo in items['photos']:
photo_id = photo['id']
data = base64.decodebytes(bytes(photo['data'], 'utf-8'))
pilobj = Image.open(BytesIO(data))
out = BytesIO()
@@ -59,5 +52,20 @@ for file in os.listdir(PATH):
photo.format = file_format(pilobj.format)
photo.data = out.read()
photo.save()
saved_photos[photo_id] = photo
for email in items['emails']:
try:
ConfirmedEmail.objects.get_or_create(email=email['email'], user=user,
photo=saved_photos.get(email['photo_id']))
except django.db.utils.IntegrityError:
print('%s not unique?' % email['email'])
for openid in items['openids']:
try:
ConfirmedOpenId.objects.get_or_create(openid=openid['openid'], user=user,
photo=saved_photos.get(openid['photo_id'])) # pylint: disable=no-member
except django.db.utils.IntegrityError:
print('%s not unique?' % openid['openid'])
fh.close()

View File

@@ -41,12 +41,12 @@ def read_gzdata(gzdata=None):
# Emails
for email in root.findall('{%s}emails' % SCHEMAROOT)[0]:
if email.tag == '{%s}email' % SCHEMAROOT:
emails.append(email.text)
emails.append({'email': email.text, 'photo_id': email.attrib['photo_id']})
# OpenIDs
for openid in root.findall('{%s}openids' % SCHEMAROOT)[0]:
if openid.tag == '{%s}openid' % SCHEMAROOT:
openids.append(openid.text)
openids.append({'openid': openid.text, 'photo_id': openid.attrib['photo_id']})
# Photos
for photo in root.findall('{%s}photos' % SCHEMAROOT)[0]:
@@ -54,14 +54,14 @@ def read_gzdata(gzdata=None):
try:
data = base64.decodebytes(bytes(photo.text, 'utf-8'))
except binascii.Error as exc:
print('Cannot decode photo; Encoding: %s, Format: %s: %s' % (
photo.attrib['encoding'], photo.attrib['format'], exc))
print('Cannot decode photo; Encoding: %s, Format: %s, Id: %s: %s' % (
photo.attrib['encoding'], photo.attrib['format'], photo.attrib['id'], exc))
continue
try:
Image.open(BytesIO(data))
except Exception as exc: # pylint: disable=broad-except
print('Cannot decode photo; Encoding: %s, Format: %s: %s' % (
photo.attrib['encoding'], photo.attrib['format'], exc))
print('Cannot decode photo; Encoding: %s, Format: %s, Id: %s: %s' % (
photo.attrib['encoding'], photo.attrib['format'], photo.attrib['id'], exc))
continue
else:
# If it is a working image, we can use it
@@ -69,6 +69,7 @@ def read_gzdata(gzdata=None):
photos.append({
'data': photo.text,
'format': photo.attrib['format'],
'id': photo.attrib['id'],
})
return {