#!/usr/bin/env python

import htmllib, formatter, urllib, time, pyosd

class Parser(htmllib.HTMLParser):
  def __init__(self):
    htmllib.HTMLParser.__init__(self, formatter.NullFormatter())
    self.a = []
  def start_a(self, attrs):
    self.save_bgn()
  def end_a(self):
    self.a.append(self.save_end())
  def update(self):
    self.a = []
    while (self.a == []):
      try: self.feed(urllib.urlopen("http://linuxfr.org/news/archive/").read())
      except:
        print "aplouf... nouvel essai dans 20 secondes..."
        time.sleep(20)

def compare(a, b, tol):
  diff = a - b
  if (diff < 0): diff = -diff
  if (diff <= tol): return True
  else: return False

time_disp = 10
time_refresh = 30
tolerance = 7
display = pyosd.osd()
display.set_pos(pyosd.POS_TOP)
try:
  display.set_font("-adobe-helvetica-bold-r-normal-*-*-200-*-*-p-*-iso8859-1")
except: pass
display.set_timeout(time_disp)
display.set_colour("#FFFF00")

parser = Parser()
parser.update()
current = parser.a

while 1:

  time.sleep(time_refresh)
  parser.update()
  updated = parser.a
  modif = False

  while not compare(len(current), len(updated), tolerance):
    print "résultat suspect: pas de traitement."
    parser.update()
    current = parser.a
    updated = parser.a

  for x in updated:
    nouveau = True
    for y in current:
      if (x == y):
        nouveau = False
        break
    if (nouveau == True):
      print x
      display.display(x)
      time.sleep(time_disp)
      modif = True

  if (modif == True):
    current = updated
