# $Id: traffic.py,v 1.10 2010/04/22 16:23:00 slobin Exp $

import os, re, time, urllib2

def translate(chunk, src, dst):
    try:
        return chunk.decode(src).encode(dst)
    except UnicodeError:
        return chunk

def write_log_file(*args):
    print >> logfile, time.ctime(), " ".join(args)

pattern = re.compile(r'\<span\s+class\=\"b\-probki__title[^"]*\"\>(.*?)\<\/?span[^>]*\>')

os.chdir(os.path.expanduser("~/public_html/traffic"))
logfile = open(time.strftime("%Y-%m.txt"), "a")

try:
    link = urllib2.urlopen("http://www.yandex.ru/")
    page = link.read()
except Exception, err:
    write_log_file("ERR:", str(err))
else:
    if False:
        f = open("debug/" + time.strftime("%Y%m%d%H%M.html"), "wt")
        f.write(page)
        f.close()
    charset = link.info().getparam("charset")
    match = pattern.search(page)
    if match:
        words = re.split(r"\<.*?\>", match.group(1))
        value = " ".join(filter(bool, words))
        value = translate(value, charset, "cp1251")
        timestamp = time.strftime("%H:%M")
        write_log_file(value, timestamp)
    else:
        write_log_file("ERR", "ERR")
