示例#1
0
    def handleNextLink(self, text, match, context=100):
        """
        Returns a tuple (text, jumpToBeginning).

        text is the unicode string after the current link has been processed.
        jumpToBeginning is a boolean which specifies if the cursor position
        should be reset to 0. This is required after the user has edited the
        article.
        """
        # ignore interwiki links and links to sections of the same page as well
        # as section links
        if not match.group('title') \
           or self.pageToUnlink.site.isInterwikiLink(match.group('title')) \
           or match.group('section'):
            return text, False
        linkedPage = pywikibot.Page(self.pageToUnlink.site,
                                    match.group('title'))
        # Check whether the link found is to the current page itself.
        if linkedPage != self.pageToUnlink:
            # not a self-link
            return text, False
        else:
            # at the beginning of the link, start red color.
            # at the end of the link, reset the color to default
            if self.getOption('always'):
                choice = 'a'
            else:
                pywikibot.output(
                    text[max(0, match.start() - context):match.start()]
                    + '\03{lightred}' + text[match.start():match.end()]
                    + '\03{default}' + text[match.end():match.end() + context])
                choice = pywikibot.inputChoice(
                    u'\nWhat shall be done with this link?\n',
                    ['unlink', 'skip', 'edit', 'more context',
                     'unlink all', 'quit'],
                    ['U', 's', 'e', 'm', 'a', 'q'], 'u')
                pywikibot.output(u'')

                if choice == 's':
                    # skip this link
                    return text, False
                elif choice == 'e':
                    editor = TextEditor()
                    newText = editor.edit(text, jumpIndex=match.start())
                    # if user didn't press Cancel
                    if newText:
                        return newText, True
                    else:
                        return text, True
                elif choice == 'm':
                    # show more context by recursive self-call
                    return self.handleNextLink(text, match,
                                               context=context + 100)
                elif choice == 'a':
                    self.options['always'] = True
                elif choice == 'q':
                    self.quit()
            new = match.group('label') or match.group('title')
            new += match.group('linktrail')
            return text[:match.start()] + new + text[match.end():], False
示例#2
0
 def invoke_editor(self, page):
     """Ask for an editor and invoke it."""
     choice = pywikibot.input_choice('Do you want to open the page?',
                                     [('with browser', 'b'),
                                      ('with gui', 'g'), ('no', 'n')], 'n')
     if choice == 'b':
         webbrowser.open('{}?redirect=no'.format(page.full_url()))
     elif choice == 'g':
         editor = TextEditor()
         editor.edit(page.text)
 def run(self):
     try:
         old = self.page.get(get_redirect=self.options.edit_redirect)
     except pywikibot.NoPage:
         old = ""
     textEditor = TextEditor()
     new = textEditor.edit(old)
     if new and old != new:
         pywikibot.showDiff(old, new)
         changes = pywikibot.input(u"What did you change?")
         comment = i18n.twtranslate(pywikibot.getSite(), "editarticle-edit", {"description": changes})
         try:
             self.page.put(new, comment=comment, minorEdit=False, watchArticle=self.options.watch)
         except pywikibot.EditConflict:
             self.handle_edit_conflict(new)
     else:
         pywikibot.output(u"Nothing changed")
示例#4
0
 def run(self):
     self.site.login()
     try:
         old = self.page.get(get_redirect=self.options.edit_redirect)
     except pywikibot.NoPage:
         old = ""
     textEditor = TextEditor()
     new = textEditor.edit(old)
     if new and old != new:
         pywikibot.showDiff(old, new)
         changes = pywikibot.input(u"What did you change?")
         comment = i18n.twtranslate(pywikibot.Site(), 'editarticle-edit',
                                    {'description': changes})
         try:
             self.page.put(new, summary=comment, minorEdit=False,
                           watchArticle=self.options.watch)
         except pywikibot.EditConflict:
             self.handle_edit_conflict(new)
     else:
         pywikibot.output(u"Nothing changed")
