Skip to content

Instantly share code, notes, and snippets.

@manuelmeurer
Created September 6, 2011 11:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save manuelmeurer/1197347 to your computer and use it in GitHub Desktop.
Save manuelmeurer/1197347 to your computer and use it in GitHub Desktop.
Quick and dirty Amazon product export to a CSV file
#!/usr/bin/env ruby
require 'amazon_product'
require 'csv'
AWSKey = ''
AWSSecret = ''
AWSAssociateTags = {
:de => '',
:uk => '',
:us => ''
}
Config = {
1 => ['de', 'Amazon.de (Germany)'],
2 => ['uk', 'Amazon.co.uk (Great Britain)'],
3 => ['us', 'Amazon.com (US)']
}
class CsvFile
Fields = {
'URL' => %w(DetailPageURL),
'ASIN' => %w(ASIN),
'Product Group' => %w(ItemAttributes ProductGroup),
'Title' => %w(ItemAttributes Title)
}
def initialize
@content = [Fields.keys]
end
def add_item(item)
@content << Fields.values.map do |attrs|
attrs.inject item do |value, attr|
value[attr]
end
end
end
def write_file!
filename = "amazon_export_#{Time.now.strftime('%Y-%m-%dT%H-%M-%S')}.csv"
CSV.open filename, 'wb' do |csv|
@content.each do |row|
csv << row
end
end
puts "File #{filename} created!"
end
end
q, config = nil, nil
puts 'Please select language:'
begin
Config.each do |i, (locale, text)|
puts "(#{i}) #{text}"
end
config_number = gets.strip
if Config.has_key?(config_number.to_i)
config = Config[config_number.to_i]
else
puts "Please enter one of the following options: #{Config.keys.join(', ')}"
end
end while config.nil?
puts "\nPlease enter search term(s):"
begin
q = gets.strip
puts 'Please enter one or more serch terms!' if q.length == 0
end while q.length == 0
req = AmazonProduct[config[0]]
req.configure do |c|
c.key = AWSKey
c.secret = AWSSecret
c.tag = AWSAssociateTags[config[0].to_sym]
end
puts "\nCreating CSV file, please wait..."
csv_file = CsvFile.new
(1..10).each do |item_page|
resp = req.search('All',
:keywords => q,
:item_page => item_page
)
next unless resp.valid? && !resp.has_errors?
resp.each('Item') do |item|
csv_file.add_item item
end
end
csv_file.write_file!
@trismegistis
Copy link

Does this need a config file? Which page does it choose and where can I set that variable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment