def api_mock(req_mock, module_mocker, title_context): # create the API object api_url = "http://wiki-scripts.localhost/api.php" index_url = "http://wiki-scripts.localhost/index.php" session = API.make_session() api = API(api_url, index_url, session) # register a callback function for dynamic responses # https://requests-mock.readthedocs.io/en/latest/response.html#dynamic-response def api_callback(request, context): params = request.qs if params.get("format") == ["json" ] and params.get("action") == ["query"]: if params.get("meta") == ["siteinfo"] and params.get("siprop") == [ "general" ]: # only props which may be relevant for tests are in the mocked response general = { "mainpage": "Main page", "base": "http://wiki-scripts.localhost/index.php/Main_page", "sitename": "wiki-scripts tests", "articlepath": "/index.php/$1", "scriptpath": "", "script": "/index.php", "server": "http://wiki-scripts.localhost", "servername": "wiki-scripts.localhost", } return {"batchcomplete": "", "query": {"general": general}} # defaults context.status_code = 404 context.reason = "Missing mock for the query parameters '{}'".format( request.qs) req_mock.get(api_url, json=api_callback) req_mock.get(index_url, status_code=404, reason="Missing mocks for the index.php entry point") # mock the title context class mContext = module_mocker.patch( "ws.parser_helpers.title.Context", module_mocker.create_autospec(title_context)) # override the from_api method to always return the fixture mContext.from_api = lambda api: title_context return api
#! /usr/bin/env python3 import os.path import datetime from ws.client import API api_url = "https://wiki.archlinux.org/api.php" index_url = "https://wiki.arclinux.org/index.php" cookie_path = os.path.expanduser("~/.cache/ArchWiki.cookie") session = API.make_session(ssl_verify=True, cookie_file=cookie_path) api = API(api_url, index_url, session) # get list of all users, who: # - are not bots # - made at least one edit # - have been active in the last 30 days # - are not blocked # the list can be sorted by: # - editcount # - recenteditcount # - avgeditsperday (needs to be calculated) users = list( api.list(list="allusers", aulimit="max", auprop="blockinfo|editcount|registration", auexcludegroup="bot", auwitheditsonly="", auactiveusers=""))
#! /usr/bin/env python3 import os.path from ws.client import API api_urls = [ "https://wiki.archlinux.org/api.php", "https://wiki.archlinux.de/api.php", ] index_url = "https://wiki.arclinux.org/index.php" session = API.make_session() for api_url in api_urls: api = API(api_url, index_url, session) prefixes = set() for ns in api.site.namespaces: if ns < 0: continue for page in api.generator(generator="allpages", gaplimit="max", gapfilterredir="nonredirects", gapnamespace=ns, prop="iwlinks", iwprop="url", iwlimit="max"): for link in page.get("iwlinks", []): prefixes.add(link["prefix"]) print("interwiki prefixes used on {}:".format(api.get_hostname())) print(sorted(prefixes))