#! /usr/local/bin/python -O


#Subject: Netscape history file loader (undumper)
#Author: Oleg Broytmann <phd2@earthling.net>


import sys, string, time, struct, bsddb


if sys.stdin.isatty():
   sys.stderr.write("Usage: %s < infile\n" % sys.argv[0])
   sys.exit(1)


def parse_time(s):
   t = list(time.strptime(s, "%Y-%m-%d %T"))
   t[8] = -1 # Reset is_dst to undefined - mktime will find the value
   return time.mktime(t)

def put(line):
   fields = string.split(line, '\t')
   if len(fields) <> 6:
      raise RuntimeError, "wrong data format"

   try:
      key = fields[0] + '\0'
      t1 = parse_time(fields[1])
      t2 = parse_time(fields[2])
      v1 = int(fields[3])
      v2 = int(fields[4])
      v3 = fields[5]

   except:
      from traceback import print_exc
      print_exc()
      import sys
      sys.exit()

   history[key] = struct.pack("<llll", t1, t2, v1, v2) + v3


history = bsddb.hashopen("history.dat", 'n')

while 1:
   line = sys.stdin.readline()
   if not line: break
   put(line[:-1])

history.close()
