#
# OperaToXBEL v0.0.1.1 (development version)
# Copyright 2001 Eddy L O Jansson - http://gazonk.org/~eloj/
#
#  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
import xreadlines
import string
import time
#import urllib

version = "0.0.1.1"

#
# We don't take any options yet ...
#
source = "Opera5.adr"
dest = "Opera5.xml"
tz = "+01:00"

def unixToISO(datetime):
    return time.strftime("%Y-%m-%dT%H:%M:%S"+tz,time.gmtime(long(datetime)))

def padXML():
    return string.rjust("",depth*2)

def doFolder():
  folder = {}
  while 1:
    s = adr.pop(0)
    if s == "\n":
      break
    else:
      pair = string.split(s,'=',2)
      name = string.strip(pair[0])
      value = string.strip(pair[1])
      if len(value) > 0:
        folder[name] = value # urllib.quote(value,": /~")
  return folder

def doURL():
  url = {}
  while 1:
    s = adr.pop(0)
    if s == "\n":
      break
    else:
      pair = string.split(s,'=',2)
      name = string.strip(pair[0])
      value = string.strip(pair[1])
      if len(value) > 0:
        url[name] = value # urllib.quote(value,": /~")
  return url

print "Opera2XBEL v"+version+" (development version)"
print "Copyright 2001 Eddy L O Jansson - http://gazonk.org/~eloj/"
print ""
print "This is free software released under the GNU GPL. See the file LICENSE"
print ""
print "Source file: " + source
print "Output file: " + dest
print ""

print "Opening files ...",
fin = open(source,'r')
fout = open(dest,'w')
print "Ok."

fout.write("<?xml version=\"1.0\"?>\n")
# output DTD
fout.write("<!DOCTYPE xbel PUBLIC \"+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML\" \"http://www.python.org/topics/xml/dtds/xbel-1.0.dtd\">\n\n")

fout.write("<xbel>\n")
# Include original header as title element.
s = fin.readline()
fout.write("<title>"+s[:len(s)-1]+"</title>\n")
fout.write("<desc>Opera bookmarks converted by OperaToXBEL v"+version+"</desc>\n\n")

print "Reading source file into list ...",
adr = fin.readlines()
print "Ok."

print "Converting ...",

depth = 0
while len(adr) > 1:
  s = adr.pop(0)
  if s == "#FOLDER\n":
    depth = depth + 1
    afolder = doFolder()

    if afolder.has_key("EXPANDED"):
      if afolder["EXPANDED"] == "YES":
        afolded=" folded=\"no\""
      else:
        afolded=" folded=\"yes\""
    else:
      afolded=" folded=\"yes\""  # Default value

    adate = ""
    if afolder.has_key("CREATED"):
      if afolder["CREATED"] > 0 : adate = " added=\""+unixToISO(afolder["CREATED"])+"\""
    fout.write(padXML()+"<folder" + adate + afolded + ">\n")
    fout.write(padXML()+"  <title>" + afolder["NAME"] + "</title>\n")
    if afolder.has_key("DESCRIPTION"):
      fout.write(padXML()+"  <desc>" + afolder["DESCRIPTION"] + "</desc>\n")
    continue

  if s == "#URL\n":
    depth = depth + 1
    aurl = doURL()

    adate = ""
    if afolder.has_key("CREATED"):
      if aurl["CREATED"] > 0 : adate = " added=\""+unixToISO(aurl["CREATED"])+"\""

    vdate = ""
    if afolder.has_key("VISITED"):
      if aurl["VISITED"] > 0 : vdate = " visited=\""+unixToISO(aurl["VISITED"])+"\""

    fout.write(padXML()+"<bookmark href=\""+aurl["URL"]+"\""+adate+vdate+">")
    fout.write("<title>" + aurl["NAME"] + "</title>")
    if aurl.has_key("DESCRIPTION"):
      fout.write("<desc>" + aurl["DESCRIPTION"] + "</desc>")
    fout.write("</bookmark>\n")
    depth = depth - 1
    continue

  if s == "-\n":
    fout.write(padXML()+"</folder>\n\n")
    depth = depth - 1
#...while

fout.write("</xbel>\n")
print "Ok."

fout.close()
fin.close()

print "All done."