Différences entre versions de « Utilisateur:RimBot/count-quotes.py »

mise à jour pour la dernière version de pywikibot
m
(mise à jour pour la dernière version de pywikibot)
"""
 
import wikipedia, pagegenerators, catlib, configpywikibot
import sys, re, string, time, locale, operator
 
msg = {
'fr': (u'<h1 style="border-bottom:none;margin-top:0;margin-bottom:0.8em;text-align:center;"> %d citations dans %d articles ',
u'<small>(Le nombre d\'articles n\'inclut que les articles qui possÃpossèdent au moins une citation. Les citations en plusieurs exemplaires ne sont comptÃcomptées qu\'une fois.)',
u'== Répartion par article ==\n\n',
u'{| class="wikitable"\n|+\'\'\'Nombre de citations par article\'\'\'\n! align="center" | Article\n! align="center" | Nombre de citations\n',
u'|- align="center" \n| \'\'\'Moyenne\'\'\' || %0.2f\n',
u'<small>(Ce tableau recense le nombre de citations qui figurent dans plusieurs articles. Ainsi, si le premier champ vaut N et le second M, cela signifie que M citations figurent en N exemplaires (dans N articles))</small>\n\n',
u'{| class="wikitable" style="width: 40%%"\n|+\'\'\'Nombre d\'exemplaires de la citation\'\'\'\n! align="center" | Article\n! align="center" | Nombre de citations\n',
u'Mise ÃÃ jour des statistiques'),
'is': (u'<h1 style="border-bottom:none;margin-top:0;margin-bottom:0.8em;text-align:center;"> %d tilvitnanir Ãí %d greinum ',
u'<small>(ÃÍ fjÃfjölda greina eru greinar meÃmeð engum tilvitnunum ekki taldar meÃmeð. TvÃTvÃÂfaldaÃfaldaðar tilvitnanir eru taldar sem ein.)',
u'== Eftir greinum ==\n\n',
u'{| class="wikitable"\n|+\'\'\'FjÃFjöldi tilvitnana eftir greinum\'\'\'\n! align="center" | Grein\n! align="center" | FjÃFjöldi tilvitna\n',
u'|- align="center" \n| \'\'\'MeÃMeðaltal\'\'\' || %0.2f\n',
u'<small>MeÃMeðaltaliÃaltalið telur ekki meÃmeð greinar sem innihalda engar tilvitnanir.</small>\n\n',
u'== TvÃTvöfaldar tilvitnanir ==\n',
u'<small>(ÞessiÞessi tafla sýnir fjÃfjölda tilvitnana sem koma fram Ãí fleirum en einni grein. If the first field is N and the second M, it means M quotes appear N times (in N articles))</small>\n\n',
u'{| class="wikitable" style="width: 40%%"\n|+\'\'\'FjÃFjöldi greina sem tilvitnunin kemur fyrir\'\'\n! align="center" | Greinar\n! align="center" | FjÃFjöldi tilvitnana\n',
u'Updated statistics'),
'en': (u'<h1 style="border-bottom:none;margin-top:0;margin-bottom:0.8em;text-align:center;"> %d quotes in %d articles ',
def outputq(s, newline = True):
if not globalvar.quiet:
wikipediapywikibot.output(u'%s' % s, newline = newline)
 
def debug(s):
if globalvar.debug:
wikipediapywikibot.output(u'%s' % s)
 
def error(s):
# Output errors to ... stdout, because pywikipedia uses stderr for "normal"
# output. *sigh*
wikipediapywikibot.output(u'ERROR: %s' % s, toStdout = True)
 
def parse_templates(text):
try:
text = page.get()
except wikipedia.IsRedirectPage:
return
 
text = prepare_text(text)
 
if not wikipediapywikibot.getSite().nocapitalize:
pattern = '[' + re.escape(globalvar.quote_template[0].upper()) + re.escape(globalvar.quote_template[0].lower()) + ']' + re.escape(globalvar.quote_template[1:])
else:
# locale.setlocale(locale.LC_ALL, "fr_FR@euro")
# for l in sorted(acount.items(), lambda x,y: locale.strcoll(x[0].title().lower(), y[0].title().lower())):
# wikipediapywikibot.output(u'%s' % l[0].title())
 
# Sort by number of quotes (return list of tuples, not dict)
# Parse options
 
for arg in wikipediapywikibot.handleArgs():
if arg.startswith('-ref'):
if len(arg) == 4:
self.__refpagetitle = wikipediapywikibot.input(u'Links to which page should be processed?')
else:
self.__refpagetitle = arg[5:]
elif arg.startswith('-cat'):
if len(arg) == 4:
self.__catname = wikipediapywikibot.input(u'Please enter the category name:');
else:
self.__catname = arg[5:]
elif arg.startswith('-links'):
if len(arg) == 6:
self.__linkpagetitle = wikipediapywikibot.input(u'Links from which page should be processed?')
else:
self.__linkpagetitle = arg[7:]
elif arg.startswith('-file:'):
if len(arg) == 5:
self.__textfile = wikipediapywikibot.input(u'File to read pages from?')
else:
self.__textfile = arg[6:]
elif arg.startswith('-start:'):
if len(arg) == 6:
self.__start = wikipediapywikibot.input(u'Which page to start from: ')
else:
self.__start = arg[7:]
elif arg.startswith('-number:'):
if len(arg) == 7:
self.__number = int(wikipediapywikibot.input(u'Number of pages to parse: '))
else:
self.__number = int(arg[8:])
elif arg.startswith('-output:'):
if len(arg) == 7:
self.__output = wikipediapywikibot.input(u'Which page to save report to: ')
else:
self.__output = arg[8:]
elif arg.startswith('-outputprefix:'):
if len(arg) == 12:
self.__outputprefix = wikipediapywikibot.input(u'Which page to save templates to: ')
else:
self.__outputprefix = arg[13:]
elif arg.startswith('-outputquotes:'):
if len(arg) == 12:
self.__outputquotes = wikipediapywikibot.input(u'Which page to save number of quotes to: ')
else:
self.__outputquotes = arg[13:]
elif arg.startswith('-outputarticles:'):
if len(arg) == 14:
self.__outputarticles = wikipediapywikibot.input(u'Which page to save number of quotes to: ')
else:
self.__outputarticles = arg[15:]
elif arg.startswith('-template:'):
if len(arg) == 9:
globalvar.quote_template = wikipediapywikibot.input(u'Which template is used for quotes?')
else:
globalvar.quote_template = arg[10:]
elif arg.startswith('-arg:'):
if len(arg) == 4:
globalvar.quote_arg = wikipediapywikibot.input(u'Which template arg is used for quotes?')
else:
globalvar.quote_arg = arg[5:]
elif self.__refpagetitle:
refpage = wikipediapywikibot.Page(wikipediapywikibot.getSite(), self.__refpagetitle)
pagegen = pagegenerators.ReferringPageGenerator(refpage)
 
elif self.__linkpagetitle:
linkpage = wikipediapywikibot.Page(wikipediapywikibot.getSite(), self.__linkpagetitle)
pagegen = pagegenerators.LinkedPageGenerator(linkpage)
 
elif self.__catname:
cat = catlib.Category(wikipediapywikibot.getSite(), 'Category:%s' % self.__catname)
 
if self.__start:
if not self.__start:
self.__start = '!'
namespace = wikipediapywikibot.Page(wikipediapywikibot.getSite(), self.__start).namespace()
start = wikipediapywikibot.Page(wikipediapywikibot.getSite(), self.__start).titleWithoutNamespacetitle(withNamespace=False)
try:
 
pagegen = pagegenerators.AllpagesPageGenerator(start, namespace, includeredirects=False)
 
return pagegen
 
def main(self):
# wikipedia.activateLog('count-quotes.log')
wikipedia.setLogfileStatus(True, 'count_quotes.log')
 
# Parse command line options
self.parse()
except:
site = wikipediapywikibot.getSite()
 
# ensure that we don't try to change main page
globalvar.mainpagename = pywikibot.Page(pywikibot.Link("Main Page", site)).title(withNamespace=False)
try:
site = wikipedia.getSite()
globalvar.mainpagename = site.family.mainpages[site.language()]
except:
outputq(u'Missing main page name')
 
if site.language() in msg:
for p in self.__pagetitles:
try:
pages.append(wikipediapywikibot.Page(wikipediapywikibot.getSite(), p))
except wikipediapywikibot.NoPage: pass
generator = pagegenerators.PreloadingGenerator(iter(pages))
else:
if self.__output:
try:
outputpage = wikipediapywikibot.Page(site, self.__output)
if outputpage.exists():
oldtext = outputpage.get()
error(u'Getting/Modifying page %s failed, generated output was:\n%s' % (outputpage, output))
else:
wikipediapywikibot.output(output)
 
nquotespage = None
else:
pname = "NUMBEROFQUOTES"
nquotespage = wikipediapywikibot.Page(site, u'%s%s' % (self.__outputprefix, pname))
 
if self.__outputarticles:
else:
pname = "NUMBEROFARTICLES"
narticlespage = wikipediapywikibot.Page(site, u'%s%s' % (self.__outputprefix, pname))
else:
if self.__outputquotes:
nquotespage = wikipediapywikibot.Page(site, u'%s' % self.__outputquotes)
if self.__outputarticles:
narticlespage = wikipediapywikibot.Page(site, u'%s' % self.__outputarticles)
 
if nquotespage:
Main().main()
finally:
wikipediapywikibot.stopme()
</source>
3 943

modifications