Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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!
Add Comment
Please, Sign In to add comment