Switch to logging

This commit is contained in:
IamTheFij 2018-04-04 16:59:49 -07:00
parent 4814e7156b
commit 47916aafd7
4 changed files with 20 additions and 10 deletions

View File

@ -1,3 +1,4 @@
import logging
from pathlib import Path from pathlib import Path
import vobject import vobject
@ -7,6 +8,9 @@ from google_photo_to_vcard.util import download_url_to_path
from google_photo_to_vcard.util import read_email_photo_json from google_photo_to_vcard.util import read_email_photo_json
logging.basicConfig(level=logging.DEBUG)
def open_card(card_path): def open_card(card_path):
with open(card_path, mode='r') as f: with open(card_path, mode='r') as f:
return vobject.readOne(f.read()) return vobject.readOne(f.read())
@ -14,7 +18,7 @@ def open_card(card_path):
def maybe_add_photo(card, photo_path): def maybe_add_photo(card, photo_path):
if hasattr(card, 'photo'): if hasattr(card, 'photo'):
print('{} has photo'.format(card.fn.value)) logging.info('%s has photo', card.fn.value)
return False return False
photo = card.add('photo') photo = card.add('photo')
photo.params = { photo.params = {
@ -56,7 +60,7 @@ def main():
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)
print('Added photo to', card.fn.value) logging.info('Added photo to %s', card.fn.value)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,10 +1,8 @@
from __future__ import print_function
import httplib2 import httplib2
import json import json
import logging import logging
import os import os
import time import time
from pprint import pprint
from apiclient import discovery from apiclient import discovery
from googleapiclient.errors import HttpError from googleapiclient.errors import HttpError
@ -99,7 +97,7 @@ def get_credentials():
credentials = tools.run_flow(flow, store, flags) credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6 else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store) credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path) logging.info('Storing credentials to ' + credential_path)
return credentials return credentials
@ -160,9 +158,12 @@ def main():
len(email_to_photo), len(email_to_photo),
)) ))
# pprint(email_to_photo)
write_email_photo_json(email_to_photo) write_email_photo_json(email_to_photo)
print('All done!') logging.info('All done!')
logging.info('Found {} photos for {} emails'.format(
len(all_photos),
len(email_to_photo),
))
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,3 +1,4 @@
import logging
import urllib.request as request import urllib.request as request
from pathlib import Path from pathlib import Path
from urllib.error import HTTPError from urllib.error import HTTPError
@ -7,13 +8,16 @@ from google_photo_to_vcard.util import download_url_to_path
from google_photo_to_vcard.util import read_email_photo_json from google_photo_to_vcard.util import read_email_photo_json
logging.basicConfig(level=logging.DEBUG)
def main(): def main():
email_to_photo = read_email_photo_json() email_to_photo = read_email_photo_json()
for email, photo_url in email_to_photo.items(): for email, photo_url in email_to_photo.items():
print(email, photo_url) loging.info('Downloading (%s, %s)', email, photo_url)
photo_path = Path(build_photo_path(email)) photo_path = Path(build_photo_path(email))
if photo_path.exists(): if photo_path.exists():
print('Photo already downloaded') loging.debug('Photo already downloaded')
continue continue
else: else:
download_url_to_path(photo_url, photo_path) download_url_to_path(photo_url, photo_path)

View File

@ -1,4 +1,5 @@
import json import json
import logging
import urllib.request as request import urllib.request as request
from urllib.error import HTTPError from urllib.error import HTTPError
@ -26,5 +27,5 @@ def download_url_to_path(url, path):
f.write(r.read()) f.write(r.read())
return path return path
except HTTPError as e: except HTTPError as e:
print(e) logging.error(e)
return None return None