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.
|
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
|
## Setup
|
||||||
|
|
||||||
To run, you first need to set up a few config files
|
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 build_photo_path
|
||||||
from google_photo_to_vcard.util import download_url_to_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
|
from google_photo_to_vcard.util import read_email_photo_json
|
||||||
|
|
||||||
|
|
||||||
@ -52,15 +53,25 @@ def generate_cards():
|
|||||||
def main():
|
def main():
|
||||||
email_to_photo = read_email_photo_json()
|
email_to_photo = read_email_photo_json()
|
||||||
for card, card_path in generate_cards():
|
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', []):
|
for email_elem in card.contents.get('email', []):
|
||||||
email = email_elem.value
|
email = email_elem.value
|
||||||
photo_path = Path(build_photo_path(email))
|
photo_path = Path(build_photo_path(email))
|
||||||
if not photo_path.exists() and email in email_to_photo:
|
if not photo_path.exists():
|
||||||
download_url_to_path(email_to_photo[email], photo_path)
|
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 photo_path.exists():
|
||||||
if maybe_add_photo(card, photo_path):
|
if maybe_add_photo(card, photo_path):
|
||||||
write_card_to_path(card, card_path)
|
write_card_to_path(card, card_path)
|
||||||
logging.info('Added photo to %s', card.fn.value)
|
logging.info('Added photo to %s', card.fn.value)
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -2,6 +2,7 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import urllib.request as request
|
import urllib.request as request
|
||||||
from urllib.error import HTTPError
|
from urllib.error import HTTPError
|
||||||
|
from hashlib import md5
|
||||||
|
|
||||||
|
|
||||||
EMAIL_TO_PHOTO_JSON_PATH = 'build/email_to_photo.json'
|
EMAIL_TO_PHOTO_JSON_PATH = 'build/email_to_photo.json'
|
||||||
@ -18,14 +19,22 @@ def read_email_photo_json():
|
|||||||
|
|
||||||
|
|
||||||
def build_photo_path(email):
|
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):
|
def download_url_to_path(url, path):
|
||||||
try:
|
try:
|
||||||
with open(path, mode='xb') as f, request.urlopen(url) as r:
|
with request.urlopen(url) as r:
|
||||||
f.write(r.read())
|
with open(path, mode='xb') as f:
|
||||||
|
f.write(r.read())
|
||||||
return path
|
return path
|
||||||
except HTTPError as e:
|
except HTTPError as e:
|
||||||
logging.error(e)
|
logging.error(e)
|
||||||
return None
|
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