ask broken down into several arguments; --dry run

andre
Castro0o 4 years ago
parent 595fcb0ccd
commit d31c4aa483

@ -45,8 +45,12 @@ Run scripts together with `./run.sh`
`python3 publication2html.py`
* with ask API perform a query:
* default query is: `[[File:+]][[Title::+]][[Date::+]]|?Title|?Date|?Part|sort=Date,Title,Part|order=asc,asc,asc`
* custom query `python3 publication2html.py --ask '[[File:+]][[Title::+]][[Date::+]]|?Date|order=desc'`
* help `python3 publication2html.py --help`
* run dry `python3 publication2html.py --dry` only printing request, not executing it
* build custom query with arguments `--conditions --printouts --sort --order`
* default query is: `[[File:+]][[Title::+]][[Part::+]][[Date::+]]|?Title|?Date|?Part|?Partof|sort=Date,Title,Part|order=asc,asc,asc`
* custom query `python3 publication2html.py -c '[[Date::>=1970/01/01]][[Date::<=1979/12/31]]' -p '?Title|?Date|?Part|?Partof' -s 'Date,Title,Part' -o 'asc,asc,asc'`
* The results, with the same Title, are stored
* into 1 single HTML
* sorted by Part
@ -54,4 +58,4 @@ Run scripts together with `./run.sh`
## TODO
* Fix issue with PANDOC conversion MW -> HTML
* remove HTML files at each new query

@ -83,4 +83,15 @@ def update_json(imgsjson_fn, img_dict, img_fn):
with open(imgsjson_fn, 'w') as imgsjson_file:
json.dump(imgsjson_dict, imgsjson_file, indent=4)
return download
return download
class Colors:
HEADER = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

@ -1,19 +1,51 @@
import os, json
import os, json, sys, urllib
from mwclient import Site
from pprint import pprint
from jinja2 import Template
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.")
p.add_argument("--host", metavar='', default="hub.xpub.nl/sandbox")
from functions import Colors
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("--ask", "-a", metavar='',
default='[[File:+]][[Title::+]][[Date::+]]|?Title|?Date|?Part|?Partof|sort=Title,Part|order=asc,asc',
help="Ask query to be sent to the wiki API.")
p.add_argument("--conditions", "-c", metavar='',
default='[[File:+]][[Title::+]][[Part::+]][[Date::+]]',
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()
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)
wd = os.path.dirname(os.path.abspath(__file__)) # working directory
@ -29,15 +61,16 @@ with open(os.path.join(wd, 'login.txt'), 'r') as login: # read login user & pwd
user, pwd = loginlines.split('\n')
site.login(username=user, password=pwd) # login to wiki
# read template files
with open(os.path.join(wd, 'templates/publication.html')) as pub_html:
pub_template = Template(pub_html.read())
with open(os.path.join(wd, 'templates/publication_part.html')) as pub_html:
pub_part_template = Template(pub_html.read())
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 = ''
print(answer, answer.keys())
printout_dict = unpack_response(answer)
@ -76,4 +109,4 @@ for answer in site.ask(args.ask):
# TODO: address Dates:
# * date values coming from mw with timestamp, the missing valus are one
# This a MW issue!
# This a MW issue!

Loading…
Cancel
Save