Page MenuHomePhabricator
Paste P7506

Pywikibot api_query sample
ActivePublic

Authored by Framawiki on Sep 1 2018, 9:26 AM.
Referenced Files
F25582241: Pywikibot api_query sample
Sep 1 2018, 9:26 AM
Subscribers
None
import pywikibot
from pywikibot.data import api
def api_query(params, lang='fr', wiki='wikipedia'):
query = api.Request(site=pywikibot.Site(lang, wiki), parameters=params)
datas = query.submit()
return datas
# and to use it as a page generator
# code taken on https://github.com/Toto-Azero/Wikipedia
def creation_log(start, end):
"""
query the recent change for creation between the timestamp in parameter
arguments:
start -- time stamp of first creation (format YYYY-MM-DDTHH:MM:SSZ)"
end -- time stamp of last creation (format YYYY-MM-DDTHH:MM:SSZ)"
result:
list of the creation events occuring between the timestamp
"""
params = {
'action': 'query',
'list': 'recentchanges',
'rctype': 'new',
'rctag': 'contenttranslation',
'rcprop': 'title|comment|timestamp|user',
'rcstart': end,
'rcend': start,
'rclimit': 'max',
}
all_done = False
while not all_done:
if all_done:
break
datas = api_query(params)
if not datas:
break
data = datas['query']['recentchanges']
for d in data:
yield d
if 'query-continue' in datas:
if 'recentchanges' in datas['query-continue']:
params['rccontinue'] = datas['query-continue']['recentchanges']['rccontinue']
else:
all_done = True
for i, page in enumerate(creation_log(start, end), 1):
xxx