|
|
@ -1,19 +1,51 @@
|
|
|
|
import os, json
|
|
|
|
import os, json, sys, urllib
|
|
|
|
from mwclient import Site
|
|
|
|
from mwclient import Site
|
|
|
|
from pprint import pprint
|
|
|
|
from pprint import pprint
|
|
|
|
from jinja2 import Template
|
|
|
|
from jinja2 import Template
|
|
|
|
from functions import pandoc, page_props, unpack_response
|
|
|
|
from functions import pandoc, page_props, unpack_response
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p = ArgumentParser(description="From smw ask string generate HTML pages with resulting results.")
|
|
|
|
from functions import Colors
|
|
|
|
p.add_argument("--host", metavar='', default="hub.xpub.nl/sandbox")
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p = argparse.ArgumentParser(description="From smw ask string generate HTML pages with resulting results.",
|
|
|
|
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
|
|
|
|
|
|
|
p.add_argument("--host", metavar='', default="hub.xpub.nl/sandbox", help='wiki host')
|
|
|
|
p.add_argument("--path", metavar='', default="/itchwiki/", help="Wiki path. Should end with /")
|
|
|
|
p.add_argument("--path", metavar='', default="/itchwiki/", help="Wiki path. Should end with /")
|
|
|
|
p.add_argument("--ask", "-a", metavar='',
|
|
|
|
p.add_argument("--conditions", "-c", metavar='',
|
|
|
|
default='[[File:+]][[Title::+]][[Date::+]]|?Title|?Date|?Part|?Partof|sort=Title,Part|order=asc,asc',
|
|
|
|
default='[[File:+]][[Title::+]][[Part::+]][[Date::+]]',
|
|
|
|
help="Ask query to be sent to the wiki API.")
|
|
|
|
help='The query conditions')
|
|
|
|
|
|
|
|
p.add_argument("--printouts", "-p", metavar='',
|
|
|
|
|
|
|
|
default='?Title|?Date|?Part|?Partof',
|
|
|
|
|
|
|
|
help='Selection of properties to printout')
|
|
|
|
|
|
|
|
p.add_argument("--sort", "-s", metavar='',
|
|
|
|
|
|
|
|
default='Date,Title,Part',
|
|
|
|
|
|
|
|
help='Sorting according to conditions')
|
|
|
|
|
|
|
|
p.add_argument("--order", "-o", metavar='',
|
|
|
|
|
|
|
|
default='asc,asc,asc',
|
|
|
|
|
|
|
|
help='Order of sorting conditions. Should same amount as the --sort properties')
|
|
|
|
|
|
|
|
p.add_argument('--dry', '-d', action='store_true',
|
|
|
|
|
|
|
|
help='dry-run: will only show the query but not run it')
|
|
|
|
|
|
|
|
|
|
|
|
args = p.parse_args()
|
|
|
|
args = p.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(args.sort.split(',')) != len(args.order.split(',')):
|
|
|
|
|
|
|
|
print(Colors.FAIL, 'Invalid query:',
|
|
|
|
|
|
|
|
Colors.WARNING, '--sort and --order do not have the same amount of elements', Colors.ENDC)
|
|
|
|
|
|
|
|
print('Script exiting now')
|
|
|
|
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
query = f'{args.conditions}|{args.printouts}|sort={args.sort}|order={args.order}'
|
|
|
|
|
|
|
|
print('query:', Colors.GREEN, query, Colors.ENDC)
|
|
|
|
|
|
|
|
query_unquoted = urllib.parse.quote(query)
|
|
|
|
|
|
|
|
query_url = f'https://{args.host}{args.path}api.php?action=ask&query={query_unquoted}&format=json'
|
|
|
|
|
|
|
|
print('query URL:', query_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if args.dry is True:
|
|
|
|
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# site and login
|
|
|
|
|
|
|
|
|
|
|
|
site = Site(host=args.host, path=args.path)
|
|
|
|
site = Site(host=args.host, path=args.path)
|
|
|
|
|
|
|
|
|
|
|
|
wd = os.path.dirname(os.path.abspath(__file__)) # working directory
|
|
|
|
wd = os.path.dirname(os.path.abspath(__file__)) # working directory
|
|
|
@ -29,6 +61,7 @@ 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# read template files
|
|
|
|
with open(os.path.join(wd, 'templates/publication.html')) as pub_html:
|
|
|
|
with open(os.path.join(wd, 'templates/publication.html')) as pub_html:
|
|
|
|
pub_template = Template(pub_html.read())
|
|
|
|
pub_template = Template(pub_html.read())
|
|
|
|
|
|
|
|
|
|
|
@ -37,7 +70,7 @@ with open(os.path.join(wd, 'templates/publication_part.html')) as pub_html:
|
|
|
|
|
|
|
|
|
|
|
|
pub_parts_html = '' # to append all content
|
|
|
|
pub_parts_html = '' # to append all content
|
|
|
|
|
|
|
|
|
|
|
|
for answer in site.ask(args.ask):
|
|
|
|
for answer in site.ask(query):
|
|
|
|
publication_title = ''
|
|
|
|
publication_title = ''
|
|
|
|
print(answer, answer.keys())
|
|
|
|
print(answer, answer.keys())
|
|
|
|
printout_dict = unpack_response(answer)
|
|
|
|
printout_dict = unpack_response(answer)
|
|
|
|