#!/usr/bin/env python import sys import time import urllib from xml.dom import minidom key = open('/home/kr/.amazon-key').read() __author__ = "Keith Rarick " __version__ = "2005-10-09" __url__ = "http://xph.us/software" __description__ = "Modified Amazon link generator." TEMPLATE = """

%(ProductName)s%(ProductName)s
%(Authors)s

""" INPUT_FILE = '%(name)s' def pull_text(nodes): rc = '' for node in nodes: if node.nodeType == node.TEXT_NODE: rc = rc + node.data return rc def text(frag, name): try: return pull_text(get0(frag, name).childNodes) except: return None def get0(frag, name): return frag.getElementsByTagName(name)[0] def expand_line(line): if line == 'INPUT_FILE\n': return INPUT_FILE % {'name':input_file} if not line.startswith("AMAZON"): return line line = line.split('::') asin = line[1] title = line[2] # try twice url = "http://ecs.amazonaws.com/onca/xml?%s" params = { 'Service': 'AWSECommerceService', 'AWSAccessKeyId': key, 'Operation': 'ItemLookup', 'AssociateTag': 'devconn-20', 'Version': '2007-01-15', 'ItemId': asin, 'ResponseGroup': 'ItemAttributes,Images', } doc = urllib.urlopen(url % urllib.urlencode(params)) xmldoc = minidom.parse(doc) res = xmldoc.getElementsByTagName('Item') # ProductInfo.Details info = {} info['URL'] = text(xmldoc, 'DetailPageURL') info['ImageUrlThumb'] = text(get0(xmldoc, 'MediumImage'), 'URL') info['ImageUrlLarge'] = text(get0(xmldoc, 'LargeImage'), 'URL') info['ProductName'] = None if not info['ProductName']: info['ProductName'] = text(xmldoc, 'Title') info['Authors'] = None if not info['Authors']: info['Authors'] = text(xmldoc, 'Authors') if not info['Authors']: info['Authors'] = text(xmldoc, 'Author') if not info['Authors']: info['Authors'] = text(xmldoc, 'Artists') if not info['Authors']: info['Authors'] = text(xmldoc, 'Artist') if not info['Authors']: info['Authors'] = text(xmldoc, 'Directors') if not info['Authors']: info['Authors'] = text(xmldoc, 'Director') return TEMPLATE % info if __name__ == '__main__': global input_file input_file = sys.argv[1] oname = sys.argv[2] out = open(oname, 'w') lines = open(input_file).readlines() lines = [expand_line(m) for m in lines] for l in lines: out.write(l.encode('UTF-8'))