def query_api(data): predata = { 'action': 'query', 'prop': 'revisions', } predata = query.CombineParams(predata, data) return query.GetData(predata)
def query_results(self, **data): """Iterate results from API action=query, using data as parameters.""" querydata = {'action': 'query', 'maxlag': str(pywikibot.config.maxlag)} querydata = query.CombineParams(querydata, data) if "action" not in querydata or querydata['action'] != 'query': raise ValueError( "query_results: 'action' set to value other than 'query'") waited = 0 while True: try: result = query.GetData(querydata, self.site) #if data.startswith(u"unknown_action"): # e = {'code': data[:14], 'info': data[16:]} # raise APIError(e) except pywikibot.ServerError: pywikibot.output(u"Wikimedia Server Error; retrying...") time.sleep(5) continue except ValueError: # if the result isn't valid JSON, there must be a server # problem. Wait a few seconds and try again # WARNING: if the server is down, this could # cause an infinite loop pywikibot.output(u"Invalid API response received; retrying...") time.sleep(5) continue if type(result) is dict and "error" in result: if result['error']['code'] == "maxlag": print "Pausing due to server lag.\r", time.sleep(5) waited += 5 if waited % 30 == 0: pywikibot.output( u"(Waited %i seconds due to server lag.)" % waited) continue else: raise APIError(result['error']) waited = 0 if type(result) is list: # query returned no results return assert type(result) is dict, ( "Unexpected result of type '%s' received." % type(result)) if "query" not in result: # query returned no results return yield result['query'] if 'query-continue' in result: assert len(result['query-continue'].keys()) == 1, ( "More than one query-continue key returned: %s" % result['query-continue'].keys()) query_type = result['query-continue'].keys()[0] assert (query_type in querydata.keys() or query_type in querydata.values()), \ "Site returned unknown query-continue type '%s'" \ % query_type querydata.update(result['query-continue'][query_type]) else: return
def query_old_api(data): predata = { 'what': 'revisions', 'rvlimit': '1', } predata = query.CombineParams(predata, data) return query.GetData(predata, useAPI = False)