arguments

pull/1/head
Castro0o 5 years ago
parent dcec809e99
commit 15f9322eb1

@ -1,36 +1,62 @@
import os, argparse import os, argparse, sys, re
from mwclient import Site from mwclient import Site
p = argparse.ArgumentParser(description="Upload files from a folder", 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',
formatter_class=argparse.ArgumentDefaultsHelpFormatter) formatter_class=argparse.ArgumentDefaultsHelpFormatter)
p.add_argument("--host", metavar='', default="hub.xpub.nl/sandbox", help='wiki host') # TODO: Add example of command to description
p.add_argument("--path", metavar='', default="/itchwiki/", help="Wiki path. Should end with /") p.add_argument('--host', default='hub.xpub.nl/sandbox', help='wiki host')
p.add_argument("--scheme", default="https", help="http or https") 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() args = p.parse_args()
site = Site(host=args.host, path=args.path, scheme=args.scheme) site = Site(host=args.host, path=args.path, scheme=args.scheme)
wd = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # parent working directory wd = os.path.dirname(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 with open(os.path.join(wd, 'login.txt'), 'r') as login: # read login user & pwd
loginlines = login.read() loginlines = login.read()
user, pwd = loginlines.split('\n') user, pwd = loginlines.split('\n')
site.login(username=user, password=pwd) # login to wiki site.login(username=user, password=pwd) # login to wiki
site = Site(host=args.host, path=args.path) site = Site(host=args.host, path=args.path)
# login # login
with open(os.path.join(wd, 'login.txt'), 'r') as login: # read login user & pwd with open(os.path.join(wd, 'login.txt'), 'r') as login: # read login user & pwd
loginlines = login.read() loginlines = login.read()
user, pwd = loginlines.split('\n') user, pwd = loginlines.split('\n')
site.login(username=user, password=pwd) # login to wiki site.login(username=user, password=pwd) # login to wiki
# todo: add arguments: title date creator imgsdir (full path)
# todo: check title existance, chech date format
# todo: loop though images in folder (.jpg, .jpeg, .png) ensuring correct order 1 before 11 # todo: loop though images in folder (.jpg, .jpeg, .png) ensuring correct order 1 before 11
if os.path.isdir(args.dir) is False:
print(f'Error: --dir {args.dir} absolute path cannot be found')
sys.exit()
elif not re.match(r'\d{4}\/\d{2}\/\d{2}', args.date):
print(f'Error: --date {args.date} format should be --date "yyyy/mm/dd"')
sys.exit()
elif len(list(site.ask(f'[[Title::{args.title}]]'))) > 0:
print(f'Error: --title "{args.title}" already exists in wiki. Please provide a different one')
sys.exit()
for _file in os.listdir(args.dir):
print(_file)
if args.dry == True:
print(args)
sys.exit()
smw_prop_val = '''{{ImageMetadata smw_prop_val = '''{{ImageMetadata
|Title={title} |Title={title}

Loading…
Cancel
Save