Progetto:Bot/Programmi in Python per i bot/ListeTestiSAL.py: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
IPork (discussione | contributi)
mNessun oggetto della modifica
snellito e migliorato
Riga 208:
'bot6': ['Testi SAL 25%25 Letteratura', 'Testi SAL 50%25 Letteratura', 'Testi SAL 75%25 Letteratura', 'Testi SAL 100%25 Letteratura', 'Testi SAL 101%25 Letteratura', 'Template:Conteggio testi SAL letteratura']
},
# BESTIARI SAL 2.0
'bestiari': {
'bot1': ['bestiari', 'Testi SAL 25%25 Bestiari', 'Wikisource:Elenco bestiari SAL 25%25'],
'bot2': ['bestiari', 'Testi SAL 50%25 Bestiari', 'Wikisource:Elenco bestiari SAL 50%25'],
'bot3': ['bestiari', 'Testi SAL 75%25 Bestiari', 'Wikisource:Elenco bestiari SAL 75%25'],
'bot4': ['bestiari', 'Testi SAL 100%25 Bestiari', 'Wikisource:Elenco bestiari SAL 100%25'],
'bot5': ['bestiari', 'Testi SAL 101%25 Bestiari', 'Wikisource:Elenco bestiari Edizioni Wikisource'],
'bot6': ['Testi SAL 25%25 Bestiari', 'Testi SAL 50%25 Bestiari', 'Testi SAL 75%25 Bestiari', 'Testi SAL 100%25 Bestiari', 'Testi SAL 101%25 Bestiari', 'Template:Conteggio testi SAL bestiari']
},
# BIOGRAFIE SAL 2.0
'biografie': {
Line 522 ⟶ 513:
'bot5': ['satira', 'Testi SAL 101%25 Satira', 'Wikisource:Elenco satira Edizioni Wikisource'],
'bot6': ['Testi SAL 25%25 Satira', 'Testi SAL 50%25 Satira', 'Testi SAL 75%25 Satira', 'Testi SAL 100%25 Satira', 'Testi SAL 101%25 Satira', 'Template:Conteggio testi SAL satira']
},
# Storia dell'arte SAL 2.0
'storia_dellarte': {
'bot1': ['testi di storia dell\'arte', 'Testi SAL 25%25 Storia dell\'arte', 'Wikisource:Elenco testi di storia dell\'arte SAL 25%25'],
'bot2': ['testi di storia dell\'arte', 'Testi SAL 50%25 Storia dell\'arte', 'Wikisource:Elenco testi di storia dell\'arte SAL 50%25'],
'bot3': ['testi di storia dell\'arte', 'Testi SAL 75%25 Storia dell\'arte', 'Wikisource:Elenco testi di storia dell\'arte SAL 75%25'],
'bot4': ['testi di storia dell\'arte', 'Testi SAL 100%25 Storia dell\'arte', 'Wikisource:Elenco testi di storia dell\'arte SAL 100%25'],
'bot5': ['testi di storia dell\'arte', 'Testi SAL 101%25 Storia dell\'arte', 'Wikisource:Elenco testi di storia dell\'arte Edizioni Wikisource'],
'bot6': ['Testi SAL 25%25 Storia dell\'arte', 'Testi SAL 50%25 Storia dell\'arte', 'Testi SAL 75%25 Storia dell\'arte', 'Testi SAL 100%25 Storia dell\'arte', 'Testi SAL 101%25 Storia dell\'arte', 'Template:Conteggio testi SAL storia dell\'arte']
},
# Filosofia SAL 2.0
Line 843 ⟶ 825:
# 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 CategoryListifyRobot:
Line 1 077 ⟶ 969:
 
try:
catDB = CategoryDatabase()
action = None
restore = False
Line 1 111 ⟶ 1 002:
bot6.run()
finally:
catDBwikipedia.dumpstopme()
</source><noinclude>
{{Documentazione}}