User:YiFeiBot/wikiindex size.py: Difference between revisions

From WikiIndex
Jump to navigation Jump to search
(upd)
(readable)
 
(One intermediate revision by the same user not shown)
Line 4: Line 4:
# -*- coding: utf-8  -*-
# -*- coding: utf-8  -*-
#
#
__version__ = '$Id: wikiindex-size.py 11155 2013-05-13 10:39:02Z xqt $'
__version__ = '$Id: NULL $'
#
#


Line 40: Line 40:
         self.generator = generator
         self.generator = generator
         # init constants
         # init constants
         self.site = pywikibot.getSite("wikiindex", "wikiindex")
         self.site = pywikibot.getSite("en", "wikiindex")
         # Set the edit summary message
         # Set the edit summary message
         if summary:
         if summary:
Line 62: Line 62:
         newtext = text
         newtext = text
         size_r = None
         size_r = None
         size_r = re.compile(ur"""(?P<all>\{\{\s*Size\s*((\|\s*(?P<pages>pages|wiki[ _]pages)\s*=\s*(?P<pages_value>\d*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<pagesurl>statistics[ _]URL|wiki[ _]statistics[ _]URL)\s*=\s*(?P<pagesurl_value>https?://[^ \|\}\<]*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<wikifactor>wikiFactor)\s*=\s*(?P<wikifactor_value>\d*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<wikifactorurl>wikiFactor[ _]URL)\s*=\s*(?P<wikifactorurl_value>http://[^ \|\}\<]*)\s*[^\|\}]*\s*))+\s*\|?\s*\}\}(\s*\([Aa]s\s*of:?\s*(?P<day>\d+)\s*(?P<month>[A-Z][a-z]+)\s*(?P<year>\d+)\s*\)\s*(\<!--manually add date when stats are verified and/or updated--\>)?)?)""")
         size_r = re.compile(ur"""(?P<all>\{\{\s*[Ss]ize\s*((\|\s*(?P<pages>pages|wiki[ _]pages)\s*=\s*(?P<pages_value>\d*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<pagesurl>statistics[ _]URL|wiki[ _]statistics[ _]URL)\s*=\s*(?P<pagesurl_value>https?://[^ \|\}\<]*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<wikifactor>wikiFactor)\s*=\s*(?P<wikifactor_value>\d*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<wikifactorurl>wikiFactor[ _]URL)\s*=\s*(?P<wikifactorurl_value>http://[^ \|\}\<]*)\s*[^\|\}]*\s*))+\s*\|?\s*\}\}(\s*\([Aa]s\s*of:?\s*(?P<day>\d+)\s*(?P<month>[A-Z][a-z]+)\s*(?P<year>\d+)\s*\)\s*(\<!--[ A-Za-z0-9/]+--\>)?)?)""")


         wtext = page.get()
         wtext = page.get()
Line 85: Line 85:
              
              
             if self.debug:
             if self.debug:
                 pywikibot.output(u"text = %s" % text)
                 pywikibot.output(u"text = " + text)
                 pywikibot.output(u"all = %s" % all)
                 pywikibot.output(u"all = " + all)
                 pywikibot.output(u"pages = %s" % pages)
                 pywikibot.output(u"pages = " + pages)
                 pywikibot.output(u"pagesurl = %s" % pagesurl)
                 pywikibot.output(u"pagesurl = " + pagesurl)
                 pywikibot.output(u"wikifactor = %s" % wikifactor)
                 pywikibot.output(u"wikifactor = " + wikifactor)
                 pywikibot.output(u"wikifactorurl = %s" % wikifactorurl)
                 pywikibot.output(u"wikifactorurl = " + wikifactorurl)
                 pywikibot.output(u"pages_value = %s" % pages_value)
                 pywikibot.output(u"pages_value = " + pages_value)
                 pywikibot.output(u"pagesurl_value = %s" % pagesurl_value)
                 pywikibot.output(u"pagesurl_value = " + pagesurl_value)
                 pywikibot.output(u"wikifactor_value = %s" % wikifactor_value)
                 pywikibot.output(u"wikifactor_value = " + wikifactor_value)
                 pywikibot.output(u"wikifactorurl_value = %s" % wikifactorurl_value)
                 pywikibot.output(u"wikifactorurl_value = " + wikifactorurl_value)
                 pywikibot.output(u"day = %s" % day)
                 pywikibot.output(u"day = " + day)
                 pywikibot.output(u"month = %s" % month)
                 pywikibot.output(u"month = " + month)
                 pywikibot.output(u"year = %s" % year)
                 pywikibot.output(u"year = " + year)


             #get new values
             #get new values
