def _api_setup(self): if not self.api_setup: # do not call _api_setup twice cookie_filename = os.path.join(self.metadir.location, 'cookies') self.api_url = self.metadir.config.get('remote', 'api_url') self.api = simplemediawiki.MediaWiki(self.api_url, cookie_file=cookie_filename) self.api_setup = True
def setUp(self): self.user_agent = simplemediawiki.build_user_agent( 'python-simplemediawiki test suite', simplemediawiki.__version__, 'https://github.com/ianweller/python-simplemediawiki') self.mw = simplemediawiki.MediaWiki('https://httpbin.org/', user_agent=self.user_agent) self.data = None
def __init__(self, api_url, http_user=None, http_pass="", wiki_user=None, wiki_pass=""): self.mw = simplemediawiki.MediaWiki(api_url, http_user=http_user, http_password=http_pass) # login if necessary if wiki_user is not None: print("Logging in as %s..." % wiki_user) if not self.mw.login(wiki_user, wiki_pass): raise RuntimeError( "Mediawiki login failed. Wrong credentials?") # version check try: generator = "".join( self._query({'meta': 'siteinfo'}, ['general', 'generator'])) version = [ int(x) for x in re.search(r'[0-9.]+', generator).group(0).split(".") ] # list of [ 1, 19, 1 ] or similar if version[0] == 1 and version[1] < 13: raise RuntimeError( "Mediawiki version is too old. Yamdwe requires 1.13 or newer. This install is %s" % generator) print("%s meets version requirements." % generator) except IndexError: raise RuntimeError( "Failed to read Mediawiki siteinfo/generator. Is version older than 1.8? Yamdwe requires 1.13 or greater." )
def __init__(self, api_url, http_user=None, http_pass="", wiki_user=None, wiki_pass="", wiki_domain=None, verbose=False): self.verbose = verbose if wiki_domain: self.mw = simplemediawiki.MediaWiki(api_url, http_user=http_user, http_password=http_pass, domain=wiki_domain) else: self.mw = simplemediawiki.MediaWiki(api_url, http_user=http_user, http_password=http_pass) # login if necessary if wiki_user is not None: print("Logging in as %s..." % wiki_user) if not self.mw.login(wiki_user, wiki_pass): raise RuntimeError( "Mediawiki login failed. Wrong credentials?") # version check try: self.need_rawcontinue = False generator = "".join( self._query({'meta': 'siteinfo'}, ['general', 'generator'])) version = [ int(x) for x in re.search(r'[0-9.]+', generator).group(0).split(".") ] # list of [ 1, 19, 1 ] or similar if version[0] == 1 and version[1] < 13: raise RuntimeError( "Mediawiki version is too old. Yamdwe requires 1.13 or newer. This install is %s" % generator) # check if the version is too old for the 'rawcontinue' parameter # see https://www.mediawiki.org/wiki/API:Query#Backwards_compatibility_of_continue self.need_rawcontinue = version[0] > 1 or (version[0] == 1 and version[1] >= 24) print("%s meets version requirements." % generator) except IndexError: raise RuntimeError( "Failed to read Mediawiki siteinfo/generator. Is version older than 1.8? Yamdwe requires 1.13 or greater." )
def test_persistent_cookiejar(self): cookiejar = cookielib.CookieJar() mw1 = simplemediawiki.MediaWiki('https://httpbin.org/', cookiejar=cookiejar, user_agent=self.user_agent) mw1._fetch_http('https://httpbin.org/cookies/set', {'unicode': UNICODE_TEST}, force_get=True) cookiejar = mw1._cj mw2 = simplemediawiki.MediaWiki('https://httpbin.org/', cookiejar=cookiejar, user_agent=self.user_agent) data = json.loads( mw2._fetch_http('https://httpbin.org/cookies', {}, force_get=True)) print(data) self.assertTrue('unicode' in data['cookies']) self.assertEqual(data['cookies']['unicode'], UNICODE_TEST)
def mw_call(args, api=config.centralnotice_mw_api): import simplemediawiki wiki = simplemediawiki.MediaWiki( api, user_agent='bot: fr-anal' ) result = wiki.call( args ) if 'error' in result: raise RuntimeError(json.dumps(result, indent=4).replace('\\n', '\n')) val = result[ args['action'] ] if 'list' in args: val = val[ args['list'] ] return val
user_agent = { 'application_name': 'ThanksLog', 'version': '0.0.2alpha', 'url': 'https://github.com/valepert/thankslog' } project = 'it' api = 'w/api.php' url = 'http://%s.wikipedia.org/%s' % (project, api) thanksdir = 'thanks_%s' % project lelimit = 500 n_times = 3 sleep_time = 60 wiki = simplemediawiki.MediaWiki(url) simplemediawiki.build_user_agent(**user_agent) #lastcontinue = u'' query = { 'action': 'query', 'list': 'logevents', 'letype': 'thanks', 'lelimit': lelimit, 'continue': u'' } # u'-||' } #, 'lecontinue': lastcontinue} out = wiki.call(query) results = out['query']['logevents'] dump_results(results, thanksdir) cont = out['continue']
#!/usr/bin/env python import warnings import simplemediawiki wiki = simplemediawiki.MediaWiki('http://gocwiki.geneontology.org/api.php?') import json def load_json(path): json_file = open(path, "r") json_string = json_file.read() json_file.close() return json.loads(json_string) example_call = { u'query': { u'normalized': [{ u'from': u'Api_test', u'to': u'Api test' }], u'pages': { u'7820': { u'contentmodel': u'wikitext', u'counter': 1, u'edittoken': u'ee8c94746e781cf718c3c5b6ac039304+\\', u'lastrevid': 57161, u'length': 459, u'new': u'', u'ns': 0, u'pageid': 7820,
def setUp(self): self.user_agent = simplemediawiki.build_user_agent( 'python-simplemediawiki test suite', simplemediawiki.__version__, 'https://github.com/ilianaw/python-simplemediawiki') self.mw = simplemediawiki.MediaWiki('FIXME/api.php', user_agent=self.user_agent)
def test_normalize_api_url_indexdotphp(self): mw = simplemediawiki.MediaWiki('FIXME/index.php', user_agent=self.user_agent) self.assertTrue(mw.normalize_api_url() is not None) self.assertTrue('api.php' in mw._api_url)