import os from mwclient import Site from datetime import datetime from pprint import pprint from argparse import ArgumentParser p = ArgumentParser() #p.add_argument("--host", metavar='', default="hub.xpub.nl") #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|sort=Date,Title,Part|order=asc,asc,asc", help="Ask query to be sent to the wiki API.") args = p.parse_args() print args site = Site(host='hub.xpub.nl/sandbox', path='/itchwiki/') 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 loginlines = login.read() user, pwd = loginlines.split('\n') site.login(username=user, password=pwd) # login to wiki # TODO: move query onto argument query = args.ask print('Query:', query) # examples: # [[File:+]][[Year::+]] # [[File:+]][[Year::1984]] # same as: api.php?action=ask&query=[[File:%2B]][[Year::1984]] # can write compound query such as Year::2000 Medium::Flyer def unpack_response(response): # printout is ordered dict d = {} printouts = response['printouts'] page = response['fulltext'] d['page'] = page for prop in printouts: p_item = response['printouts'][prop] for prop_val in p_item: if isinstance(prop_val, dict) is False: d[prop] = prop_val else: # if len(prop_val) > 0: props = list(prop_val.keys()) if 'fulltext' in props: val = prop_val.get('fulltext') elif 'timestamp' in props: val = datetime.fromtimestamp(int(prop_val.get('timestamp'))) else: val = list(prop_val.values())[0] d[prop] = val return(d) for answer in site.ask(query): print(answer, answer.keys()) # print('printouts:', answer['printouts']['Title']) printout_dict = unpack_response(answer) pprint(printout_dict)