Skip to content

Instantly share code, notes, and snippets.

@legoktm
Created September 3, 2013 01:29
Show Gist options
  • Save legoktm/6418756 to your computer and use it in GitHub Desktop.
Save legoktm/6418756 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Copyright (C) 2013 Legoktm
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
"""
import pywikibot
from pywikibot.data import api
site = pywikibot.Site('wikidata', 'wikidata')
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': 'Legobot',
'rclimit': 'max',
'rcprop': 'title|comment'}
req = api.Request(site=site, **params)
data =req.submit()
for edit in data['query']['recentchanges']:
if 'P436' in edit['comment']: # Check that we're rolling back only the bad edits
print 'rolling back'
t = token(edit['title'])
rback(edit['title'], 'Legobot', t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment