Created
June 2, 2013 06:44
-
-
Save legoktm/5692839 to your computer and use it in GitHub Desktop.
An example mass-rollback script used to fix errors caused by Hazard-Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Released into the public domain by Legoktm, 2013 | |
""" | |
import pywikibot | |
from pywikibot import config | |
config.usernames['wikipedia']['en'] = 'Legoktm' | |
from pywikibot.data import api | |
site = pywikibot.Site('en', 'wikipedia') | |
if not site.logged_in(): | |
site.login() | |
def token(page): | |
params={'action':'query','prop':'revisions','rvtoken':'rollback','titles':page} | |
r=api.Request(site=site, **params) | |
data = r.submit() | |
print 'fetched token' | |
return data['query']['pages'].values()[0]['revisions'][0]['rollbacktoken'] | |
def rback(page, user, token): | |
params = {'action': 'rollback', | |
'title': page, | |
'user': user, | |
'token': token, | |
} | |
req = api.Request(site=site, **params) | |
try: | |
data =req.submit() | |
print data | |
except pywikibot.data.api.APIError, e: | |
if e.code == 'badtoken': | |
print 'already reverted' | |
params = {'action': 'query', | |
'list': 'recentchanges', | |
'rcuser': 'Hazard-Bot', | |
'rclimit': 'max', | |
'rcprop': 'title|comment', | |
'rcnamespace': 0} | |
req = api.Request(site=site, **params) | |
data =req.submit() | |
for edit in data['query']['recentchanges']: | |
if 'WikiProject Lists' in edit['comment']: | |
print 'rolling back' | |
print edit['comment'] | |
t = token(edit['title']) | |
rback(edit['title'], 'Hazard-Bot', t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment