You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
special-issue-11-wiki2html/sandbox/ask_wiki.py

61 lines
1.7 KiB
Python

5 years ago
import os
from mwclient import Site
from datetime import datetime
from pprint import pprint
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
query = ('[[File:+]][[Title::+]][[Date::+]]|?Title|?Date|?Part|sort=Date,Title,Part|order=asc,asc,asc')
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]
5 years ago
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)