Utente:BimBot/Scripts: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
m →articoli_defaultsort.py: con l'asincrono lo script cannava tutti gli oggetti... fix |
Nessun oggetto della modifica |
||
Riga 500:
i.put_async(newtext, comment="Aggiungo: " + nuovoTitolo)
if __name__ == "__main__":
try:
main()
finally:
wikipedia.stopme()
</source>
== <tt>newsPedia.py</tt> ==
Prende le ultime notizie da 'news e le sbatte su 'pedia.
<source lang="python">
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wikipedia, urllib2, re
# Costanti
newsListUrl = 'http://it.wikinews.org/wiki/Utente:BimBot/RecentNews' # Prende qui le notizie
updatePage = 'Template:Pagina principale/Notizie/Auto' # Dove mette le notizie
def pageText(url):
request = urllib2.Request(url)
user_agent = 'BimBot/1.0'
request.add_header("User-Agent", user_agent)
response = urllib2.urlopen(request)
text = response.read()
response.close()
return text
def main():
args = wikipedia.handleArgs()
all = False
site = wikipedia.getSite("it", "wikipedia")
for currentArgument in args:
if currentArgument.startswith("-always"):
all = True
page = wikipedia.Page(site, updatePage)
wikipedia.output(">>>>> " + page.title() + " <<<<<")
try:
oldtext = page.get()
except wikipedia.NoPage:
oldtext = ''
try:
htmlText = pageText(newsListUrl)
except urllib2.HTTPError:
try:
wikipedia.output(u"Errore del server. Aspetto 10 secondi... " + time.strftime("%d %b %Y %H:%M:%S (UTC)", time.gmtime()) )
time.sleep(10)
htmlText = pageText(newsListUrl)
except urllib2.HTTPError:
wikipedia.output(u"Errore del server. Chiudo.")
return
divMatch = re.search('(?s)<div id="recentnews">(.*?)</div>', htmlText) # Prendo il blocco che mi interessa
news = re.findall('<li><a .*?>(.*?)</a></li>', divMatch.group(1)) # Array di titoli di notizie
newtext = ''
for i in news:
newtext = newtext + '[[n:' + i + '|' + i + ']] · ' # Formatto il link
newtext = newtext[:len(newtext)-3].decode('utf-8') # Tolgo il pallino alla fine e decodifico
wikipedia.showDiff(oldtext, newtext)
if not all:
choice = wikipedia.inputChoice(u"Aggiorno?", ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
else:
choice = 'y'
if choice in ['A', 'a']:
all = True
choice = 'y'
if choice in ['Y', 'y']:
wikipedia.setAction("Aggiorno notizie da Wikinews")
page.put(newtext)
if __name__ == "__main__":
try:
|