From 59ba70bebb0e840dcfb28ab8be5f9660c5a5743f Mon Sep 17 00:00:00 2001 From: Castro0o Date: Tue, 18 Feb 2020 15:56:00 +0100 Subject: [PATCH] images reorder --- functions.py | 38 +++++++++++++++++++++++++++++++++++++- upload_imgs_dir.py | 26 ++++++++++++++++---------- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/functions.py b/functions.py index da7b14c..09b7305 100644 --- a/functions.py +++ b/functions.py @@ -113,4 +113,40 @@ class Colors: FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' - UNDERLINE = '\033[4m' \ No newline at end of file + UNDERLINE = '\033[4m' + +def listimgs(dir): + lsimgs = [_file for _file in os.listdir(dir) if + (os.path.splitext(_file)[-1]).lower() in + ['.jpg', '.jpeg', '.png']] + lsimgs.sort() + return lsimgs + + +def reorder_imgs(dir, dry): + lsimgs = listimgs(dir) + for img in lsimgs: + img_name, img_ext = os.path.splitext(img) + + # does file follow \d{1,}\.img_ext + numb_exp = re.compile( + r'(?P.*?)(?P\d+)(?P%s)'% re.escape(img_ext)) + match = re.search(numb_exp, img) + if not match: + print(f'Image {img} Filename is not suitable for bulk upload.' + f'Filename pattern does not match 1.jpg 01.jpg' + f'You have to DO IT MANUALLY') + else: + # only change name of single digit numbers + if len(match.groupdict()['num']) == 1: + name = match.groupdict()['name'] + num = match.groupdict()['num'].zfill(2) # pad with 0 + ext = match.groupdict()['ext'] + new_img = name + num + ext + if dry == False: + os.rename(os.path.join(dir, img), + os.path.join(dir, new_img)) + print(f'Renaming: {img} >>>>> {new_img}') + + return listimgs(dir) + diff --git a/upload_imgs_dir.py b/upload_imgs_dir.py index 085384b..58e2154 100644 --- a/upload_imgs_dir.py +++ b/upload_imgs_dir.py @@ -1,9 +1,11 @@ import os, argparse, sys, re from mwclient import Site -from functions import print_colormsg +from functions import (print_colormsg, + reorder_imgs) p = argparse.ArgumentParser(description='Upload files from a directory, with metadata values to the wiki.\n' - 'Note that any value containing spaces should be between quotation marks', + 'Note that any VALUES CONTAINING ' + 'SPACES SHOULD BE BETWEEN QUOTATION MARKS', formatter_class=argparse.ArgumentDefaultsHelpFormatter) # TODO: Add example of command to description p.add_argument('--host', default='hub.xpub.nl/sandbox', help='wiki host') @@ -17,7 +19,8 @@ p.add_argument('--date', required=True, help='Metadata **Date** value of publica 'For dates without day or date use 01 ie. 1986 --date "1986/01/01" March 1985: --date "1984/05/01"') p.add_argument('--creator', required=True, action='append', help='Metadata **Creator** value(s) of publication.' 'Multiple values should be SEPARATED BY COMMA') -p.add_argument('--dir', required=True, help='Full path of the image directory, that you wish to upload') +p.add_argument('--dir', required=True, help='Full path of the image directory, ' + 'that you wish to upload') args = p.parse_args() # login @@ -42,10 +45,13 @@ elif len(list(site.ask(f'[[Title::{args.title}]]'))) > 0: # TODO os.listdir(args.dir) ORDER! -lsimgs = [_file for _file in os.listdir(args.dir) if (os.path.splitext(_file)[-1]).lower() in - ['.jpg', '.jpeg', '.png']] -lsimgs.sort() + +lsimgs = reorder_imgs(dir=args.dir, dry=args.dry) + print('lsimgs:', lsimgs) + +sys.exit() ## STOP HERE + # TODO PartOf from os.listdir(args.dir) that fit on to condition # TODO Part base on os.lisdir order! @@ -64,7 +70,7 @@ if args.dry == True: print(args) sys.exit() -for n _file in enumerate(lsimgs): +for n, _file in enumerate(lsimgs): print_colormsg(_file, level='ok') page = site.pages[_file] if page.exists: @@ -72,8 +78,6 @@ for n _file in enumerate(lsimgs): print_colormsg( f'Already exists in {url} Will be uploaded as new version', level='warning') - smw_prop_val.format(title=, - ) @@ -91,5 +95,7 @@ if page.exists is True: print('The upload process wont proceed. Please upload all images in folder by hand') else: with open(filename, 'rb') as _file: - site.upload(file=_file, filename=filename, description='img_smw_prop_val', ignore=True) + dirname = (os.path.split(dir)[-1]) + site.upload(file=_file, filename=f'{dirname}_{filename}', + description='img_smw_prop_val', ignore=True)