Guest User

Untitled

a guest
May 21st, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'rubygems'
  3. require 'amazon/aws'
  4. require 'amazon/aws/search'
  5. require 'optparse'
  6.  
  7. include Amazon::AWS
  8. include Amazon::AWS::Search
  9.  
  10. options = {}
  11. OptionParser.new do |opts|
  12. opts.banner = "Usage: aws-quicksearch.rb [options]"
  13.  
  14. opts.on("-t", "--title [TITLE]", "Search for title match") do |t|
  15. options[:Title] = t
  16. end
  17. opts.on("-a", "--author [AUTHOR]", "Search for author match") do |a|
  18. options[:Author] = a
  19. end
  20. opts.on("-n", "--max-items [MAX]", "Set max results") do |m|
  21. options[:max] = m.to_i
  22. end
  23. end.parse!
  24.  
  25. exit 1 unless (options[:Title] || options[:Author])
  26.  
  27. is = ItemSearch.new( :Books, {
  28. :MerchantId => 'Amazon',
  29. :Sort => 'salesrank'
  30. }.merge(options.reject { |k,v| k == :max }))
  31.  
  32. rg = ResponseGroup.new( 'Small' )
  33.  
  34. req = Request.new
  35. resp = req.search( is, rg )
  36.  
  37. items = resp.item_search_response[0].items.item
  38.  
  39. items.each_with_index do |item, idx|
  40. break if options[:max] && idx + 1 > options[:max]
  41.  
  42. attributes = item.item_attributes[0]
  43. puts "Title: #{attributes.title}"
  44. puts "Author: #{attributes.author.join(', ')}" if attributes.author
  45. puts "Publisher: #{attributes.manufacturer}"
  46. puts "ASIN: #{item.asin}"
  47. puts "URL: #{item.detail_page_url}"
  48. puts "-" * 20
  49. end
Add Comment
Please, Sign In to add comment