From c996cb98b6889910f6850042cb0d66f5d98b9b3c Mon Sep 17 00:00:00 2001 From: Castro0o Date: Tue, 18 Feb 2020 22:54:00 +0100 Subject: [PATCH] smw_prop_val_ template rendered --- functions.py | 16 ++++++++ upload_imgs_dir.py | 97 +++++++++++++++++++++++++++------------------- 2 files changed, 73 insertions(+), 40 deletions(-) diff --git a/functions.py b/functions.py index f938d24..fcf23dd 100644 --- a/functions.py +++ b/functions.py @@ -115,6 +115,8 @@ class Colors: BOLD = '\033[1m' UNDERLINE = '\033[4m' +# image upload function + def listimgs(dir): lsimgs = [_file for _file in os.listdir(dir) if (os.path.splitext(_file)[-1]).lower() in @@ -152,3 +154,17 @@ def reorder_imgs(dir, dry): os.replace(src_img, dst_img) return listimgs(dir) # update list w/ renamed imgs +smw_propval_template = ''' +{{ImageMetadata +|Title={title} +|Date={date} +|Part={part} +|Partof={partof} +|Creator={creator} +|Organization={organization} +|Format={format} +|Event={event} +|Topic={topic} +}} +[[Template:ImageMetadata]] +''' diff --git a/upload_imgs_dir.py b/upload_imgs_dir.py index 062e896..e767508 100644 --- a/upload_imgs_dir.py +++ b/upload_imgs_dir.py @@ -1,7 +1,8 @@ import os, argparse, sys, re from mwclient import Site from functions import (print_colormsg, - reorder_imgs) + reorder_imgs, + smw_propval_template) p = argparse.ArgumentParser(description='Upload files from a directory, with metadata values to the wiki.\n' 'Note that any VALUES CONTAINING ' @@ -10,21 +11,41 @@ p = argparse.ArgumentParser(description='Upload files from a directory, with met # TODO: Add example of command to description p.add_argument('--host', default='hub.xpub.nl/sandbox', help='wiki host') p.add_argument('--path', default='/itchwiki/', help='Wiki path. Should end with /') -p.add_argument('--scheme', default='https', help='http or https') p.add_argument('--dry', '-d', action='store_true', - help='dry-run: will only print the metadata of each file that will be upload, but does NOT upload') - -p.add_argument('--title', required=True, help='Metadata **Title** value of publication. Must not exist yet in the wiki.') -p.add_argument('--date', required=True, help='Metadata **Date** value of publication. Format yyyy/mm/dd ' - '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') + help='dry-run: will only print the metadata of each file that ' + 'will be upload, but does NOT upload') +p.add_argument('--dir', required=True, + help='Full path of the image directory, that you wish to upload') + +p.add_argument('--title', required=True, + help='Metadata **Title** value of publication. Must not exist yet' + ' in the wiki.') +p.add_argument('--date', required=True, + help='Metadata **Date** value of publication. Format yyyy/mm/dd ' + 'For dates without day or mont use 01 as default ' + 'ie. 1986: --date "1986/01/01" ' + 'March 1985: --date "1984/05/01"') +p.add_argument('--creator', required=False, action='append', default=[''], + help='Metadata **Creator** value(s) of publication. Multiple ' + 'values should be SEPARATED BY COMMA') +p.add_argument('--organization', required=False, action='append', default=[''], + help='Metadata **Organization** value(s) of publication. ' + 'Multiple values should be SEPARATED BY COMMA') +p.add_argument('--format', required=False, action='append', default=[''], + help='Metadata **Format** value(s) of publication. ' + 'Multiple values should be SEPARATED BY COMMA') +p.add_argument('--event', required=False, action='append', default=[''], + help='Metadata **Event** value(s) of publication. ' + 'Multiple values should be SEPARATED BY COMMA') +p.add_argument('--topic', required=False, action='append', default=[''], + help='Metadata **Topic** value(s) of publication. ' + 'Multiple values should be SEPARATED BY COMMA') + +# TODO ADD NEW PROPS args = p.parse_args() # login -site = Site(host=args.host, path=args.path, scheme=args.scheme) +site = Site(host=args.host, path=args.path) wd =os.path.dirname(os.path.abspath(__file__)) # parent working directory with open(os.path.join(wd, 'login.txt'), 'r') as login: # read login user & pwd @@ -46,45 +67,41 @@ elif len(list(site.ask(f'[[Title::{args.title}]]'))) > 0: 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! - -# todo creator value comma-seperated -smw_prop_val = '''{{ImageMetadata -|Title={title} -|Date={date} -|Part={part} -|Partof={partof} -|Creator={creator} -}} -[[Template:ImageMetadata]] -''' - -if args.dry == True: - print(args) - sys.exit() +dirname = os.path.split(args.dir)[-1].replace(' ', '_') +dirname = re.sub(r'[\W]', '', dirname) #remove non letters or digits +# print('lsimgs:', lsimgs, '\n', dirname) for n, _file in enumerate(lsimgs): - print_colormsg(_file, level='ok') + pagename = f'{dirname}-{_file}' + print_colormsg(pagename, level='ok') page = site.pages[_file] + if page.exists: url = page.imageinfo['descriptionurl'] print_colormsg( - f'Already exists in {url} Will be uploaded as new version', + f'Already exists in {url} Will NOT be uploaded', level='warning') + else: + img_smw_prop_val = smw_propval_template.format( + title=args.title, + date=args.date, + part=n + 1, + partof=len(lsimgs), + creator=(',').join(args.creator[1:]), + organization=(',').join(args.organization[1:]), + format=(',').join(args.format[1:]), + event=(',').join(args.event[1:]), + topic=(',').join(args.topic[1:]) + ) + + print(img_smw_prop_val) +if args.dry == True: + print(args) + sys.exit() - -img_smw_prop_val = smw_prop_val.format(title='cat from hell', date='2020/02/13', part='1', partof='1', creator='test') -print(img_smw_prop_val) - filename = 'cat2.jpg' page = site.images[filename] img_url = f'https://{args.host}{args.path}/index.php/File:{filename}'