#!/usr/bin/python

import sys
import urllib2
import xml.dom.minidom
from xml.dom.minidom import Document


def makeField (document, book, name, dataField):
    
    fieldElement = document.createElement ("field")
    fieldElement.setAttribute ("name", name)

    textElement = document.createTextNode(dataField)
    fieldElement.appendChild(textElement)
    book.appendChild(fieldElement)



quickfill_xml = xml.dom.minidom.parse('/tmp/books-quickfill.xml')
nodelist= quickfill_xml.getElementsByTagName('field')

title = ""
authors = ""
publisher = ""
isbn = ""

for node in nodelist:
    node.normalize()

    if node.firstChild != None:
	name = node.attributes["name"].value
	dataField = node.firstChild.data
	if dataField != "":
	    if name == "authors":
		authors = node.firstChild.data
	    elif name == "title":
		title = node.firstChild.data
	    elif name == "publisher" :
		publisher = node.firstChild.data
	    elif name == "isbn" :
		isbn = node.firstChild.data

query = ""

if isbn != "":
	query = isbn
elif authors != "" and title=="" and publisher=="":
	query=authors
elif authors == "" and title !="" and publisher=="":
	query=title
elif authors != "" and title != "" and publisher=="":
	query=authors + " " + title
elif authors =="" and title =="" and publisher!="":
	query=publisher
else:
	query = authors + " " + title + " " + publisher


query = "http://www.aladdin.co.kr/ttb/api/search.aspx?TTBKey=ttbwhatistao2341011&Cover=Big&MaxResults=100&Query=" + query
query=query.encode('utf-8')  # ATTENTION!

aladdin_xml = urllib2.urlopen(query)
doc = xml.dom.minidom.parse(aladdin_xml)
resultlist = doc.getElementsByTagName('item')

output_doc = Document()

root = output_doc.createElement ("importedData")
output_doc.appendChild (root)

collection = output_doc.createElement ("List")
collection.setAttribute ("name", "Aladdin Search Result")
root.appendChild (collection)

if resultlist.length != 0:
	
    for item in resultlist:
    	
	item.normalize()

	bookElement = output_doc.createElement ("Book")

    	for field in item.childNodes:
		
	    if field.hasChildNodes() :

		if field.nodeName == "title": makeField (output_doc, bookElement, "title", field.firstChild.data)
		elif field.nodeName == "author": makeField (output_doc, bookElement, "authors", field.firstChild.data)  # author's'. not 'author'
		elif field.nodeName == "aladdin:publisher": makeField (output_doc, bookElement, "publisher", field.firstChild.data)
		elif field.nodeName == "dc:date": makeField (output_doc, bookElement, "publishDate", field.firstChild.data)
		elif field.nodeName == "aladdin:isbn": makeField (output_doc, bookElement, "isbn", field.firstChild.data)  # pubDate has same value, but I don't need time.
		elif field.nodeName == "aladdin:cover": makeField (output_doc, bookElement, "CoverImageURL", field.firstChild.data)
		elif field.nodeName == "aladdin:categoryName": makeField (output_doc, bookElement, "genre", field.firstChild.data)
		elif field.nodeName == "description": makeField (output_doc, bookElement, "summary", field.firstChild.data)

	collection.appendChild (bookElement)

print output_doc.toprettyxml(encoding="UTF-8", indent=" ")

sys.stdout.flush()