示例#5
0
 def run(self):
     """Run the bot."""
     self.site.login()
     try:
         old = self.page.get(get_redirect=self.options.edit_redirect)
     except pywikibot.NoPage:
         old = ""
     textEditor = TextEditor()
     new = textEditor.edit(old)
     if new and old != new:
         pywikibot.showDiff(old, new)
         changes = pywikibot.input("What did you change?")
         comment = i18n.twtranslate(pywikibot.Site(), 'editarticle-edit',
                                    {'description': changes})
         try:
             self.page.put(new, summary=comment, minorEdit=False,
                           watchArticle=self.options.watch)
         except pywikibot.EditConflict:
             self.handle_edit_conflict(new)
     else:
         pywikibot.output("Nothing changed")
示例#6
0
 def run(self):
     """Run the bot."""
     self.site.login()
     try:
         old = self.page.get(get_redirect=self.options.edit_redirect)
     except NoPageError:
         old = ''
     text_editor = TextEditor()
     new = text_editor.edit(old)
     if new and old != new:
         pywikibot.showDiff(old, new)
         changes = pywikibot.input('What did you change?')
         comment = i18n.twtranslate(self.site, 'editarticle-edit',
                                    {'description': changes})
         try:
             self.page.put(new, summary=comment, minor=False,
                           watch=self.options.watch)
         except EditConflictError:
             self.handle_edit_conflict(new)
     else:
         pywikibot.output('Nothing changed')
 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 self.spam_external_url in line:
             if lastok:
                 pywikibot.output(lastok)
             pywikibot.output(color_format('{lightred}{0}{default}', line))
             lastok = None
         else:
             newpage.append(line)
             if line.strip():
                 if lastok is None:
                     pywikibot.output(line)
                 lastok = line
     if self.getOption('always'):
         answer = 'y'
     else:
         answer = pywikibot.input_choice('\nDelete the red lines?',
                                         [('yes', 'y'), ('no', 'n'),
                                          ('edit', 'e')],
                                         'n',
                                         automatic_quit=False)
     if answer == 'n':
         return
     elif answer == 'e':
         editor = TextEditor()
         newtext = editor.edit(text,
                               highlight=self.spam_external_url,
                               jumpIndex=text.find(self.spam_external_url))
     else:
         newtext = '\n'.join(newpage)
     if newtext != text:
         self.put_current(newtext, summary=self.getOption('summary'))
示例#8
0
 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 self.spam_external_url in line:
             if lastok:
                 pywikibot.output(lastok)
             pywikibot.output(color_format('{lightred}{0}{default}', line))
             lastok = None
         else:
             newpage.append(line)
             if line.strip():
                 if lastok is None:
                     pywikibot.output(line)
                 lastok = line
     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":
         return
     elif answer == "e":
         editor = TextEditor()
         newtext = editor.edit(text, highlight=self.spam_external_url,
                               jumpIndex=text.find(self.spam_external_url))
     else:
         newtext = "\n".join(newpage)
     if newtext != text:
         self.put_current(newtext, summary=self.getOption('summary'))
示例#9
0
    def unlink(self, target_page):
        """Unlink all links linking to the target page."""
        text = self.current_page.text
        while True:
            unlink_callback = self._create_callback()
            try:
                text = replace_links(text, unlink_callback, target_page.site)
            except EditReplacement:
                new_text = TextEditor().edit(
                    unlink_callback.current_text,
                    jumpIndex=unlink_callback.current_range[0])
                # if user didn't press Cancel
                if new_text:
                    text = new_text
                else:
                    text = unlink_callback.current_text
            else:
                break

        self.put_current(text)
