google-photo-to-vcard/google_photo_to_vcard/util.py

32 lines
732 B
Python
Raw Normal View History

2018-04-04 23:55:09 +00:00
import json
2018-04-04 23:59:49 +00:00
import logging
2018-04-04 23:55:09 +00:00
import urllib.request as request
from urllib.error import HTTPError
EMAIL_TO_PHOTO_JSON_PATH = 'build/email_to_photo.json'
def write_email_photo_json(email_to_photo):
with open(EMAIL_TO_PHOTO_JSON_PATH, 'w') as f:
f.write(json.dumps(email_to_photo))
def read_email_photo_json():
with open(EMAIL_TO_PHOTO_JSON_PATH, 'r') as f:
return json.loads(f.read())
def build_photo_path(email):
return 'build/photos/{}.jpeg'.format(email)
def download_url_to_path(url, path):
try:
with open(path, mode='xb') as f, request.urlopen(url) as r:
f.write(r.read())
return path
except HTTPError as e:
2018-04-04 23:59:49 +00:00
logging.error(e)
2018-04-04 23:55:09 +00:00
return None