User:YiFeiBot/wikiindex size.py: Difference between revisions
Jump to navigation
Jump to search
Zhuyifei1999 (talk | contribs) m (Zhuyifei1999 moved page User:YiFeiBot/wikiindex-size.py to User:YiFeiBot/wikiindex size.py: File remamed because of the fail of importing) |
Zhuyifei1999 (talk | contribs) (readable) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
# | # | ||
__version__ = '$Id: | __version__ = '$Id: NULL $' | ||
# | # | ||
Line 29: | Line 29: | ||
# script (i.e. wikiindex-size.py in this case) | # script (i.e. wikiindex-size.py in this case) | ||
def __init__(self, generator, summary): | def __init__(self, generator, summary, debug=False): | ||
""" | """ | ||
Constructor. Parameters: | Constructor. Parameters: | ||
Line 40: | Line 40: | ||
self.generator = generator | self.generator = generator | ||
# init constants | # init constants | ||
self.site = pywikibot.getSite( | self.site = pywikibot.getSite("en", "wikiindex") | ||
# Set the edit summary message | # Set the edit summary message | ||
if summary: | if summary: | ||
Line 46: | Line 46: | ||
else: | else: | ||
self.summary = i18n.twtranslate(self.site, 'basic-changing') | self.summary = i18n.twtranslate(self.site, 'basic-changing') | ||
self.debug = debug | |||
def run(self): | def run(self): | ||
Line 60: | Line 61: | ||
newtext = text | newtext = text | ||
size_r = None | |||
size_r = re.compile(ur""" | 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 79: | Line 80: | ||
wikifactor_value = i.group('wikifactor_value') and i.group('wikifactor_value').strip() or '' | wikifactor_value = i.group('wikifactor_value') and i.group('wikifactor_value').strip() or '' | ||
wikifactorurl_value = i.group('wikifactorurl_value') and i.group('wikifactorurl_value').strip() or '' | wikifactorurl_value = i.group('wikifactorurl_value') and i.group('wikifactorurl_value').strip() or '' | ||
day = i.group('day') and i.group('day').strip() or '' | day = i.group('day') and i.group('day').strip() or '' | ||
month = i.group('month') and i.group('month').strip() or '' | month = i.group('month') and i.group('month').strip() or '' | ||
year = i.group('year') and i.group('year').strip() or '' | year = i.group('year') and i.group('year').strip() or '' | ||
if self.debug: | |||
pywikibot.output(u"text = " + text) | |||
pywikibot.output(u"all = " + all) | |||
pywikibot.output(u"pages = " + pages) | |||
pywikibot.output(u"pagesurl = " + pagesurl) | |||
pywikibot.output(u"wikifactor = " + wikifactor) | |||
pywikibot.output(u"wikifactorurl = " + wikifactorurl) | |||
pywikibot.output(u"pages_value = " + pages_value) | |||
pywikibot.output(u"pagesurl_value = " + pagesurl_value) | |||
pywikibot.output(u"wikifactor_value = " + wikifactor_value) | |||
pywikibot.output(u"wikifactorurl_value = " + wikifactorurl_value) | |||
pywikibot.output(u"day = " + day) | |||
pywikibot.output(u"month = " + month) | |||
pywikibot.output(u"year = " + year) | |||
#get new values | #get new values | ||
n = re.findall(ur"(https?://[^\|\}\]]+\?action=raw|https?://[^\|\}\]]+:Statistics)", pagesurl_value) | n = re.findall(ur"(https?://[^\|\}\]]+\?action=raw|https?://[^\|\}\]]+:Statistics)", pagesurl_value) | ||
Line 100: | Line 115: | ||
if o: | if o: | ||
if o[0] and int(pages_value) != int(o[0]): | if o[0] and int(pages_value) != int(o[0]): | ||
self.summary = u"Robot: Updating size: %s -> %s" % (pages_value, o[0]) | |||
pages_value = o[0] | pages_value = o[0] | ||
newtime = True | newtime = True | ||
Line 127: | Line 142: | ||
year = dt.strftime('%Y') | year = dt.strftime('%Y') | ||
newvalues = u"""{{Size <!--see Template:Size for full | newvalues = u"""{{Size <!--see Template:Size for full detail--> | ||
| %s = %s <!--type the plain number of pages - NO thousands separators--> | | %s = %s <!--type the plain number of pages - NO thousands separators--> | ||
| %s = %s <!--page count source (often a 'Statistics' page); if unknown type 'No'--> | | %s = %s <!--page count source (often a 'Statistics' page); if unknown type 'No'--> | ||
| %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-->""" % ( | }}(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) | ||
if not self.save(newtext, page, self.summary): | if not self.save(newtext, page, self.summary): | ||
Line 166: | Line 191: | ||
pywikibot.showDiff(page.get(), text) | pywikibot.showDiff(page.get(), text) | ||
pywikibot.output(u'Comment: %s' % comment) | pywikibot.output(u'Comment: %s' % comment) | ||
choice = pywikibot.inputChoice( | #choice = pywikibot.inputChoice( | ||
# u'Do you want to accept these changes?', | |||
# ['Yes', 'No'], ['y', 'N'], 'N') | |||
if | if True: | ||
try: | try: | ||
# Save the page | # Save the page | ||
Line 189: | Line 214: | ||
Line 334: | Line 228: | ||
# summary message | # summary message | ||
editSummary = '' | editSummary = '' | ||
debug = False | |||
start = "!" | start = "!" | ||
Line 342: | Line 237: | ||
elif arg.startswith('-start:'): | elif arg.startswith('-start:'): | ||
start = arg[7:] | start = arg[7:] | ||
elif arg == '-debug': | |||
debug = True | |||
else: | else: | ||
pywikibot.output(u'Unknown argument: %s' % arg) | pywikibot.output(u'Unknown argument: %s' % arg) | ||
cat = catlib.Category(pywikibot.getSite(), 'Category:MediaWiki') | cat = catlib.Category(pywikibot.getSite("en", "wikiindex"), 'Category:MediaWiki') | ||
gen = pagegenerators.CategorizedPageGenerator(cat, start=start) | gen = pagegenerators.CategorizedPageGenerator(cat, start=start) | ||
Line 354: | Line 251: | ||
# pages from the wiki simultaneously. | # pages from the wiki simultaneously. | ||
gen = pagegenerators.PreloadingGenerator(gen) | gen = pagegenerators.PreloadingGenerator(gen) | ||
bot = WiSizeBot(gen, editSummary) | bot = WiSizeBot(gen, editSummary, debug) | ||
bot.run() | bot.run() | ||
else: | else: |
Latest revision as of 06:34, 25 August 2014