#!/usr/bin/env python

from xml.etree.ElementTree import parse
import sys, re, hashlib
from pysqlite2 import dbapi2 as sqlite
import htmlentitydefs

if len(sys.argv) <3:
  print "usage:"
  print "./pocket2tango gpxfile poidbfile"
  print "  gpxfile: a file in the geocaching.com gpx pocket query file format, containing cache information"
  print "  poidbfile: usually poi.db, or any other wellformed tangogps poi sqlite database file"
  sys.exit(1)

def unicode2html(str):
  """translate any unicode or upper ascii char by its html entity"""
  ret=""
  for char in str:
    if ord(char) > 127:
      try:
        ret=ret+"&"+htmlentitydefs.codepoint2name[ord(char)]+";"
      except:
        ret=ret+'?'
    else:
      ret=ret+char
  return ret

def escapeQuote(str):
  """escapes single quotes by double single quote, sqlite style"""
  return str.replace('\'', '\'\'');

gpx = open(sys.argv[1])
xml = parse(gpx).getroot()

con = sqlite.connect(sys.argv[2]) 
cur = con.cursor()

for wpt in xml:
  if wpt.tag=="{http://www.topografix.com/GPX/1/0}wpt":
    data = {} # dictionary holding all relevant data to be written into database
    # transfer lat, lon into dictionary directly
    for item in wpt.items():
      data[item[0]]=item[1]
    # transfer all text information from the groundspeak tags into dictionary
    for sub in wpt:
      if sub.tag=="{http://www.topografix.com/GPX/1/0}name":
        data['gcid']=sub.text
      if sub.tag=="{http://www.groundspeak.com/cache/1/0}cache":
        for item in sub.items():
          if item[0]=="available":
            data[item[0]]=item[1]
        for sub2 in sub:
          shorttag = sub2.tag[sub2.tag.index('}')+1:]
          data[shorttag]=sub2.text
    # build the query
    idmd5 = hashlib.md5(data['lon']+data['lat']).hexdigest()[0:18] #not the format tangogps uses, but what's an idmd5 anyway?
    summary = escapeQuote(data['container']+" "+data['type']+" "+data['difficulty']+"/"+data['terrain']+"\n"+data['encoded_hints'])
    if data["available"]=="False":
      summary = "---NOT AVAILABLE---\n"+summary
    query = "insert into poi (idmd5, lon, lat, keywords, visibility, cat, subcat, price_range, extended_open, desc) values ('"+idmd5+"', '"+data['lon']+"', '"+data['lat']+"', '"+data['gcid']+" "+escapeQuote(data['name'])+"', 0.0, 0.0, 0.0, 3.0, 0.0, '"+summary+"')" #tangogps crashes if some of these remain NULL. whatever.
    cur.execute(query)

    # optional step: write the long description into a text/html file in folder html/.
    html = open ("html/"+data['gcid']+".html", "w");
    html.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><title>""");
    html.write(unicode2html(data['name']))
    html.write("</title></head><body>")
    html.write(unicode2html(data['short_description']))
    html.write(unicode2html(data['long_description']))
    html.write("</body></html>")
    html.close()

con.commit()

