def test_no_user(self): """Test Request object when not a user.""" self.site._userinfo = {} with self.subTest(userinfo=self.site._userinfo): with self.assertRaisesRegex( Error, 'API write action attempted without user'): Request(site=self.site, parameters={'action': 'edit'}) self.site._userinfo = {'name': '1.2.3.4', 'groups': [], 'anon': ''} with self.subTest(userinfo=self.site._userinfo): with self.assertRaisesRegex(Error, " as IP '1.2.3.4'"): Request(site=self.site, parameters={'action': 'edit'})
def test_normal(self): """Test Request object when username is correct.""" self.site._userinfo = {'name': 'myusername', 'groups': [], 'id': '1'} self.site._username = '******' Request(site=self.site, parameters={'action': 'edit'}) self.assertEqual(self.site.user(), self.site.username()) self.assertTrue(self.site.logged_in())
def test_unexpected_user(self): """Test Request object when username is not correct.""" site = self.get_site() site._userinfo = {'name': 'other_username', 'groups': []} site._username[0] = 'myusername' Request(site=site, action='edit')
def RecentChanges(limit): limitString = limit time_format = "%Y-%m-%dT%H:%M:%SZ" limit = datetime.datetime.fromtimestamp( time.mktime(time.strptime(limitString, time_format))) current = datetime.datetime.now() list = set() params = { 'action': 'query', 'list': 'recentchanges', 'rcprop': 'timestamp|title', 'rclimit': '100', 'rcnamespace': 0 } while current > limit: textDate = current.strftime(time_format) params['rcstart'] = textDate req = Request(**params) qr = req.submit() try: current = datetime.datetime.fromtimestamp( time.mktime( time.strptime( qr['query-continue']['recentchanges']['rcstart'], time_format))) except KeyError: for elem in qr['query']['recentchanges']: list.add(elem['title']) break else: for elem in qr['query']['recentchanges']: list.add(elem['title']) return list
def insert(page, level=2, space=True, zero_padded=False, minorEdit=True, autolink=True): header = Request( site=page.site, action='expandtemplates', text='{{#timel:%s xg}}' % ('d' if zero_padded else 'j')).submit()['expandtemplates']['*'] text = page.get(force=True) if pywikibot.textlib.does_text_contain_section(text, header): pywikibot.output(u'\03{{lightyellow}}{} already contains' u'the header for "{}"'.format(page, header)) return if isinstance(level, int): level = '=' * level if isinstance(space, bool): space = (' ' if space is True else '') fmt = u'\n\n{level}{space}{header}{space}{level}' page.text += fmt.format(header=header, level=level, space=space) pywikibot.showDiff(text, page.text) summary = u'[['+page.site.namespace(4)+':Bot|Bot]]: ' \ u'inserimento della sezione giornaliera' if autolink: summary = u'/* {} */ {}'.format(header, summary) page.save(comment=summary, minor=minorEdit, botflag=True)
def test_normal(self): """Test Request object when username is correct.""" site = self.get_site() site._userinfo = {'name': 'myusername', 'groups': []} site._username[0] = 'myusername' Request(site=site, action='edit')
def test_upload_object(self): """Test Request object prepared to upload.""" req = Request(site=self.get_site(), action="upload", file='MP_sounds.png', mime=True, filename=os.path.join(_images_dir, 'MP_sounds.png')) self.assertEqual(req.mime, True)
def submit(self, parameters): """ submit the request with the given parameters Args: parameters(list): the parameters to use for the SMW API request Returns: dict: the submit result""" request = Request(site=self.site, parameters=parameters) return request.submit()
def test_unexpected_user(self): """Test Request object when username is not correct.""" site = self.get_site() site._userinfo = {'name': 'other_username', 'groups': []} site._username[0] = 'myusername' # Ignore warning: API write action by unexpected username commenced. with patch('pywikibot.warning'): Request(site=site, parameters={'action': 'edit'})
def pageCounter(language): #returns number of entries for a language params = { 'action': 'expandtemplates', 'text': '{{PAGESINCAT:%s (indeks)|R}}' % language, } req = Request(**params) qr = req.submit() print(qr['expandtemplates']['*'])
def change_message_translation(msg_title: str, content: str) -> list: """ @param: msgTitle (str): Title of the message we want to change @param: content (str): New text of the translation unit TODO should we do it this way or probably we could just use the pywikibot.Page class and set the text?! """ global global_site logger.info(F"Change_message_translation: {msg_title}: {content}") requeste_for_token: list = Request(site=global_site, action="query", meta="tokens").submit() my_token: str = requeste_for_token['query']['tokens']['csrftoken'] result: list = Request(site=global_site, action="edit", title=msg_title, token=my_token, text=content).submit() return result
def test_upload_object(self): """Test Request object prepared to upload.""" # fake write test needs the config username site = self.get_site() site._username[0] = 'myusername' site._userinfo = {'name': 'myusername', 'groups': []} req = Request(site=site, action="upload", file='MP_sounds.png', mime=True, filename=os.path.join(_images_dir, 'MP_sounds.png')) self.assertEqual(req.mime, True)
def convert_title(title): req = Request(site=site, parameters={ 'action': 'query', 'titles': title, "redirects": 1, "converttitles": 1 }) data = req.submit() new_title = list(data['query']['pages'].values())[0]['title'] return new_title
def converttitle(title): r = Request(site=site, parameters={ 'action': 'query', 'titles': title, 'redirects': 1, 'converttitles': 1 }) data = r.submit() return list(data['query']['pages'].values())[0]['title']
def test_upload_object(self): """Test Request object prepared to upload.""" # fake write test needs the config username site = self.get_site() site._username[0] = 'myusername' site._userinfo = {'name': 'myusername', 'groups': []} parameters = {'action': 'upload', 'file': 'MP_sounds.png', 'filename': join_images_path('MP_sounds.png')} req = Request(site=site, mime=True, parameters=parameters) self.assertEqual(req.mime, True)
def submit(self, parameters): """ submit the request with the given parameters Args: parameters(list): the parameters to use for the SMW API request Returns: dict: the submit result""" if not "Request" in sys.modules: from pywikibot.data.api import Request request = Request(site=self.site, parameters=parameters) return request.submit()
def is_user_active(site, username): """Tests if a given user is active.""" contributions = Request( site=site, action="query", list="usercontribs", ucuser=username, uclimit=1, ucprop="timestamp").submit()[u"query"][u"usercontribs"] return bool(contributions) and contributions[0][u"timestamp"].partition( "T")[0] > THREE_MONTHS_AGO
def converttitle(site, title): r = Request(site=site, parameters={ 'action': 'query', 'titles': title, 'redirects': 1, 'converttitles': 1 }) data = r.submit() page = list(data['query']['pages'].values())[0] if 'missing' in page: return None return page['title'].replace(' ', '_')
def main(self): badPage = pywikibot.Page(self.site, 'MediaWiki:Bad image list') text = badPage.text logger.info('cache pages') for page in badPage.linkedPages(): self.cachedPages[page.title()] = page logger.info('cache files') data = Request(site=self.site, parameters={ 'action': 'query', 'format': 'json', 'formatversion': '2', 'prop': 'imageinfo', 'titles': 'MediaWiki:Bad image list', 'generator': 'links', 'gplnamespace': '6', 'gpllimit': 'max' }).submit() for page in data['query']['pages']: if page['imagerepository'] != '': self.cachedFiles[page['title']] = True else: self.cachedFiles[page['title']] = False logger.info('get_en_list') en_list = self.get_en_list() logger.info('process_text') new_text = self.process_text(text, en_list) logger.info('done') if text == new_text: logger.info('nothing changed') return if self.CONFIRM: pywikibot.showDiff(text, new_text) save = input('Save?') elif self.DRY_RUN: save = 'no' else: save = 'yes' if save.lower() in ['y', 'yes']: badPage.text = new_text badPage.save(summary=self.cfg['summary'], minor=False, botflag=False) else: with open('temp.txt', 'w', encoding='utf8') as f: f.write(new_text)
def flagLastRev(site, revid, comment=''): token = site.getToken(sysop=False) params = { 'site': site, 'action': 'review', 'revid': revid, 'token': token, 'flag_accuracy': 1, 'comment': comment, } req = Request(**params) query = req.submit()
def converttitle(title): r = Request(site=site, parameters={ 'action': 'query', 'titles': title, "redirects": 1, "converttitles": 1 }) data = r.submit() newtitle = list(data['query']['pages'].values())[0]['title'] checkAfdTemplate(newtitle) return newtitle
def test_unexpected_user(self): """Test Request object when username is not correct.""" self.site._userinfo = { 'name': 'other_username', 'groups': [], 'id': '1' } self.site._username = '******' # Ignore warning: API write action by unexpected username commenced. with patch('pywikibot.warning'): Request(site=self.site, parameters={'action': 'edit'}) self.assertNotEqual(self.site.user(), self.site.username()) self.assertNotEqual(self.site.userinfo['name'], self.site.username()) self.assertFalse(self.site.logged_in())
def transclusions(pages): from pywikibot.data.api import Request parameters = { 'action': 'query', 'generator': 'templates', 'prop': 'revisions', 'titles': '|'.join([p.title for p in pages]) } # Use POST in case the list of titles would cause a very long URL for GET request = Request(_site, use_get=False, parameters=parameters) # TODO Error handling raw_query_data = request.submit() return _title_and_revision_dict(raw_query_data)
def get_orlist(site=DEFAULT_SITE, namespace="0|6|10|14|100|828", redirects="nonredirects"): """Get list of oldreviewed pages.""" request = Request(site=site, action="query", list="oldreviewedpages", ornamespace=namespace, orfilterredir=redirects, orlimit="5000") result = [] while True: answer = request.submit() result += [page["title"] for page in answer["query"]["oldreviewedpages"]] if "query-continue" in answer: request["orstart"] = answer["query-continue"]["oldreviewedpages"]["orstart"] else: break return result
def converttitle(title): oldtitle = title r = Request(site=site, parameters={ 'action': 'query', 'titles': title, 'redirects': 1, 'converttitles': 1 }) data = r.submit() title = list(data['query']['pages'].values())[0]['title'] mode = [] if 'redirects' in data['query']: # 重定向 mode.append('redirects') if 'converted' in data['query']: # 繁簡轉換 mode.append('converted') if 'normalized' in data['query']: # 命名空間 mode.append('normalized') if 'redirects' not in mode: page = pywikibot.Page(site, title) if not page.exists(): mode.append('vfd_on_source') if page.exists() and ( page.content_model != 'wikitext' or page.namespace().id == 8 or re.search( r'{{\s*([vaictumr]fd|Copyvio)', page.text, flags=re.I)): mode.append('vfd_on_source') else: page = pywikibot.Page(site, oldtitle) if page.exists() and ( page.content_model != 'wikitext' or page.namespace().id == 8 or re.search( r'{{\s*([vaictumr]fd|Copyvio)', page.text, flags=re.I)): mode.append('vfd_on_source') page = pywikibot.Page(site, title) if page.exists() and ( page.content_model != 'wikitext' or page.namespace().id == 8 or re.search( r'{{\s*([vaictumr]fd|Copyvio)', page.text, flags=re.I)): mode.append('vfd_on_target') if 'vfd_on_source' not in mode and 'vfd_on_target' not in mode: mode.append('no_vfd') return {'title': title, 'mode': mode}
def get_summary(self, is_archive=False): summary = "[[Wikidata:Bots|Bot]]: %s %i request%s %s %s" % ( "Archived" if is_archive else "Archiving", self.archive_count, "" if self.archive_count == 1 else "s", "from" if is_archive else "to", self.rfd_page.title(asLink=True) if is_archive else self.archive_page.title(asLink=True)) if is_archive: return summary version = self.rfd_page.getVersionHistory(total=1)[0] user = version[2] params = { "action": "query", "list": "users", "ususers": user, "usprop": "groups" } user_groups = Request(**params).submit()["query"]["users"][0]["groups"] summary += " (last edit at %s by [[User:%s|]]%s%s" % ( version[1], user, " (administrator)" if (not ("bot" in user_groups) and ("sysop" in user_groups)) else "", ": '%s'" % version[3] if version[3] else "") return summary
def active_and_future_campaigns(): from pywikibot.data.api import Request from pywikibot import Timestamp parameters = { 'action': 'query', 'list': 'centralnoticeactivecampaigns', 'cnacincludefuture': '' } request = Request(_site, parameters=parameters) # TODO Error handling raw_query_data = request.submit() raw_campaigns = ( raw_query_data['query']['centralnoticeactivecampaigns']['campaigns']) # Convert start and end to datetime objects for c in raw_campaigns: c['start'] = Timestamp.fromtimestampformat(c['start']) c['end'] = Timestamp.fromtimestampformat(c['end']) return raw_campaigns
def get_ds_alert_hits(start_date: datetime.datetime, end_date: datetime.datetime) -> Iterator[DsAlert]: # url = "https://en.wikipedia.org/w/api.php" params = { "action": "query", "list": "abuselog", "format": "json", "formatversion": 2, "aflstart": start_date.isoformat(), "aflend": end_date.isoformat(), "afldir": "newer", "aflfilter": 602, "afllimit": "max", "aflprop": "user|title|result|timestamp|details|revid", "continue": "", } for i in range(100): logger.debug(i) # res = session.get(url, params=params) # res.raise_for_status() # raw_data = res.json() req = Request(site=site, parameters=params, use_get=True) raw_data = req.submit() # breakpoint() for hit in raw_data["query"]["abuselog"]: if hit["result"] == "tag": for alert in parse_alert_data(hit): yield alert if raw_data.get("continue"): logger.debug(f"Continue: {raw_data['continue']}") params.update(raw_data["continue"]) else: break else: # flask.abort(400) logger.warning("Too many API queries!")
def get_pages_categories(pagelist, site=DEFAULT_SITE, limit=500): """ For every page from the list get list of categories and return {page: [categories]} dictionary. """ result = dict.fromkeys(pagelist, []) kwargs = {"action": "query", "prop": "categories", "cllimit": "5000"} for idx in range(0, len(pagelist), limit): kwargs["titles"] = "|".join(pagelist[idx:idx + limit]) request = Request(site=site, **kwargs) while True: answer = request.submit() # Wikipedia API can return page list in non-canonical form! # At least when there are two possible canonical forms for one namespace # (for instance, "Участник" – "Участница" in Russian Wikipedia). # This query will normalize them and we need to handle it. denormalize = {} if "normalized" in answer["query"]: for fix in answer["query"]["normalized"]: denormalize[fix["to"]] = fix["from"] for value in answer["query"]["pages"].values(): title = value["title"] if title in denormalize: title = denormalize[title] if "categories" in value: cats = [cat["title"] for cat in value["categories"]] result[title] = result[title] + cats if "query-continue" in answer: request["clcontinue"] = answer["query-continue"]["categories"][ "clcontinue"] continue break return result
#!/usr/bin/python # -*- coding: utf-8 -*- # Distributed under the terms of MIT License (MIT) import pywikibot import time from pywikibot.data.api import Request import re site = pywikibot.Site('fa', fam='wikipedia') print "Fetching admins list" data = Request(site=site, action="query", list="allusers", augroup="sysop", aulimit=500).submit() adminsac = [] adminbots = ["Dexbot"] adminsdiac = {} for admin in data["query"]["allusers"]: admin = admin["name"] if admin in adminbots: continue acaction = [] dcaction = [] actions = "block, protect, rights, delete, upload, import, renameuser".split( ", ") for adminaction in actions: data1 = Request(site=site, action="query", list="logevents", leuser=admin,