Commit 93e0e624 authored by Oliver Falk's avatar Oliver Falk

Add script to import a full libravatar export

parent 2643e118
Pipeline #851 passed with stage
in 4 minutes and 36 seconds
#!/usr/bin/env python
'''
Import the whole libravatar export
'''
import os
from os.path import isfile, isdir, join
import sys
import base64
from io import BytesIO
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ivatar.settings") # pylint: disable=wrong-import-position
django.setup() # pylint: disable=wrong-import-position
from django.contrib.auth.models import User
from PIL import Image
from ivatar.settings import JPEG_QUALITY
from ivatar.ivataraccount.read_libravatar_export import read_gzdata as libravatar_read_gzdata
from ivatar.ivataraccount.models import ConfirmedEmail
from ivatar.ivataraccount.models import ConfirmedOpenId
from ivatar.ivataraccount.models import Photo
from ivatar.ivataraccount.models import file_format
if len(sys.argv) < 2:
print("First argument to '%s' must be the path to the exports" % sys.argv[0])
exit(-255)
if not isdir(sys.argv[1]):
print("First argument to '%s' must be a directory containing the exports" % sys.argv[0])
exit(-255)
PATH = sys.argv[1]
for file in os.listdir(PATH):
if not file.endswith('.xml.gz'):
continue
if isfile(join(PATH, file)):
fh = open(join(PATH, file), 'rb')
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)
for photo in items['photos']:
data = base64.decodebytes(bytes(photo['data'], 'utf-8'))
pilobj = Image.open(BytesIO(data))
out = BytesIO()
pilobj.save(out, pilobj.format, quality=JPEG_QUALITY)
out.seek(0)
photo = Photo()
photo.user = user
photo.ip_address = '0.0.0.0'
photo.format = file_format(pilobj.format)
photo.data = out.read()
photo.save()
fh.close()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment