#!/usr/bin/python
# This script is part of the piwik project
# author : Johan Mathe <johan.mathe@tremplin-utc.net>
# Licence : GPL
# works with the following apache log config :
# LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{Cookie}i\"" combinedcookie-stressme

#TODO
#cookie
#headers IP;browser etc
#+ expliquer quels params changer pour duree visite

import getopt
import sys
import os
import time
import urllib2

class player:
  def __init__(self, logFile, hostName, delay = 0, verbose = False):
    self.logFile = logFile;
    self.hostName = hostName;
    self.delay = delay
    self.verbose = verbose
  def play(self):
    fd = open(self.logFile)
    for line in fd.readlines():
      fields = line.split(' ')
      uri = fields[6]
      url =  "http://%(w)s/%(u)s" % {'w' : self.hostName, 'u' : uri}; 
      if self.verbose:
        print url
      try:
        req = urllib2.Request(url)
        handle = urllib2.urlopen(req)
        the_page = handle.read()
      except IOError, e:
        if e.code == 404: 
          print "404 error : %s" % url
        else:
          print "Unknown error while opening URL %s" % url
      except KeyboardInterrupt:
        print "Keyboard interrupt"
        sys.exit()
      time.sleep(self.delay)

class Usage(Exception):
    def __init__(self, msg):
        self.msg = msg

def usage():
  print "--help : show this help\n--log : logfile\n--host : host name\n--delay : delay in sec"

def main(argv=None):
    hostName = ""
    logFile = ""
    verbose = False
    delay = 0
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "vhf:h:d:", ["help","log=","host=","delay="])
        except getopt.error, msg:
             raise Usage(msg)
        for o, a in opts:
          if o == "-v":
            verbose = True
          elif o in ("f", "--log"):
            logFile = a
          elif o in ("h", "--host"):
            hostName = a 
          elif o in ("-h", "--help"):
            usage()
            sys.exit()
          elif o in ("-d", "--delay"):
            delay = float(a)
          else:
            assert False, "unhandled option"
        if not hostName or not logFile:
          raise Usage("Plase specify the host name and the log file.")

        p = player(logFile, hostName, delay, verbose)
        p.play()
    except Usage, err:
        print >>sys.stderr, err.msg
        print >>sys.stderr, "for help use --help"
        return 2

if __name__ == "__main__":
    sys.exit(main())