示例#10
0
    def handleNextLink(self, page, match, context=100):
        """Process the next link on a page, offering the user choices.

        @param page: The page being edited
        @type page: pywikibot.Page
        @param match: The match object for the current link.
        @type match: re.MatchObject
        @param context: The amount of context around the link shown to the user
        @type context: int
        @return: jumpToBeginning, a boolean, which specifies if the cursor
        position should be reset to 0. This is required after the user has
        edited the article.
        """
        # ignore interwiki links and links to sections of the same page as well
        # as section links
        if not match.group('title') \
           or page.site.isInterwikiLink(match.group('title')) \
           or match.group('section'):
            return False
        try:
            linkedPage = pywikibot.Page(page.site, title=match.group('title'))
        except pywikibot.InvalidTitle as err:
            pywikibot.warning(u'%s' % err)
            return False

        # Check whether the link found is to the current page itself.
        if linkedPage != page:
            # not a self-link, nothing to do
            return False

        # at the beginning of the link, start red color.
        # at the end of the link, reset the color to default
        if self.getOption('always'):
            choice = 'a'
        else:
            pre = page.text[max(0, match.start() - context):match.start()]
            post = page.text[match.end():match.end() + context]
            matchText = match.group(0)
            pywikibot.output(
                pre + '\03{lightred}' + matchText + '\03{default}' + post)
            choice = pywikibot.input_choice(
                u'\nWhat shall be done with this selflink?\n',
                [('unlink', 'u'), ('make bold', 'b'), ('skip', 's'),
                 ('edit', 'e'), ('more context', 'm'), ('unlink all')], 'u')
            pywikibot.output(u'')

            if choice == 's':
                # skip this link
                return False
            elif choice == 'e':
                editor = TextEditor()
                newText = editor.edit(page.text, jumpIndex=match.start())
                # if user didn't press Cancel
                if newText:
                    page.text = newText
                    return True
                else:
                    return True
            elif choice == 'm':
                # show more context by recursive self-call
                return self.handleNextLink(page, match, context=context + 100)
            elif choice == 'a':
                self.always = True

        # choice was 'U', 'b', or 'a'
        new = match.group('label') or match.group('title')
        new += match.group('linktrail')
        preMatch = page.text[:match.start()]
        postMatch = page.text[match.end():]
        if choice == 'b':
            # make bold
            page.text = preMatch + "'''" + new + "'''" + postMatch
            return False
        else:
            page.text = preMatch + new + postMatch
            return False
