#!/usr/bin/env python
 
'''
Expamapur (Export Amazon Purchases, 2011 by Konrad Voelkel) is a script that reads Amazon's "purchased books" HTML files and spits out a list of ASINs (for books, this coincides with ISBN-10) of your items.

See also:
http://blog.konradvoelkel.de/2011/09/export-amazon-purchased-books/
'''
 
import sys, re
 
# <tr valign=middle id="iyrListItemASINASIN10">
asinRegExString = "<tr valign=middle id=\"iyrListItem([A-Z0-9]{10})\">"
asinRegEx = re.compile(asinRegExString)
 
if(len(sys.argv)<2):
    print "Please provide the location of the HTML file of your purchases as command-line argument."
    exit()
 
filename = sys.argv[1]
try:
    f = open(filename,'r')
except IOError:
    print "There have been I/O problems with the file '%s'" % filename
    exit()
 
asinlist = []
for line in f.readlines():
    match = asinRegEx.match(line)
    if match != None:
        asinlist+=[match.group(1)]
f.close()
 
print "\n".join(asinlist)

