images reorder

docker
Castro0o 5 years ago
parent 1c8ca7d045
commit 59ba70bebb

@ -113,4 +113,40 @@ class Colors:
FAIL = '\033[91m' FAIL = '\033[91m'
ENDC = '\033[0m' ENDC = '\033[0m'
BOLD = '\033[1m' BOLD = '\033[1m'
UNDERLINE = '\033[4m' 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<name>.*?)(?P<num>\d+)(?P<ext>%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)

@ -1,9 +1,11 @@
import os, argparse, sys, re import os, argparse, sys, re
from mwclient import Site 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' 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) formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# TODO: Add example of command to description # TODO: Add example of command to description
p.add_argument('--host', default='hub.xpub.nl/sandbox', help='wiki host') 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"') '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.' p.add_argument('--creator', required=True, action='append', help='Metadata **Creator** value(s) of publication.'
'Multiple values should be SEPARATED BY COMMA') '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() args = p.parse_args()
# login # login
@ -42,10 +45,13 @@ elif len(list(site.ask(f'[[Title::{args.title}]]'))) > 0:
# TODO os.listdir(args.dir) ORDER! # 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 = reorder_imgs(dir=args.dir, dry=args.dry)
lsimgs.sort()
print('lsimgs:', lsimgs) print('lsimgs:', lsimgs)
sys.exit() ## STOP HERE
# TODO PartOf from os.listdir(args.dir) that fit on to condition # TODO PartOf from os.listdir(args.dir) that fit on to condition
# TODO Part base on os.lisdir order! # TODO Part base on os.lisdir order!
@ -64,7 +70,7 @@ if args.dry == True:
print(args) print(args)
sys.exit() sys.exit()
for n _file in enumerate(lsimgs): for n, _file in enumerate(lsimgs):
print_colormsg(_file, level='ok') print_colormsg(_file, level='ok')
page = site.pages[_file] page = site.pages[_file]
if page.exists: if page.exists:
@ -72,8 +78,6 @@ for n _file in enumerate(lsimgs):
print_colormsg( print_colormsg(
f'Already exists in {url} Will be uploaded as new version', f'Already exists in {url} Will be uploaded as new version',
level='warning') 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') print('The upload process wont proceed. Please upload all images in folder by hand')
else: else:
with open(filename, 'rb') as _file: 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)

Loading…
Cancel
Save