💄nicer log messages

This commit is contained in:
Marc Koch 2021-09-12 15:25:58 +02:00
parent e1380d81c2
commit fec991792b
1 changed files with 13 additions and 8 deletions

21
main.py
View File

@ -23,7 +23,7 @@ def main(config: dict):
try: try:
logging.getLogger().setLevel(config['log_level'].upper()) logging.getLogger().setLevel(config['log_level'].upper())
except ValueError as e: except ValueError as e:
logging.error(f"Could not set log level.\n{e}", exc_info=True) logging.error(f"Could not set log level.\n {e}")
# Read download history from csv file # Read download history from csv file
try: try:
@ -44,14 +44,13 @@ def main(config: dict):
logging.info('Tomorrow\'s newspaper was already downloaded. Execution canceled.') logging.info('Tomorrow\'s newspaper was already downloaded. Execution canceled.')
sys.exit(0) sys.exit(0)
except Exception as e: except Exception as e:
logging.error(f"Could not check whether tomorrow's newspaper has already been downloaded.\n{e}", logging.error(f"Could not check whether tomorrow's newspaper has already been downloaded.\n {e}")
exc_info=True)
# Instantiate downloader object # Instantiate downloader object
try: try:
taz_dl = TazDownloader(config['id'], config['password'], config['download_format']) taz_dl = TazDownloader(config['id'], config['password'], config['download_format'])
except TazDownloadFormatException as e: except TazDownloadFormatException as e:
logging.error(e, exc_info=True) logging.error(e)
sys.exit(1) sys.exit(1)
try: try:
@ -64,7 +63,7 @@ def main(config: dict):
# Find newspaper which are not already downloaded # Find newspaper which are not already downloaded
newspaper_to_download = [n for n in newspaper_available if n not in df.file.values] newspaper_to_download = [n for n in newspaper_available if n not in df.file.values]
except TazDownloadError as e: except TazDownloadError as e:
logging.error(e, exc_info=True) logging.error(e)
sys.exit(1) sys.exit(1)
# Download newspaper # Download newspaper
@ -74,7 +73,7 @@ def main(config: dict):
if taz_dl.download_newspaper(n, tmp_folder): if taz_dl.download_newspaper(n, tmp_folder):
newspaper_downloaded.append(n) newspaper_downloaded.append(n)
except Exception as e: except Exception as e:
logging.error(f"Could not download {n}\n{e}", exc_info=True) logging.error(f"Could not download {n}\n {e}")
# Add downloaded newspaper to download_history.csv # Add downloaded newspaper to download_history.csv
try: try:
@ -89,9 +88,10 @@ def main(config: dict):
df.sort_values(by='file', ascending=False, inplace=True) df.sort_values(by='file', ascending=False, inplace=True)
df.to_csv(os.path.join(dir_path, 'download_history.csv'), index=False) df.to_csv(os.path.join(dir_path, 'download_history.csv'), index=False)
except Exception as e: except Exception as e:
logging.error(f"Could not update download_history.csv\n{e}", exc_info=True) logging.error(f"Could not update download_history.csv\n {e}")
# Move downloaded file to download folder # Move downloaded file to download folder
newspaper_downloaded_string = "\n ".join(newspaper_downloaded)
if os.path.isdir(config['download_folder']): if os.path.isdir(config['download_folder']):
download_folder = \ download_folder = \
config['download_folder'] \ config['download_folder'] \
@ -101,7 +101,12 @@ def main(config: dict):
try: try:
shutil.move(os.path.join(tmp_folder, n), download_folder) shutil.move(os.path.join(tmp_folder, n), download_folder)
except Exception as e: except Exception as e:
logging.error(f"Could not move file to download folder \"{download_folder}\"\n{e}", exc_info=True) logging.error(f"Could not move {n} to download folder \"{download_folder}\"\n {e}")
if newspaper_downloaded:
logging.info(f"Downloaded\n {newspaper_downloaded_string}\n to {config['download_folder']}")
else:
logging.error(f"{config['download_folder']} does not exists.\n {newspaper_downloaded_string}"
f"\n downloaded to {tmp_folder}")
if __name__ == '__main__': if __name__ == '__main__':