Progetto:Bot/Programmi in Python per i bot/ConteggioAutori.py
# -*- coding: utf-8 -*-
"""
Per lanciare il bot scrivere:
python ConteggioAutori.py totale
"""
#*************** INIZIO: PARAMETRI PER PROGETTI ***************
# per aggiungere un nuovo progetto basta aggiungerlo nella sezione qui sotto.
parametri = {
'totale':
{
'bot': ['Autori-A', 'Autori-B', 'Autori-C', 'Autori-D', 'Autori-E', 'Autori-F', 'Autori-G', 'Autori-H', 'Autori-I', 'Autori-J', 'Autori-K', 'Autori-L', 'Autori-M', 'Autori-N', 'Autori-O', 'Autori-P', 'Autori-Q', 'Autori-R', 'Autori-S', 'Autori-T', 'Autori-U', 'Autori-V', 'Autori-W', 'Autori-X', 'Autori-Y', 'Autori-Z', 'Template:Conteggio autori'],
},
}
#*************** FINE: PARAMETRI PER PROGETTI ***************
#
# (C) Rob W.W. Hooft, 2004
# (C) Daniel Herding, 2004
#
__version__ = '$Id: category.py 3998 2007-08-07 20:28:27Z wikipedian $'
#
# Distributed under the terms of the MIT license.
#
import pickle, bz2
import wikipedia, catlib
import datetime
class CategoryDatabase:
'''
This is a temporary knowledge base saving for each category the contained
subcategories and articles, so that category pages do not need to
be loaded over and over again
'''
def __init__(self, rebuild = False, filename = 'category.dump.bz2'):
if rebuild:
self.rebuild()
else:
try:
f = bz2.BZ2File(filename, 'r')
wikipedia.output(u'Reading dump from %s' % filename)
databases = pickle.load(f)
f.close()
# keys are categories, values are 2-tuples with lists as entries.
self.catContentDB = databases['catContentDB']
# like the above, but for supercategories
self.superclassDB = databases['superclassDB']
del databases
except:
# If something goes wrong, just rebuild the database
self.rebuild()
def rebuild(self):
self.catContentDB={}
self.superclassDB={}
def getSubcats(self, supercat):
'''
For a given supercategory, return a list of Categorys for all its
subcategories.
Saves this list in a temporary database so that it won't be loaded from the
server next time it's required.
'''
# if we already know which subcategories exist here
if self.catContentDB.has_key(supercat):
return self.catContentDB[supercat][0]
else:
subcatlist = supercat.subcategoriesList()
articlelist = supercat.articlesList()
# add to dictionary
self.catContentDB[supercat] = (subcatlist, articlelist)
return subcatlist
def getArticles(self, cat):
'''
For a given category, return a list of Pages for all its articles.
Saves this list in a temporary database so that it won't be loaded from the
server next time it's required.
'''
# if we already know which articles exist here
if self.catContentDB.has_key(cat):
return self.catContentDB[cat][1]
else:
subcatlist = cat.subcategoriesList()
articlelist = cat.articlesList()
# add to dictionary
self.catContentDB[cat] = (subcatlist, articlelist)
return articlelist
def getSupercats(self, subcat):
# if we already know which subcategories exist here
if self.superclassDB.has_key(subcat):
return self.superclassDB[subcat]
else:
supercatlist = subcat.supercategoriesList()
# add to dictionary
self.superclassDB[subcat] = supercatlist
return supercatlist
def dump(self, filename = 'category.dump.bz2'):
'''
Saves the contents of the dictionaries superclassDB and catContentDB to disk.
'''
wikipedia.output(u'Dumping to %s, please wait...' % filename)
f = bz2.BZ2File(filename, 'w')
databases = {
'catContentDB': self.catContentDB,
'superclassDB': self.superclassDB
}
# store dump to disk in binary format
try:
pickle.dump(databases, f, protocol=pickle.HIGHEST_PROTOCOL)
except pickle.PicklingError:
pass
f.close()
class CreateTableRobot:
'''
Creates a table.
'''
table_msg={
'en':u'Updating number of elements (%d)',
'it':u'Aggiornamento conteggio (%d)',
}
def __init__(self, infoPagine, editSummary, overwrite = True, subCats = False, talkPages = False):
catTitle1, catTitle2, catTitle3, catTitle4, catTitle5, catTitle6, catTitle7, catTitle8, catTitle9, catTitle10, catTitle11, catTitle12, catTitle13, catTitle14, catTitle15, catTitle16, catTitle17, catTitle18, catTitle19, catTitle20, catTitle21, catTitle22, catTitle23, catTitle24, catTitle25, catTitle26, listTitle = infoPagine
self.editSummary = editSummary
self.overwrite = overwrite
self.catName1 = catTitle1
self.catName2 = catTitle2
self.catName3 = catTitle3
self.catName4 = catTitle4
self.catName5 = catTitle5
self.catName6 = catTitle6
self.catName7 = catTitle7
self.catName8 = catTitle8
self.catName9 = catTitle9
self.catName10 = catTitle10
self.catName11 = catTitle11
self.catName12 = catTitle12
self.catName13 = catTitle13
self.catName14 = catTitle14
self.catName15 = catTitle15
self.catName16 = catTitle16
self.catName17 = catTitle17
self.catName18 = catTitle18
self.catName19 = catTitle19
self.catName20 = catTitle20
self.catName21 = catTitle21
self.catName22 = catTitle22
self.catName23 = catTitle23
self.catName24 = catTitle24
self.catName25 = catTitle25
self.catName26 = catTitle26
self.cat1 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle1)
self.cat2 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle2)
self.cat3 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle3)
self.cat4 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle4)
self.cat5 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle5)
self.cat6 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle6)
self.cat7 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle7)
self.cat8 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle8)
self.cat9 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle9)
self.cat10 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle10)
self.cat11 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle11)
self.cat12 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle12)
self.cat13 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle13)
self.cat14 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle14)
self.cat15 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle15)
self.cat16 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle16)
self.cat17 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle17)
self.cat18 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle18)
self.cat19 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle19)
self.cat20 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle20)
self.cat21 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle21)
self.cat22 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle22)
self.cat23 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle23)
self.cat24 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle24)
self.cat25 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle25)
self.cat26 = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle26)
self.list = wikipedia.Page(wikipedia.getSite(), listTitle)
self.subCats = subCats
self.talkPages = talkPages
def run(self):
nomeCat1 = self.catName1
nomeCat2 = self.catName2
nomeCat3 = self.catName3
nomeCat4 = self.catName4
nomeCat5 = self.catName5
nomeCat6 = self.catName6
nomeCat7 = self.catName7
nomeCat8 = self.catName8
nomeCat9 = self.catName9
nomeCat10 = self.catName10
nomeCat11 = self.catName11
nomeCat12 = self.catName12
nomeCat13 = self.catName13
nomeCat14 = self.catName14
nomeCat15 = self.catName15
nomeCat16 = self.catName16
nomeCat17 = self.catName17
nomeCat18 = self.catName18
nomeCat19 = self.catName19
nomeCat20 = self.catName20
nomeCat21 = self.catName21
nomeCat22 = self.catName22
nomeCat23 = self.catName23
nomeCat24 = self.catName24
nomeCat25 = self.catName25
nomeCat26 = self.catName26
listOfArticles1 = self.cat1.articlesList()
listOfArticles2 = self.cat2.articlesList()
listOfArticles3 = self.cat3.articlesList()
listOfArticles4 = self.cat4.articlesList()
listOfArticles5 = self.cat5.articlesList()
listOfArticles6 = self.cat6.articlesList()
listOfArticles7 = self.cat7.articlesList()
listOfArticles8 = self.cat8.articlesList()
listOfArticles9 = self.cat9.articlesList()
listOfArticles10 = self.cat10.articlesList()
listOfArticles11 = self.cat11.articlesList()
listOfArticles12 = self.cat12.articlesList()
listOfArticles13 = self.cat13.articlesList()
listOfArticles14 = self.cat14.articlesList()
listOfArticles15 = self.cat15.articlesList()
listOfArticles16 = self.cat16.articlesList()
listOfArticles17 = self.cat17.articlesList()
listOfArticles18 = self.cat18.articlesList()
listOfArticles19 = self.cat19.articlesList()
listOfArticles20 = self.cat20.articlesList()
listOfArticles21 = self.cat21.articlesList()
listOfArticles22 = self.cat22.articlesList()
listOfArticles23 = self.cat23.articlesList()
listOfArticles24 = self.cat24.articlesList()
listOfArticles25 = self.cat25.articlesList()
listOfArticles26 = self.cat26.articlesList()
totale1 = len(listOfArticles1)
totale2 = len(listOfArticles2)
totale3 = len(listOfArticles3)
totale4 = len(listOfArticles4)
totale5 = len(listOfArticles5)
totale6 = len(listOfArticles6)
totale7 = len(listOfArticles7)
totale8 = len(listOfArticles8)
totale9 = len(listOfArticles9)
totale10 = len(listOfArticles10)
totale11 = len(listOfArticles11)
totale12 = len(listOfArticles12)
totale13 = len(listOfArticles13)
totale14 = len(listOfArticles14)
totale15 = len(listOfArticles15)
totale16 = len(listOfArticles16)
totale17 = len(listOfArticles17)
totale18 = len(listOfArticles18)
totale19 = len(listOfArticles19)
totale20 = len(listOfArticles20)
totale21 = len(listOfArticles21)
totale22 = len(listOfArticles22)
totale23 = len(listOfArticles23)
totale24 = len(listOfArticles24)
totale25 = len(listOfArticles25)
totale26 = len(listOfArticles26)
totaleTotale = totale1 + totale2 + totale3 + totale4 + totale5 + totale6 + totale7 + totale8 + totale9 + totale10 + totale11 + totale12 + totale13 + totale14 + totale15 + totale16 + totale17 + totale18 + totale19 + totale20 + totale21 + totale22 + totale23 + totale24 + totale25 + totale26
if self.editSummary:
wikipedia.setAction(self.editSummary)
else:
wikipedia.setAction(wikipedia.translate(wikipedia.getSite(), self.table_msg) % (totaleTotale))
paginaTemplate = \
u"""<noinclude>{{Lista Bot}}</noinclude>*TOTALE*<noinclude>__NOEDITSECTION__
<div class="toccolours" style="clear:both; margin-bottom: 0.5em; font-size: 90%;">
=== Descrizione ===
Inserisce il numero di autori pubblicati su Wikisource.
=== Uso ===
Ricopiare il testo sottostante.
: '''<nowiki>{{</nowiki>{{PAGENAME}}<nowiki>}}</nowiki>'''
</div>[[Categoria:Template etichetta per i portali|{{PAGENAME}}]][[Categoria:Template per il portale autori|{{PAGENAME}}]]</noinclude>"""
if self.list.exists() and not self.overwrite:
wikipedia.output(u'Page %s already exists, aborting.' % self.list.title())
else:
listString = "%d" % (totaleTotale)
pagina = paginaTemplate.replace("*TOTALE*", listString)
self.list.put(pagina)
def listaProgetti():
"""Visualizza la lista dei progetti conosciuti dal bot"""
print "\n\nDi seguito sono elencati i parametri da aggiungere per generare i conteggi:\n"
for progetto in sorted(parametri.keys()):
print " " + progetto
print "\nPer generare le statistiche usare la forma \"python conteggi.py PARAMETRO\"\n"
def helpExit(avviso = ""):
print "** " + avviso + " **"
#wikipedia.showHelp('gestione_elenchi_SAL')
print __doc__
import sys
sys.exit(1)
if __name__ == "__main__":
editSummary = ''
overwrite = True
talkPages = False
try:
catDB = CategoryDatabase()
action = None
restore = False
for arg in wikipedia.handleArgs():
if arg in parametri:
action = arg
elif arg == 'listaProgetti':
listaProgetti()
import sys
sys.exit()
elif arg.startswith('-summary:'):
editSummary = arg[len('-summary:'):]
else:
helpExit("Non conosco il progetto " + arg)
if action is None:
helpExit(u"Non c'è il parametro che indica il progetto")
bot = CreateTableRobot(parametri[action]['bot'], editSummary, overwrite, subCats = False, talkPages = False)
bot.run()
finally:
catDB.dump()
[visualizza] [modifica] Documentazione
Questa documentazione è transclusa da Progetto:Bot/Programmi in Python per i bot/ConteggioAutori.py/doc. (modifica | cronologia)
Descrizione
Questo programma permette di avere in un template il template con il numero di autori pubblicati. In questo modo è possibile inserire tale dato in ogni pagina serva con estrema facilità.
Quando usarlo
Il programma viene lanciato ogni notte in modo automatico.
Può essere lanciato all'occorrenza quando si è certi che siano stati aggiunti/rimossi degli autori.
Nome del file
ConteggioAutori.py
Comando
- python ConteggioAutori.py totale