ask script
parent
653b35cef1
commit
c86056a0bc
@ -0,0 +1,60 @@
|
||||
|
||||
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 = answer['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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue