# Bens amazing 'list of book barcodes -> folder of book cover images' convertor. # March, 2010 # # Freeware. Use it for the benefit of mankind (or not, it's up to you). # Credit Ben Dunham [www.toasta.com] if you feel like it. require 'ftools' require 'rubygems' require 'amazon/aws/search' require 'open-uri' include Amazon::AWS include Amazon::AWS::Search # Initialise the image file name counter i = 0 # Read in the barcodes from the file generated by RedLaser barcodes = File.read('barcodes.txt') # Loop over every line separated barcode in the file barcodes.split("\n").each do |barcode| # Wrap everything in a big ol exception handler. Not too bothered if something goes wrong. begin # Construct all the AWS gubbins lookup = ItemLookup.new('ISBN', { 'ItemId' => barcode, 'SearchIndex' => 'Books' }) group = ResponseGroup.new('Large') request = Request.new # Only check the Amazon UK site request.locale = 'uk' # Perform the AWS lookup response = request.search(lookup, group) # Check if any items were found if response.item_lookup_response[0].items # I want all images to be sequentially numbered, so only increment on successful AWS lookup i += 1 # Download the small Amazon book cover image = open(response.item_lookup_response[0].items[0].item[0].small_image.url) # Write the cover to a file in the Books directory File.open('../books/' + i.to_s + '.jpg','w'){|f| f.write(image.read) } # Close the downloaded image handle (not sure if this is required, but seems like a good idea!) image.close end rescue # No image found. Oh well, we tried. end end