Guest User

Untitled

a guest
Jan 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'amazon_product'
  4. require 'csv'
  5.  
  6. AWSKey = ''
  7. AWSSecret = ''
  8.  
  9. AWSAssociateTags = {
  10. :de => '',
  11. :uk => '',
  12. :us => ''
  13. }
  14.  
  15. Config = {
  16. 1 => ['de', 'Amazon.de (Germany)'],
  17. 2 => ['uk', 'Amazon.co.uk (Great Britain)'],
  18. 3 => ['us', 'Amazon.com (US)']
  19. }
  20.  
  21. class CsvFile
  22. Fields = {
  23. 'URL' => %w(DetailPageURL),
  24. 'ASIN' => %w(ASIN),
  25. 'Product Group' => %w(ItemAttributes ProductGroup),
  26. 'Title' => %w(ItemAttributes Title)
  27. }
  28.  
  29. def initialize
  30. @content = [Fields.keys]
  31. end
  32.  
  33. def add_item(item)
  34. @content << Fields.values.map do |attrs|
  35. attrs.inject item do |value, attr|
  36. value[attr]
  37. end
  38. end
  39. end
  40.  
  41. def write_file!
  42. filename = "amazon_export_#{Time.now.strftime('%Y-%m-%dT%H-%M-%S')}.csv"
  43.  
  44. CSV.open filename, 'wb' do |csv|
  45. @content.each do |row|
  46. csv << row
  47. end
  48. end
  49.  
  50. puts "File #{filename} created!"
  51. end
  52. end
  53.  
  54. q, config = nil, nil
  55.  
  56. puts 'Please select language:'
  57.  
  58. begin
  59. Config.each do |i, (locale, text)|
  60. puts "(#{i}) #{text}"
  61. end
  62.  
  63. config_number = gets.strip
  64.  
  65. if Config.has_key?(config_number.to_i)
  66. config = Config[config_number.to_i]
  67. else
  68. puts "Please enter one of the following options: #{Config.keys.join(', ')}"
  69. end
  70. end while config.nil?
  71.  
  72. puts "\nPlease enter search term(s):"
  73.  
  74. begin
  75. q = gets.strip
  76.  
  77. puts 'Please enter one or more serch terms!' if q.length == 0
  78. end while q.length == 0
  79.  
  80. req = AmazonProduct[config[0]]
  81.  
  82. req.configure do |c|
  83. c.key = AWSKey
  84. c.secret = AWSSecret
  85. c.tag = AWSAssociateTags[config[0].to_sym]
  86. end
  87.  
  88. puts "\nCreating CSV file, please wait..."
  89.  
  90. csv_file = CsvFile.new
  91.  
  92. (1..10).each do |item_page|
  93. resp = req.search('All',
  94. :keywords => q,
  95. :item_page => item_page
  96. )
  97.  
  98. next unless resp.valid? && !resp.has_errors?
  99.  
  100. resp.each('Item') do |item|
  101. csv_file.add_item item
  102. end
  103. end
  104.  
  105. csv_file.write_file!
Add Comment
Please, Sign In to add comment