示例#11
0
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
    """
    always = False
    namespaces = []
    spamSite = ''
    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:'):])
        else:
            spamSite = arg

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

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

    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()))
        lines = text.split('\n')
        newpage = []
        lastok = ""
        for line in lines:
            if spamSite in line:
                if lastok:
                    pywikibot.output(lastok)
                pywikibot.output(color_format('{lightred}{0}{default}', line))
                lastok = None
            else:
                newpage.append(line)
                if line.strip():
                    if lastok is None:
                        pywikibot.output(line)
                    lastok = line
        if 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
        elif answer == "e":
            editor = TextEditor()
            newtext = editor.edit(text, highlight=spamSite,
                                  jumpIndex=text.find(spamSite))
        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.')
        else:
            pywikibot.output('%d pages done.' % i)
示例#12
0
class SelflinkBot(Bot):
    def __init__(self, generator, **kwargs):
        super(SelflinkBot, self).__init__(**kwargs)
        self.generator = generator
        linktrail = pywikibot.Site().linktrail()
        # The regular expression which finds links. Results consist of four
        # groups:
        # group title is the target page title, everything before | or ].
        # group section is the page section. It'll include the # to make life
        # easier for us.
        # group label is the alternative link title, that's everything between
        # | and ].
        # group linktrail is the link trail, that's letters after ]] which are
        # part of the word.
        # note that the definition of 'letter' varies from language to
        # language.
        self.linkR = re.compile(r'\[\[(?P<title>[^\]\|#]*)'
                                r'(?P<section>#[^\]\|]*)?'
                                '(\|(?P<label>[^\]]*))?\]\]'
                                r'(?P<linktrail>' + linktrail + ')')
        self.done = False

    def handleNextLink(self, page, match, context=100):
        """Process the next link on a page, offering the user choices.

        @param page: The page being edited
        @type page: pywikibot.Page
        @param match: The match object for the current link.
        @type match: re.MatchObject
        @param context: The amount of context around the link shown to the user
        @type context: int
        @return: jumpToBeginning, a boolean, which specifies if the cursor
        position should be reset to 0. This is required after the user has
        edited the article.
        """
        # ignore interwiki links and links to sections of the same page as well
        # as section links
        if not match.group('title') \
           or page.site.isInterwikiLink(match.group('title')) \
           or match.group('section'):
            return False
        try:
            linkedPage = pywikibot.Page(page.site, title=match.group('title'))
        except pywikibot.InvalidTitle, err:
            pywikibot.warning(u'%s' % err)
            return False

        # Check whether the link found is to the current page itself.
        if linkedPage != page:
            # not a self-link, nothing to do
            return False

        # at the beginning of the link, start red color.
        # at the end of the link, reset the color to default
        if self.getOption('always'):
            choice = 'a'
        else:
            pre = page.text[max(0, match.start() - context):match.start()]
            post = page.text[match.end():match.end() + context]
            matchText = match.group(0)
            pywikibot.output(pre + '\03{lightred}' + matchText +
                             '\03{default}' + post)
            choice = pywikibot.inputChoice(
                u'\nWhat shall be done with this selflink?\n', [
                    'unlink', 'make bold', 'skip', 'edit', 'more context',
                    'unlink all', 'quit'
                ], ['U', 'b', 's', 'e', 'm', 'a', 'q'], 'u')
            pywikibot.output(u'')

            if choice == 's':
                # skip this link
                return False
            elif choice == 'e':
                editor = TextEditor()
                newText = editor.edit(page.text, jumpIndex=match.start())
                # if user didn't press Cancel
                if newText:
                    page.text = newText
                    return True
                else:
                    return True
            elif choice == 'm':
                # show more context by recursive self-call
                return self.handleNextLink(page, match, context=context + 100)
            elif choice == 'a':
                self.always = True
            elif choice == 'q':
                self.done = True
                return False

        # choice was 'U', 'b', or 'a'
        new = match.group('label') or match.group('title')
        new += match.group('linktrail')
        preMatch = page.text[:match.start()]
        postMatch = page.text[match.end():]
        if choice == 'b':
            # make bold
            page.text = preMatch + "'''" + new + "'''" + postMatch
            return False
        else:
            page.text = preMatch + new + postMatch
            return False
示例#13
0
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
    """
    always = False
    namespaces = []
    spamSite = ''
    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:'):])
        else:
            spamSite = arg

    if not spamSite:
        pywikibot.showHelp()
        pywikibot.output(u"No spam site specified.")
        return

    mysite = pywikibot.Site()
    pages = mysite.exturlusage(spamSite)
    if namespaces:
        pages = pagegenerators.NamespaceFilterPageGenerator(pages, namespaces)
    pages = pagegenerators.PreloadingGenerator(pages)

    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(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
                         % p.title())
        lines = text.split('\n')
        newpage = []
        lastok = ""
        for line in lines:
            if spamSite in line:
                if lastok:
                    pywikibot.output(lastok)
                pywikibot.output('\03{lightred}%s\03{default}' % line)
                lastok = None
            else:
                newpage.append(line)
                if line.strip():
                    if lastok is None:
                        pywikibot.output(line)
                    lastok = line
        if 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
        elif answer == "e":
            editor = TextEditor()
            newtext = editor.edit(text, highlight=spamSite,
                                  jumpIndex=text.find(spamSite))
        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.')
        else:
            pywikibot.output('%d pages done.' % i)
