Showing with 31 additions and 7 deletions.
  1. +31 −7 script/spazzolone.py
38 changes: 31 additions & 7 deletions script/spazzolone.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
sys.exit("Necessito di un interprete Python dalla versione 2.6 in poi")

try:
import atexit, csv, datetime, glob, multiprocessing, os, socket, sys, smtplib, syslog, tempfile, time
import atexit, csv, datetime, glob, multiprocessing, os, socket, sys, smtplib, syslog, tempfile, time, urllib2
except:
sys.exit("Non sono disponibili tutti i moduli standard necessari")

Expand All @@ -42,6 +42,11 @@
except:
sys.exit("Installa mechanize: 'easy_install mechanize' oppure 'apt-get install python-mechanize'")

try:
import BeautifulSoup
except:
sys.exit("Installa beautifulsoup: 'easy_install beautifulsoup' oppure 'apt-get install python-beautifulsoup'")

try:
import cPickle as pickle
except:
Expand Down Expand Up @@ -213,7 +218,25 @@ def controllo_homepage(self):
self._v_browser.addheaders = [('User-agent', 'Bot: http://lugmap.linux.it - lugmap@linux.it')]

try:
self._v_Termini_Attuali = set(self._v_browser.open(self.url).read().split()) # Estrapolo parole
if self.id == 'Blug':
# Questo terribile hack/eccezione si è reseo necessario perché Mechanize
# resta appesa nel tentativo di connessione al sito del Blug.
# Mechanize è l'unico package del genere che segue automaticamente anche
# i refresh nei tag (roba non standard, ma pesantemente usata da quasi tutti
# i wiki. -- Il problema è gia' stato segnalato all'autore.)

self._v_richiesta = urllib2.Request(self.url,None, {"User-Agent":"Bot: http://lugmap.linux.it - lugmap@linux.it"})
self._v_pagina_html = urllib2.urlopen(self._v_richiesta).read()
self._v_Termini_Attuali = set(self._v_pagina_html.split())

# estraiamo subito anche il title
try:
self._v_soup = BeautifulSoup.BeautifulSoup(self._v_pagina_html)
self._v_titolo_attuale = self._v_soup.html.head.title.string.strip()
except: # se non ho un title, lo setto vuoto
self._v_titolo_attuale = ''
else:
self._v_Termini_Attuali = set(self._v_browser.open(self.url).read().split())
except:
self.notifica('Errore web: impossibile leggere homepage')

Expand All @@ -240,7 +263,7 @@ def controllo_homepage(self):

if self.web_errore_segnalato is not False:
self.notifica("Precedente errore WEB del " + time.strftime('%d/%m/%y', time.gmtime(self.web_errore_segnalato)) + ' risolto')
self.dns_errore_segnalato = False
self.web_errore_segnalato = False

return True

Expand All @@ -249,10 +272,11 @@ def controllo_title(self):

logga('Lug <'+self.id+'>: controllo title per '+self.url)

try:
self._v_titolo_attuale = self.browser.title().encode('utf-8')
except: # se non ho un title, mollo
return True
if not hasattr(self, '_v_titolo_attuale'): # se non è stato gia' settato dall'eccezione Blug (vedi sopra)
try: # estrapolo il titolo della pagina nella maniera usuale
self._v_titolo_attuale = self.browser.title().encode('utf-8')
except: # se non ho un title, mollo
return True

try:
if self.title_homepage != self._v_titolo_attuale:
Expand Down