<s> """<STR_LIT:U+0020>""" <EOL> __version__ = '<STR_LIT>' <EOL> import re <EOL> import wikipedia as pywikibot <EOL> import query <EOL> class BaseRevertBot ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , site , comment = None ) : <EOL> self . site = site <EOL> self . comment = comment <EOL> def get_contributions ( self , max = - <NUM_LIT:1> , ns = None ) : <EOL> predata = { <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT:list>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . site . username ( ) , <EOL> } <EOL> if ns : <EOL> predata [ '<STR_LIT>' ] = ns <EOL> if max < <NUM_LIT> and max != - <NUM_LIT:1> : <EOL> predata [ '<STR_LIT>' ] = str ( max ) <EOL> count = <NUM_LIT:0> <EOL> iterator = iter ( xrange ( <NUM_LIT:0> ) ) <EOL> never_continue = False <EOL> while count != max or never_continue : <EOL> try : <EOL> item = iterator . next ( ) <EOL> except StopIteration : <EOL> self . log ( u'<STR_LIT>' ) <EOL> data = query . GetData ( predata , self . site ) <EOL> if '<STR_LIT:error>' in data : <EOL> raise RuntimeError ( data [ '<STR_LIT:error>' ] ) <EOL> if '<STR_LIT>' in data : <EOL> predata . update ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> never_continue = True <EOL> iterator = iter ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> count += <NUM_LIT:1> <EOL> yield item <EOL> def revert_contribs ( self , callback = None ) : <EOL> self . site . forceLogin ( ) <EOL> if callback is None : <EOL> callback = self . callback <EOL> contribs = self . get_contributions ( ) <EOL> for item in contribs : <EOL> try : <EOL> if callback ( item ) : <EOL> result = self . revert ( item ) <EOL> if result : <EOL> self . log ( u'<STR_LIT>' % ( item [ '<STR_LIT:title>' ] , result ) ) <EOL> else : <EOL> self . log ( u'<STR_LIT>' % item [ '<STR_LIT:title>' ] ) <EOL> else : <EOL> self . log ( u'<STR_LIT>' % item [ '<STR_LIT:title>' ] ) <EOL> except StopIteration : <EOL> return <EOL> def callback ( self , item ) : <EOL> return '<STR_LIT>' in item <EOL> def revert ( self , item ) : <EOL> predata = { <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : item [ '<STR_LIT:title>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : item [ '<STR_LIT>' ] , <EOL> } <EOL> data = query . GetData ( predata , self . site ) <EOL> if '<STR_LIT:error>' in data : <EOL> raise RuntimeError ( data [ '<STR_LIT:error>' ] ) <EOL> pages = data [ '<STR_LIT>' ] . get ( '<STR_LIT>' , ( ) ) <EOL> if not pages : <EOL> return False <EOL> page = pages . itervalues ( ) . next ( ) <EOL> if len ( page . get ( '<STR_LIT>' , ( ) ) ) != <NUM_LIT:2> : <EOL> return False <EOL> rev = page [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> comment = u'<STR_LIT>' % ( rev [ '<STR_LIT>' ] , <EOL> rev [ '<STR_LIT:user>' ] , <EOL> rev [ '<STR_LIT>' ] ) <EOL> if self . comment : <EOL> comment += '<STR_LIT>' + self . comment <EOL> page = pywikibot . Page ( self . site , item [ '<STR_LIT:title>' ] ) <EOL> pywikibot . output ( u"<STR_LIT>" <EOL> % page . title ( asLink = True , forceInterwiki = True , <EOL> textlink = True ) ) <EOL> old = page . get ( ) <EOL> new = rev [ '<STR_LIT:*>' ] <EOL> pywikibot . showDiff ( old , new ) <EOL> page . put ( new , comment ) <EOL> return comment <EOL> def log ( self , msg ) : <EOL> pywikibot . output ( msg ) <EOL> class myRevertBot ( BaseRevertBot ) : <EOL> def callback ( self , item ) : <EOL> if '<STR_LIT>' in item : <EOL> page = pywikibot . Page ( self . site , item [ '<STR_LIT:title>' ] ) <EOL> text = page . get ( ) <EOL> pattern = re . compile ( u'<STR_LIT>' , re . UNICODE ) <EOL> return pattern . search ( text ) >= <NUM_LIT:0> <EOL> return False <EOL> def main ( ) : <EOL> for arg in pywikibot . handleArgs ( ) : <EOL> continue <EOL> bot = myRevertBot ( site = pywikibot . getSite ( ) ) <EOL> bot . revert_contribs ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> try : <EOL> main ( ) <EOL> finally : <EOL> pywikibot . stopme ( ) </s>
