# DEPRECATED as of July 2007 - use generate-xml-gal.py instead if possible

#     This file is part of the XML Photo Gallery Project
#     by Philip J. Guo (Created August 2005)
#     Copyright 2005 Philip J. Guo
#     http://alum.mit.edu/www/pgbovine/
#
#       based on the People Photo Gallery Project
#       by Philip J. Guo (Created January 2005)
#       Copyright 2005 Philip J. Guo
#
#     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

# This script converts the XML files generated by Picasa 2
# (which are bogus because it doesn't enclose captions
#  in <![CDATA[ ... ]]> tags so characters like '&' break the parser)
# into my own gallery XML format which conforms to gallery.dtd

# Usage: picasa_xml_converter.py input-filename output-filename galleryName folderName
# where input-filename is a Picasa-generated XML file,
# output-filename is the gallery XML filename,
# galleryName is the name for the gallery (must be quoted if there are spaces in it),
# and folderName is the folder where the gallery is located

from xml.dom import minidom
import re
import sys

picasa_fname = sys.argv[1];
gallery_fname = sys.argv[2];
galleryName = sys.argv[3];
folderName = sys.argv[4];

# Load the Picasa-generated XML file
f = open(picasa_fname, 'r');
out_file = open(gallery_fname, 'w');

# First, we must read in the Picasa-generated XML file and
# convert every "<itemCaption>" into "<itemCaption><![CDATA["
# and every "</itemCaption>" into "]]></itemCaption>"
# in order to make it a well-formed XML string
# Picasa also generates "\x93" for starting quotes " and "\x94" for ending quotes, which is weird
xml_string = "".join(f.readlines());

f.close();

CDATA_HEADER = "<![CDATA[";
CDATA_FOOTER = "]]>";

xml_string_corrected = xml_string.replace("\x93", '"').replace("\x94", '"').replace("<itemCaption>", "<itemCaption>" + CDATA_HEADER).replace("</itemCaption>", CDATA_FOOTER + "</itemCaption>");

# Now feed xml_string_corrected into the DOM parser
picasa_xml = minidom.parseString(xml_string_corrected);

# Now perform the magic of converting picasa_xml into a valid gallery XML file
# which conforms to gallery.dtd

out_file.write('<?xml version="1.0" encoding="ISO-8859-1"?>\n<!DOCTYPE gallery SYSTEM "gallery.dtd">\n\n');
out_file.write('<gallery>\n\n');

out_file.write('<galleryName>' + CDATA_HEADER + galleryName + CDATA_FOOTER + '</galleryName>\n');
out_file.write('<folderName>' + folderName + '</folderName>\n\n');
out_file.write('<images>\n\n');

imageNodes = picasa_xml.getElementsByTagName("image");

for image in imageNodes:
    # itemLargeImage looks like: "images/my_pic.jpg" so we need to get rid of "images/"
    filename = image.getElementsByTagName("itemLargeImage")[0].firstChild.data.strip().replace("images/", "");
    width = image.getElementsByTagName("itemWidth")[0].firstChild.data.strip();
    height = image.getElementsByTagName("itemHeight")[0].firstChild.data.strip();

# Don't worry about grabbing captions for now, because Picasa generates
# captions that are just the filename if the user doesn't enter in a custom
# caption, so that's way lame because it prevents the inline caching of
# people_gal_config.htm from working properly.
#
#    itemCaptionNodes = image.getElementsByTagName("itemCaption");
#    if (itemCaptionNodes.length and itemCaptionNodes[0].firstChild):
#        description = itemCaptionNodes[0].firstChild.data.strip();
#    else:
#        description = "";

    out_file.write('<image>\n<filename>' + filename + '</filename>\n');
    out_file.write('<title>' + CDATA_HEADER + "Untitled Image" + CDATA_FOOTER + '</title>\n');
#    out_file.write('<description>' + CDATA_HEADER + description + CDATA_FOOTER + '</description>\n');

# Just print out a blank description entry for now:
    out_file.write('<description></description>\n');
    out_file.write('<people>\n</people>\n<keywords>\n</keywords>\n');
    out_file.write('<date>\n<month>0</month>\n<year>0</year>\n</date>\n');
    out_file.write('<physical>\n<width>' + width + '</width>\n<height>' + height + '</height>\n</physical>\n');
    out_file.write('</image>\n\n');


out_file.write('</images>\n\n</gallery>');
out_file.close();
