From 9652ec2a2ff9a68af265f0f906534f4dc3f7e364 Mon Sep 17 00:00:00 2001 From: Castro0o Date: Wed, 19 Feb 2020 09:19:30 +0100 Subject: [PATCH] print functions --- functions.py | 9 ++++----- upload_imgs_dir.py | 23 ++++++++++++++--------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/functions.py b/functions.py index 35707d5..4c6b438 100644 --- a/functions.py +++ b/functions.py @@ -96,13 +96,12 @@ def clean_dir(dirfullpath): def print_colormsg(msg, level): if level == 'fail': - print(Colors.FAIL) + color_cmd = Colors.FAIL elif level == 'warning': - print(Colors.WARNING) + color_cmd = Colors.WARNING elif level == 'ok': - print(Colors.BLUE) - print(msg) - print(Colors.ENDC) + color_cmd = Colors.BLUE + print(color_cmd, msg, Colors.ENDC) class Colors: diff --git a/upload_imgs_dir.py b/upload_imgs_dir.py index 6c4d443..1eb6b4e 100644 --- a/upload_imgs_dir.py +++ b/upload_imgs_dir.py @@ -1,5 +1,6 @@ import os, argparse, sys, re -from mwclient import Site +from mwclient import (Site, + errors) from jinja2 import Template from functions import (print_colormsg, reorder_imgs) @@ -97,15 +98,19 @@ for n, _file in enumerate(lsimgs): ) _file_path = os.path.join(args.dir, _file) - print(_file_path) if not args.dry: + pageurl = f'https://{args.host}{args.path}index.php/File:{pagename}' with open(_file_path, 'rb') as _f: - site.upload(file=_file_path, - filename=pagename, - description=img_smw_prop_val, - ignore=True) - - print(img_smw_prop_val) - print(f'https://{args.host}{args.path}index.php/File:{pagename}') + try: + site.upload(file=_file_path, + filename=pagename, + description=img_smw_prop_val, + ignore=True) + print(img_smw_prop_val) + except errors.APIError as e: + print_colormsg(f'Error: {e.info}\n' + f'It will not be uploaded', + level='fail') + print(f'See image at {pageurl}')