#!/usr/bin/python
# -*- Mode: Python; tab-width: 4 -*-

""" ScanAltaCount
 Copyright (C) 1998 Luc Stepniewski

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 Scanneur de pages Ouebes Altavista
 # Chaine:
 # http://www.altavista.com/cgi-bin/query?pg=q&kl=XX&q=%2Bmicrosoft+%2Bwindows
 # http://www.altavista.com/cgi-bin/query?pg=q&kl=XX&q=linux
"""

__version__="1.00"

import string
import httplib
import urllib
import sys
import time
import re

def chomp(s):
    if s[-1:] == '\n': return s[:-1]
    else: return s

def recherche(s):
    # On transforme le ou les mots clefs en compatible HTTP
    s = urllib.quote_plus(s)
    
    h = httplib.HTTP('www.altavista.com')
    h.putrequest('GET', 'http://www.altavista.com/cgi-bin/query?pg=q&kl=XX&q=' + s)
    h.putheader('Accept', 'text/html')
    h.endheaders()
    errcode, errmsg, headers = h.getreply()
    if errcode == 200:
        f = h.getfile()
        data = f.readlines()
        f.close()
        
        prog = re.compile('.*AltaVista found about ([\d,]+).*')
        for ligne in data:
            result = prog.search(ligne)
            if result:
                return result.group(1)
                break
    return None
    
if __name__ == '__main__':
#    if len(sys.argv) < 2:
#        print 'Usage: ' + sys.argv[0] + ' word(s)'
#        sys.exit(1)
    
    while (1):
        resultat = recherche('linux')
        if resultat != None:
            resultat = string.join(string.split(resultat, ','), '')
            f = open("/usr/local/download/projets/compte/out.txt","a")
            t = time.localtime(time.time())
            ladate = '%d%02d%02d.%02d%02d%02d' % (t[0], t[1], t[2], t[3], t[4], t[5])
            f.write(ladate + ' ' + resultat + '\n')
            f.close()
        # Intervalle de 30 minutes
        time.sleep(60*30)