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

@ -94,6 +94,15 @@ def clean_dir(dirfullpath):
if os.path.isfile(f): if os.path.isfile(f):
os.remove(f) os.remove(f)
def print_colormsg(msg, level):
if level == 'fail':
print(Colors.FAIL)
elif level == 'warning':
print(Colors.WARNING)
elif level == 'ok':
print(Colors.BLUE)
print(msg)
print(Colors.ENDC)
class Colors: class Colors:

@ -1,6 +1,6 @@
import os, argparse, sys, re import os, argparse, sys, re
from mwclient import Site from mwclient import Site
from functions import print_colormsg
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 value containing spaces should be between quotation marks',
@ -23,7 +23,7 @@ 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.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')
@ -36,22 +36,25 @@ with open(os.path.join(wd, 'login.txt'), 'r') as login: # read login user & pwd
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: loop though images in folder (.jpg, .jpeg, .png) ensuring correct order 1 before 11
if os.path.isdir(args.dir) is False: if os.path.isdir(args.dir) is False:
print(f'Error: --dir {args.dir} absolute path cannot be found') print_colormsg(f'Error: --dir {args.dir} absolute path cannot be found', level='fail')
sys.exit() sys.exit()
elif not re.match(r'\d{4}\/\d{2}\/\d{2}', args.date): 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"') print_colormsg(f'Error: --date {args.date} format should be --date "yyyy/mm/dd"', level='fail')
sys.exit() sys.exit()
elif len(list(site.ask(f'[[Title::{args.title}]]'))) > 0: 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') print_colormsg(f'Error: --title "{args.title}" already exists in wiki. Provide a different one', level='fail')
sys.exit() sys.exit()
# TODO os.listdir(args.dir) ORDER!
# TODO PartOf from os.listdir(args.dir) that fit on to condition
# TODO Part base on os.lisdir order!
for _file in os.listdir(args.dir): for _file in os.listdir(args.dir):
print(_file) if (os.path.splitext(_file)[-1]).lower() not in ['.jpg', '.jpeg', '.png']:
print_colormsg(f'File: {_file} is not a .jpg .jpeg or .png hence will Not be upload', level='warning')
else:
print_colormsg(_file, level='ok')
if args.dry == True: if args.dry == True:
print(args) print(args)
Loading…
Cancel
Save