Skip to content

Commit

Permalink
Merge "standardize spamremove.py"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Jun 18, 2017
2 parents 36519ae + a360417 commit 9b288d4
Showing 1 changed file with 90 additions and 67 deletions.
157 changes: 90 additions & 67 deletions scripts/spamremove.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,88 +18,80 @@
-always Do not ask, but remove the lines automatically. Be very
careful in using this option!
-namespace: Filters the search to a given namespace. If this is specified
multiple times it will search all given namespaces
-protocol: The protocol prefix (default: "http")
-summary: A string to be used instead of the default summary
In addition, these arguments can be used to restrict changes to some pages:
&params;
"""
#
# (C) Pywikibot team, 2007-2016
# (C) Pywikibot team, 2007-2017
#
# Distributed under the terms of the MIT license.
#
from __future__ import absolute_import, unicode_literals

__version__ = '$Id$'

#

import pywikibot

from pywikibot import i18n

from pywikibot import pagegenerators
from pywikibot.bot import (
SingleSiteBot, ExistingPageBot, NoRedirectPageBot, AutomaticTWSummaryBot)
from pywikibot.editor import TextEditor
from pywikibot.tools.formatter import color_format

docuReplacements = {
'&params;': pagegenerators.parameterHelp
}

def main(*args):
"""
Process command line arguments and perform task.

If args is an empty list, sys.argv is used.
class SpamRemoveBot(SingleSiteBot, ExistingPageBot, NoRedirectPageBot,
AutomaticTWSummaryBot):

@param args: command line arguments
@type args: list of unicode
"""
always = False
namespaces = []
spamSite = ''
protocol = 'http'
summary = None
for arg in pywikibot.handle_args(args):
if arg == "-always":
always = True
elif arg.startswith('-namespace:'):
try:
namespaces.append(int(arg[len('-namespace:'):]))
except ValueError:
namespaces.append(arg[len('-namespace:'):])
elif arg.startswith('-protocol:'):
protocol = arg.partition(':')[2]
elif arg.startswith('-summary:'):
summary = arg.partition(':')[2]
else:
spamSite = arg
"""Bot to remove links that are being or have been spammed.
if not spamSite:
pywikibot.bot.suggest_help(missing_parameters=['spam site'])
return False
@param generator: page generator with preloaded pages.
@type generator: generator
@param spam_external_url: an external url
@type spam_external_url: str
@keyword summary: summary message when given. Otherwise the default
summary will be used
@type summary: str
@keyword always: Don't ask for text replacements
@type always: bool
"""

mysite = pywikibot.Site()
pages = mysite.exturlusage(
spamSite, protocol=protocol, namespaces=namespaces, content=True
)

if not summary:
summary = i18n.twtranslate(
mysite,
'spamremove-remove',
{'url': spamSite}
)
for i, p in enumerate(pages, 1):
text = p.text
if spamSite not in text:
continue
# Show the title of the page we're working on.
# Highlight the title in purple.
pywikibot.output(color_format(
'\n\n>>> {lightpurple}{0}{default} <<<', p.title()))
summary_key = 'spamremove-remove'

def __init__(self, generator, spam_external_url, **kwargs):
"""Constructor."""
self.availableOptions.update({
'summary': None,
})
super(SpamRemoveBot, self).__init__(**kwargs)
self.generator = generator
self.spam_external_url = spam_external_url
self.changed_pages = 0

@property
def summary_parameters(self):
"""A dictionary of all parameters for i18n."""
return {'url': self.spam_external_url}

def treat_page(self):
"""Process a single page."""
text = self.current_page.text
if self.spam_external_url not in text:
return
lines = text.split('\n')
newpage = []
lastok = ""
for line in lines:
if spamSite in line:
if self.spam_external_url in line:
if lastok:
pywikibot.output(lastok)
pywikibot.output(color_format('{lightred}{0}{default}', line))
Expand All @@ -110,31 +102,62 @@ def main(*args):
if lastok is None:
pywikibot.output(line)
lastok = line
if always:
if self.getOption('always'):
answer = "y"
else:
answer = pywikibot.input_choice(
u'\nDelete the red lines?',
[('yes', 'y'), ('no', 'n'), ('edit', 'e')],
'n', automatic_quit=False)
if answer == "n":
continue
return
elif answer == "e":
editor = TextEditor()
newtext = editor.edit(text, highlight=spamSite,
jumpIndex=text.find(spamSite))
newtext = editor.edit(text, highlight=self.spam_external_url,
jumpIndex=text.find(self.spam_external_url))
else:
newtext = "\n".join(newpage)
if newtext != text:
p.text = newtext
p.save(summary)
else:
if "i" not in locals():
pywikibot.output('No page found.')
elif i == 1:
pywikibot.output('1 pages done.')
self.put_current(newtext, summary=self.getOption('summary'))


def main(*args):
"""
Process command line arguments and perform task.
If args is an empty list, sys.argv is used.
@param args: command line arguments
@type args: list of unicode
"""
spam_external_url = None
protocol = 'http'
options = {}
local_args = pywikibot.handle_args(args)
genFactory = pagegenerators.GeneratorFactory()
for arg in local_args:
if arg == '-always':
options['always'] = True
elif arg.startswith('-protocol:'):
protocol = arg.partition(':')[2]
elif arg.startswith('-summary:'):
options['summary'] = arg.partition(':')[2]
elif genFactory.handleArg(arg):
continue
else:
pywikibot.output('%d pages done.' % i)
spam_external_url = arg

if not spam_external_url:
pywikibot.bot.suggest_help(missing_parameters=['spam site'])
return False

link_search = pagegenerators.LinksearchPageGenerator(spam_external_url,
protocol=protocol)
generator = genFactory.getCombinedGenerator(gen=link_search)
generator = pagegenerators.PreloadingGenerator(generator)

bot = SpamRemoveBot(generator, spam_external_url, **options)
bot.run()


if __name__ == '__main__':
Expand Down

0 comments on commit 9b288d4

Please sign in to comment.