Line 147: Line 147:
| %s = %s <!--preferred; if unknown leave void; see: Category:wikiFactor for help-->
| %s = %s <!--preferred; if unknown leave void; see: Category:wikiFactor for help-->
| %s = %s <!--wF source (often 'PopularPages', 'Mostvisitedpages' or 'PageHits'); if unknown leave void-->
| %s = %s <!--wF source (often 'PopularPages', 'Mostvisitedpages' or 'PageHits'); if unknown leave void-->
}}(As of: %s %s %s)<!--manually add/amend date when stats are verified and/or updated-->""" % (pages and pages or 'pages', pages_value and pages_value or '', pagesurl and pagesurl or 'statistics URL', pagesurl_value and pagesurl_value or '', wikifactor and wikifactor or 'wikiFactor', wikifactor_value and wikifactor_value or '', wikifactorurl and wikifactorurl or 'wikiFactor URL', wikifactorurl_value and wikifactorurl_value or '', day and day or '', month and month or '', year and year or '')
}}(As of: %s %s %s)<!--manually add/amend date when stats are verified and/or updated-->""" % (
                pages or 'pages',
                pages_value or '',
                pagesurl or 'statistics URL',
                pagesurl_value or '',
                wikifactor or 'wikiFactor',
                wikifactor_value or '',
                wikifactorurl or 'wikiFactor URL',
                wikifactorurl_value or '',
                day or '',
                month or '',
                year or '')
             newtext = text.replace(all, newvalues)
             newtext = text.replace(all, newvalues)
          
          
Line 231: Line 242:
             pywikibot.output(u'Unknown argument: %s' % arg)
             pywikibot.output(u'Unknown argument: %s' % arg)


     cat = catlib.Category(pywikibot.getSite("wikiindex", "wikiindex"), 'Category:MediaWiki')
     cat = catlib.Category(pywikibot.getSite("en", "wikiindex"), 'Category:MediaWiki')
     gen = pagegenerators.CategorizedPageGenerator(cat, start=start)
     gen = pagegenerators.CategorizedPageGenerator(cat, start=start)



Latest revision as of 06:34, 25 August 2014

#!/usr/bin/python
# -*- coding: utf-8  -*-
#
__version__ = '$Id: NULL $'
#

import re

import wikipedia as pywikibot
import pagegenerators
from pywikibot import i18n
import catlib
import sys
import urllib
from datetime import datetime

# This is required for the text that is shown when you run this script
# with the parameter -help.
docuReplacements = {
    '&params;': pagegenerators.parameterHelp
}


