Add Gravatar fallback and improve logging
This commit is contained in:
parent
da06fe14bd
commit
4af4f84147
@ -2,6 +2,8 @@
|
||||
|
||||
A set of scripts to download profile images from Google and write them to vcards. Useful for completing the migration from Google to Nextcloud or ownCloud.
|
||||
|
||||
As a fallback, if a user doesn't have a Google profile photo, the script will check [Gravatar](https://www.gravatar.com).
|
||||
|
||||
## Setup
|
||||
|
||||
To run, you first need to set up a few config files
|
||||
|
@ -5,6 +5,7 @@ import vobject
|
||||
|
||||
from google_photo_to_vcard.util import build_photo_path
|
||||
from google_photo_to_vcard.util import download_url_to_path
|
||||
from google_photo_to_vcard.util import generate_gravatar_url
|
||||
from google_photo_to_vcard.util import read_email_photo_json
|
||||
|
||||
|
||||
@ -52,15 +53,25 @@ def generate_cards():
|
||||
def main():
|
||||
email_to_photo = read_email_photo_json()
|
||||
for card, card_path in generate_cards():
|
||||
if hasattr(card, 'photo'):
|
||||
logging.info('%s has photo', card.fn.value)
|
||||
continue
|
||||
for email_elem in card.contents.get('email', []):
|
||||
email = email_elem.value
|
||||
photo_path = Path(build_photo_path(email))
|
||||
if not photo_path.exists() and email in email_to_photo:
|
||||
download_url_to_path(email_to_photo[email], photo_path)
|
||||
if not photo_path.exists():
|
||||
if email in email_to_photo:
|
||||
logging.debug('Downloading google photo for %s', card.fn.value)
|
||||
download_url_to_path(email_to_photo[email], photo_path)
|
||||
else:
|
||||
logging.debug('Downloading Gravatar photo for %s', card.fn.value)
|
||||
gravatar_url = generate_gravatar_url(email)
|
||||
download_url_to_path(gravatar_url, photo_path)
|
||||
if photo_path.exists():
|
||||
if maybe_add_photo(card, photo_path):
|
||||
write_card_to_path(card, card_path)
|
||||
logging.info('Added photo to %s', card.fn.value)
|
||||
break
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -2,6 +2,7 @@ import json
|
||||
import logging
|
||||
import urllib.request as request
|
||||
from urllib.error import HTTPError
|
||||
from hashlib import md5
|
||||
|
||||
|
||||
EMAIL_TO_PHOTO_JSON_PATH = 'build/email_to_photo.json'
|
||||
@ -18,14 +19,22 @@ def read_email_photo_json():
|
||||
|
||||
|
||||
def build_photo_path(email):
|
||||
return 'build/photos/{}.jpeg'.format(email)
|
||||
return 'build/photos/{}.jpeg'.format(email.lower())
|
||||
|
||||
|
||||
def download_url_to_path(url, path):
|
||||
try:
|
||||
with open(path, mode='xb') as f, request.urlopen(url) as r:
|
||||
f.write(r.read())
|
||||
with request.urlopen(url) as r:
|
||||
with open(path, mode='xb') as f:
|
||||
f.write(r.read())
|
||||
return path
|
||||
except HTTPError as e:
|
||||
logging.error(e)
|
||||
return None
|
||||
|
||||
|
||||
def generate_gravatar_url(email, size=200, default='404'):
|
||||
email_hash = md5(email.lower().encode('utf-8')).hexdigest()
|
||||
return 'https://www.gravatar.com/avatar/{}?s={}&d={}'.format(
|
||||
email_hash, str(size), default
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user