mirror of
https://git.linux-kernel.at/oliver/ivatar.git
synced 2025-11-11 18:56:23 +00:00
Add script to import a full libravatar export
This commit is contained in:
63
import_libravatar.py
Normal file
63
import_libravatar.py
Normal file
@@ -0,0 +1,63 @@
|
||||
#!/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()
|
||||
Reference in New Issue
Block a user