Skip to content

Instantly share code, notes, and snippets.

@julianeon
Last active December 21, 2015 16:59
Show Gist options
  • Save julianeon/6337831 to your computer and use it in GitHub Desktop.
Save julianeon/6337831 to your computer and use it in GitHub Desktop.
Amazon example script; an example of working with a much different API.
require "rubygems"
require "amazon_product"
req = AmazonProduct["us"]
req.configure do |c|
c.key = "fake"
c.secret = "also-fake"
c.tag = "another-fake"
end
if ARGV.first.nil?
puts "You have to enter a file to fetch serial numbers."
abort
end
a = File.open(ARGV.first).readlines
a.map! { |x| x.chomp }
asin = Array.new
cardvalue = Array.new
a.each do |x|
y = x.split(",")
y.each { |z| z.to_i > 1000 ? asin.push(z) : cardvalue.push(z) }
end
h = Hash.new
a.each_index { |x| h[asin[x]]= cardvalue[x]}
fn = File.open(ARGV.first.sub(/\./, 'low.'),'w')
top=0
while top<asin.length
req << {
'Operation' => 'ItemLookup',
'ItemLookup.Shared.IdType' => 'ASIN',
'ItemLookup.Shared.Condition' => 'All',
'ItemLookup.Shared.MerchantId' => 'All',
'ItemLookup.Shared.ResponseGroup' => 'OfferFull',
'ItemLookup.1.ItemId' => asin[top, 10],
'ItemLookup.2.ItemId' => asin[top+10, 10] }
resp = req.get
top+=20
resp.each('Item') do |item|
isbn=item['ASIN']
card = h[isbn]
if (item['OfferSummary']['LowestUsedPrice'] != nil)
price = item['OfferSummary']['LowestUsedPrice']['Amount'].to_f/100
else
price = item['OfferSummary']['LowestNewPrice']['Amount'].to_f/100
end
unless isbn.nil? || price.nil? #|| card.nil?
word = "#{isbn}, #{price.to_s}, #{price.to_i}"
else
word = "nil"
end
price.to_i>20 ? puts(word) : word
(card.to_f*0.65) > (price+3.99) ? fn.write(word) : word
end
puts (top*100/asin.length)
end
print Time.now.hour.modulo(12),":",Time.now.min,"\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment