From 3917f3530c9c4690c5e0847f9e2817dfdab2b336 Mon Sep 17 00:00:00 2001 From: clime Date: Sun, 30 Dec 2018 16:35:39 +0100 Subject: [PATCH] add support for extracting relations between emails, openids, and photos from libravatar's export data * support to export script needs to be added too --- import_libravatar.py | 28 ++++++++++++------- .../ivataraccount/read_libravatar_export.py | 13 +++++---- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/import_libravatar.py b/import_libravatar.py index 8240301..fcb437e 100644 --- a/import_libravatar.py +++ b/import_libravatar.py @@ -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() diff --git a/ivatar/ivataraccount/read_libravatar_export.py b/ivatar/ivataraccount/read_libravatar_export.py index b248399..0ec4f81 100644 --- a/ivatar/ivataraccount/read_libravatar_export.py +++ b/ivatar/ivataraccount/read_libravatar_export.py @@ -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 {