示例#14
0
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
    """
    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

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

    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()))
        lines = text.split('\n')
        newpage = []
        lastok = ""
        for line in lines:
            if spamSite in line:
                if lastok:
                    pywikibot.output(lastok)
                pywikibot.output(color_format('{lightred}{0}{default}', line))
                lastok = None
            else:
                newpage.append(line)
                if line.strip():
                    if lastok is None:
                        pywikibot.output(line)
                    lastok = line
        if 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
        elif answer == "e":
            editor = TextEditor()
            newtext = editor.edit(text,
                                  highlight=spamSite,
                                  jumpIndex=text.find(spamSite))
        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.')
        else:
            pywikibot.output('%d pages done.' % i)
示例#15
0
    def handleNextLink(self, page, match, context=100):
        """Process the next link on a page, offering the user choices.

        @param page: The page being edited
        @type page: pywikibot.Page
        @param match: The match object for the current link.
        @type match: re.MatchObject
        @param context: The amount of context around the link shown to the user
        @type context: int
        @return: jumpToBeginning, a boolean, which specifies if the cursor
        position should be reset to 0. This is required after the user has
        edited the article.
        """
        # ignore interwiki links and links to sections of the same page as well
        # as section links
        if not match.group('title') \
           or page.site.isInterwikiLink(match.group('title')) \
           or match.group('section'):
            return False
        try:
            linkedPage = pywikibot.Page(page.site, title=match.group('title'))
        except pywikibot.InvalidTitle as err:
            pywikibot.warning(u'%s' % err)
            return False

        # Check whether the link found is to the current page itself.
        if linkedPage != page:
            # not a self-link, nothing to do
            return False

        # at the beginning of the link, start red color.
        # at the end of the link, reset the color to default
        if self.getOption('always'):
            choice = 'a'
        else:
            pre = page.text[max(0, match.start() - context):match.start()]
            post = page.text[match.end():match.end() + context]
            matchText = match.group(0)
            pywikibot.output(
                pre + '\03{lightred}' + matchText + '\03{default}' + post)
            choice = pywikibot.input_choice(
                u'\nWhat shall be done with this selflink?\n',
                [('unlink', 'u'), ('make bold', 'b'), ('skip', 's'),
                 ('edit', 'e'), ('more context', 'm'), ('unlink all')], 'u')
            pywikibot.output(u'')

            if choice == 's':
                # skip this link
                return False
            elif choice == 'e':
                editor = TextEditor()
                newText = editor.edit(page.text, jumpIndex=match.start())
                # if user didn't press Cancel
                if newText:
                    page.text = newText
                    return True
                else:
                    return True
            elif choice == 'm':
                # show more context by recursive self-call
                return self.handleNextLink(page, match, context=context + 100)
            elif choice == 'a':
                self.always = True

        # choice was 'U', 'b', or 'a'
        new = match.group('label') or match.group('title')
        new += match.group('linktrail')
        preMatch = page.text[:match.start()]
        postMatch = page.text[match.end():]
        if choice == 'b':
            # make bold
            page.text = preMatch + "'''" + new + "'''" + postMatch
            return False
        else:
            page.text = preMatch + new + postMatch
            return False
示例#16
0
def editDesc(itemPage, key, description, newDescription, count, editMode,
             editGroup, logName):
    """Provides the available options and executes the corresponding editions.

    Parameters
    ----------
    itemPage : string
        Wikidata item.
    key : string
        language of the description with a fulls stop.
    description : string
        the description with the full stop.
    newDescription : string
        the description without the full stop
    count : integer
        counter incremented by 1
    editMode : boolean
        editing mode, False for test edits or True to enable the editing.
    editGroup : boolean
        if it is true, generate a hash to identify the EditGroup,
        check: https://tools.wmflabs.org/editgroups
    logName : string
        name of the logfile.

    Returns
    -------
    string
        edition

    """
    replacement = {key: newDescription}

    # Summaries for the action of "remove full stop" and for "Edit description"
    # respectively
    summary = {
        "removed": "removing end full stop/period of {}-desc".format(key),
        "edited":
        "removing end full stop/period and fixing {}-desc".format(key)
    }

    # Check if the operator choose to create an EditGroup and change the Summaries
    # according to the decision
    if editGroup is not None:
        summary = {
            "removed":
            "{} ([[:toollabs:editgroups/b/CB/{}|details]])".format(
                summary["removed"], editGroup),
            "edited":
            "{} ([[:toollabs:editgroups/b/CB/{}|details]])".format(
                summary["edited"], editGroup)
        }

    # Item identifier formatted
    item = str(itemPage).lstrip("[[wikidata:").rstrip("]]")

    # Ask for the correct action
    questions = [
        inquirer.List(
            'actions',
            message="What do you want to do?",
            choices=[
                'Remove full stop', 'Add description to checklist',
                'Edit description', 'Remove duplicates automatically',
                'Skip description', 'Quit'
            ],
        ),
    ]

    answers = inquirer.prompt(questions)

    # Remove full stop
    if answers["actions"] == "Remove full stop":
        count[key] += 1
        try:
            # Check the editing mode
            if editMode is True:
                # Information string to print in the cli
                info = u"{}{}{}{}\t{}\tfull stop removed".format(
                    c.Fore.WHITE, c.Style.BRIGHT, item, cR, lang[key])
                print(info)

                # Information dictionary to make the log (without Colorama)
                info = {
                    "time": timestamp,
                    "item": item,
                    "key": key + "-desc",
                    "msg": "full stop removed"
                }

                # Applying the edition
                itemPage.editDescriptions(replacement,
                                          summary=summary["removed"])

                # Checking and updating the CSV logfile
                log.check(info, logName, mode="csv")

            else:
                info = u"{}{}{}{}\t{}\tfull stop removed (non edit made, test mode)".format(
                    c.Fore.WHITE, c.Style.BRIGHT, item, cR, lang[key])
                print(info)

                info = {
                    "time": timestamp,
                    "item": item,
                    "key": key + "-desc",
                    "msg": "full stop removed (non edit made, test mode)"
                }

                log.check(info, logName, mode="csv")

        except Exception as e:
            print(e)

            info = {
                "time": timestamp,
                "item": item,
                "key": key + "-desc",
                "msg": e
            }

            log.check(info, logName, mode="csv")

    # Add description to checklist.
    elif answers["actions"] == "Add description to checklist":
        info = {
            "time": timestamp,
            "item": item,
            "key": key + "-desc",
            "msg": "Added to checklist: " + description
        }

        # It doesn't edit the item description, it just add the info to the logfile
        log.check(info, logName, mode="csv")
        # And update the checklist to review manually later.
        log.check(info, "descriptionCheckList", mode="csv")

    # Edit description
    elif answers["actions"] == "Edit description":
        # If the description has other error, the operator can edit it directly
        # from the terminal
        textEditor = TextEditor()
        newDescription = textEditor.edit(description)

        # Diff to check that everything is correct
        if newDescription and description != newDescription:
            pywikibot.showDiff(description, newDescription)

            question = [
                inquirer.Confirm(
                    "confirmation",
                    message="Are you sure you want to make this change?")
            ]

            answer = inquirer.prompt(question)

            # If the operator confirm the change...
            if answer["confirmation"] is True:
                try:
                    # Check the editing mode
                    if editMode is True:
                        info = u"{}{}{}{}\t{}\tfull stop removed and other errors fixed".format(
                            c.Fore.WHITE, c.Style.BRIGHT, item, cR, lang[key])
                        print(info)

                        info = {
                            "time": timestamp,
                            "item": item,
                            "key": key + "-desc",
                            "msg": "full stop removed and other errors fixed"
                        }

                        itemPage.editDescriptions(replacement,
                                                  summary=summary["edited"])
                        log.check(info, logName, mode="csv")

                    else:
                        info = u"{}{}{}{}\t{}\tfull stop removed and other errors fixed (test mode)".format(
                            c.Fore.WHITE, c.Style.BRIGHT, item, cR, lang[key])
                        print(info)

                        info = {
                            "time":
                            timestamp,
                            "item":
                            item,
                            "key":
                            key + "-desc",
                            "msg":
                            "full stop removed and other errors fixed (test mode)"
                        }

                        log.check(info, logName, mode="csv")

                except Exception as e:
                    print(e)

                    info = {
                        "time": timestamp,
                        "item": item,
                        "key": key + "-desc",
                        "msg": e
                    }

                    log.check(info, logName, mode="csv")

            # If the operator doesn't confirm the change
            else:
                info = u"{}{}{}{}\t{}The change hasn't been made by decision of the operator.".format(
                    c.Fore.WHITE, c.Style.BRIGHT, item, cR, lang[key])
                print(info)

                info = {
                    "time":
                    timestamp,
                    "item":
                    item,
                    "key":
                    key + "-desc",
                    "msg":
                    "The change hasn't been made by decision of the operator"
                }

                log.check(info, logName, mode="csv")

        else:
            print("No changes were made.")

    # Remove if the script find the same description again
    elif answers["actions"] == "Remove duplicates automatically":
        global dataIndex
        dataIndex += 1

        with open(duplicated, "r+") as duplicatedFile:
            reader = csv.reader(duplicatedFile, delimiter=",")
            for row in reader:
                if description not in row[1]:
                    writer = csv.writer(duplicatedFile, delimiter=",")
                    writer.writerow([dataIndex, description])
                    print("Duplicated description added to the file.")

                    try:
                        # Check the editing mode
                        if editMode is True:
                            # Information string to print in the cli
                            info = u"{}{}{}{}\t{}\tfull stop removed automatically".format(
                                c.Fore.WHITE, c.Style.BRIGHT, item, cR,
                                lang[key])
                            print(info)

                            # Information dictionary to make the log (without Colorama)
                            info = {
                                "time": timestamp,
                                "item": item,
                                "key": key + "-desc",
                                "msg": "full stop removed automatically"
                            }

                            # Applying the edition
                            itemPage.editDescriptions(
                                replacement, summary=summary["removed"])

                            # Checking and updating the CSV logfile
                            log.check(info, logName, mode="csv")

                        else:
                            info = u"{}{}{}{}\t{}\tfull stop removed automatically(non edit made, test mode)".format(
                                c.Fore.WHITE, c.Style.BRIGHT, item, cR,
                                lang[key])
                            print(info)

                            info = {
                                "time":
                                timestamp,
                                "item":
                                item,
                                "key":
                                key + "-desc",
                                "msg":
                                "full stop removed automatically (non edit made, test mode)"
                            }

                            log.check(info, logName, mode="csv")

                    except Exception as e:
                        print(e)

                        info = {
                            "time": timestamp,
                            "item": item,
                            "key": key + "-desc",
                            "msg": e
                        }

                        log.check(info, logName, mode="csv")

    # Skip description
    elif answers["actions"] == "Skip description":
        info = u"{}{}{}{}\t{}\tskipped.".format(c.Fore.WHITE, c.Style.BRIGHT,
                                                item, cR, lang[key])
        print(info)

        info = {
            "time": timestamp,
            "item": item,
            "key": key + "-desc",
            "msg": "skipped"
        }

        log.check(info, logName, mode="csv")

    # Quit
    elif answers["actions"] == "Quit":
        print("Stopping bot...")
        # Set "edit" as False to stop the script
        edit = False

        return edit

    else:
        print("Something goes wrong...")
示例#17
0
def main():
    always = False
    namespaces = []
    spamSite = ''
    for arg in pywikibot.handleArgs():
        if arg == "-always":
            always = True
        elif arg.startswith('-namespace:'):
            try:
                namespaces.append(int(arg[len('-namespace:'):]))
            except ValueError:
                namespaces.append(arg[len('-namespace:'):])
        else:
            spamSite = arg

    if not spamSite:
        pywikibot.showHelp()
        pywikibot.output(u"No spam site specified.")
        return

    mysite = pywikibot.Site()
    pages = mysite.exturlusage(spamSite)
    if namespaces:
        pages = pagegenerators.NamespaceFilterPageGenerator(pages, namespaces)
    pages = pagegenerators.PreloadingGenerator(pages)

    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(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
                         % p.title())
        lines = text.split('\n')
        newpage = []
        lastok = ""
        for line in lines:
            if spamSite in line:
                if lastok:
                    pywikibot.output(lastok)
                pywikibot.output('\03{lightred}%s\03{default}' % line)
                lastok = None
            else:
                newpage.append(line)
                if line.strip():
                    if lastok is None:
                        pywikibot.output(line)
                    lastok = line
        if always:
            answer = "y"
        else:
            answer = pywikibot.inputChoice(u'\nDelete the red lines?',
                                           ['yes', 'no', 'edit'],
                                           ['y', 'N', 'e'], 'n')
        if answer == "n":
            continue
        elif answer == "e":
            editor = TextEditor()
            newtext = editor.edit(text, highlight=spamSite,
                                  jumpIndex=text.find(spamSite))
        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.')
        else:
            pywikibot.output('%d pages done.' % i)