class WiSizeBot:
    # Edit summary message that should be used is placed on /i18n subdirectory.
    # The file containing these messages should have the same name as the caller
    # script (i.e. wikiindex-size.py in this case)

    def __init__(self, generator, summary, debug=False):
        """
        Constructor. Parameters:
            @param generator: The page generator that determines on which pages
                              to work.
            @type generator: generator.
            @param summary: Set the summary message text for the edit.
            @type summary: (unicode) string.
        """
        self.generator = generator
        # init constants
        self.site = pywikibot.getSite("en", "wikiindex")
        # Set the edit summary message
        if summary:
            self.summary = summary
        else:
            self.summary = i18n.twtranslate(self.site, 'basic-changing')
        self.debug = debug

    def run(self):
        for page in self.generator:
            self.treat(page)

    def treat(self, page):
        """
        Loads the given page, does some changes, and saves it.
        """
        text = self.load(page)
        if not text:
            return

        newtext = text
        size_r = None
        size_r = re.compile(ur"""(?P<all>\{\{\s*[Ss]ize\s*((\|\s*(?P<pages>pages|wiki[ _]pages)\s*=\s*(?P<pages_value>\d*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<pagesurl>statistics[ _]URL|wiki[ _]statistics[ _]URL)\s*=\s*(?P<pagesurl_value>https?://[^ \|\}\<]*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<wikifactor>wikiFactor)\s*=\s*(?P<wikifactor_value>\d*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<wikifactorurl>wikiFactor[ _]URL)\s*=\s*(?P<wikifactorurl_value>http://[^ \|\}\<]*)\s*[^\|\}]*\s*))+\s*\|?\s*\}\}(\s*\([Aa]s\s*of:?\s*(?P<day>\d+)\s*(?P<month>[A-Z][a-z]+)\s*(?P<year>\d+)\s*\)\s*(\
| %s = %s 
| %s = %s 
| %s = %s 
| %s = %s 
}}(As of: %s %s %s)""" % (
                pages or 'pages',
                pages_value or ,
                pagesurl or 'statistics URL',
                pagesurl_value or ,
                wikifactor or 'wikiFactor',
                wikifactor_value or ,
                wikifactorurl or 'wikiFactor URL',
                wikifactorurl_value or ,
                day or ,
                month or ,
                year or )
            newtext = text.replace(all, newvalues)
        
        if not self.save(newtext, page, self.summary):
            pywikibot.output(u'Page %s not saved.' % page.title(asLink=True))

    def load(self, page):
        """
        Loads the given page, does some changes, and saves it.
        """
        try:
            # Load the page
            text = page.get()
        except pywikibot.NoPage:
            pywikibot.output(u"Page %s does not exist; skipping."
                             % page.title(asLink=True))
        except pywikibot.IsRedirectPage:
            pywikibot.output(u"Page %s is a redirect; skipping."
                             % page.title(asLink=True))
        else:
            return text
        return None

    def save(self, text, page, comment=None, **kwargs):
        # only save if something was changed
        if text != page.get():
            # 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} <<<"
                             % page.title())
            # show what was changed
            pywikibot.showDiff(page.get(), text)
            pywikibot.output(u'Comment: %s' % comment)
            #choice = pywikibot.inputChoice(
            #    u'Do you want to accept these changes?',
            #    ['Yes', 'No'], ['y', 'N'], 'N')
            if True:
                try:
                    # Save the page
                    page.put(text, comment=comment or self.comment, **kwargs)
                except pywikibot.LockedPage:
                    pywikibot.output(u"Page %s is locked; skipping."
                                     % page.title(asLink=True))
                except pywikibot.EditConflict:
                    pywikibot.output(
                        u'Skipping %s because of edit conflict'
                        % (page.title()))
                except pywikibot.SpamfilterError, error:
                    pywikibot.output(
                        u'Cannot change %s because of spam blacklist entry %s'
                        % (page.title(), error.url))
                else:
                    return True
        return False




def main():
    # This factory is responsible for processing command line arguments
    # that are also used by other scripts and that determine on which pages
    # to work on.
    genFactory = pagegenerators.GeneratorFactory()
    # The generator gives the pages that should be worked upon.
    gen = None
    # This temporary array is used to read the page title if one single
    # page to work on is specified by the arguments.
    pageTitleParts = []
    # summary message
    editSummary = 
    debug = False
    start = "!"

    # Parse command line arguments
    for arg in pywikibot.handleArgs():
        if arg.startswith('-summary:'):
            editSummary = arg[9:]
        elif arg.startswith('-start:'):
            start = arg[7:]
        elif arg == '-debug':
            debug = True
        else:
            pywikibot.output(u'Unknown argument: %s' % arg)

    cat = catlib.Category(pywikibot.getSite("en", "wikiindex"), 'Category:MediaWiki')
    gen = pagegenerators.CategorizedPageGenerator(cat, start=start)

    #if not gen:
    #    gen = genFactory.getCombinedGenerator()
    if gen:
        # The preloading generator is responsible for downloading multiple
        # pages from the wiki simultaneously.
        gen = pagegenerators.PreloadingGenerator(gen)
        bot = WiSizeBot(gen, editSummary, debug)
        bot.run()
    else:
        pywikibot.showHelp()

if __name__ == "__main__":
    try:
        main()
    finally:
        pywikibot.stopme()