|
|
|
import os, argparse, sys, re
|
|
|
|
from mwclient import Site
|
|
|
|
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 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')
|
|
|
|
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')
|
|
|
|
args = p.parse_args()
|
|
|
|
|
|
|
|
# login
|
|
|
|
site = Site(host=args.host, path=args.path, scheme=args.scheme)
|
|
|
|
|
|
|
|
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
|
|
|
|
loginlines = login.read()
|
|
|
|
user, pwd = loginlines.split('\n')
|
|
|
|
site.login(username=user, password=pwd) # login to wiki
|
|
|
|
|
|
|
|
# metadata checks
|
|
|
|
if os.path.isdir(args.dir) is False:
|
|
|
|
print_colormsg(f'Error: --dir {args.dir} absolute path cannot be found', level='fail')
|
|
|
|
sys.exit()
|
|
|
|
elif not re.match(r'\d{4}\/\d{2}\/\d{2}', args.date):
|
|
|
|
print_colormsg(f'Error: --date {args.date} format should be --date "yyyy/mm/dd"', level='fail')
|
|
|
|
sys.exit()
|
|
|
|
elif len(list(site.ask(f'[[Title::{args.title}]]'))) > 0:
|
|
|
|
print_colormsg(f'Error: --title "{args.title}" already exists in wiki. Provide a different one', level='fail')
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
# TODO os.listdir(args.dir) ORDER!
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
for n, _file in enumerate(lsimgs):
|
|
|
|
print_colormsg(_file, 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',
|
|
|
|
level='warning')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}'
|
|
|
|
|
|
|
|
if page.exists is True:
|
|
|
|
print(f'{filename} is already upload it, you can see it at {img_url}' )
|
|
|
|
print('The upload process wont proceed. Please upload all images in folder by hand')
|
|
|
|
else:
|
|
|
|
with open(filename, 'rb') as _file:
|
|
|
|
dirname = (os.path.split(dir)[-1])
|
|
|
|
site.upload(file=_file, filename=f'{dirname}_{filename}',
|
|
|
|
description='img_smw_prop_val', ignore=True)
|
|
|
|
|