This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.rb
69 lines (61 loc) · 1.99 KB
/
loader.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require 'amazon_product'
require 'json'
require 'couchrest'
require File.expand_path('../helper.rb', __FILE__)
# Check configuration
if COUCHDB_SERVER.nil?
raise RuntimeError, "Loader requires a CouchDB server url in the configuration.yml file."
end
if COUCHDB.nil?
raise RuntimeError, "Loader requires a CouchDB db name in the configuration.yml file."
end
if AMAZON_KEY.nil?
raise RuntimeError, "Loader requires an AWS Key in the configuration.yml file."
end
if AMAZON_SECRET.nil?
raise RuntimeError, "Loader requires an AWS Secret in the configuration.yml file."
end
if AMAZON_ASSOCIATE_TAG.nil?
raise RuntimeError, "Loader requires a AWS Associate ID in the configuration.yml file."
end
if AMAZON_SEARCH_INDEX.nil?
raise RuntimeError, "Loader requires an AMAZON Product Advertising Search Index in the configuration.yml file."
end
if AMAZON_KEYWORDS.nil?
raise RuntimeError, "Loader requires AMAZON Product Advertising Keywords in the configuration.yml file."
end
req = AmazonProduct["de"]
req.configure do |c|
c.key = AMAZON_KEY
c.secret = AMAZON_SECRET
c.tag = AMAZON_ASSOCIATE_TAG
end
# Load Products from Amazon
# req << { :operation => 'ItemSearch',
# :search_index => 'Books',
# :power => 'author:geisler',
# :keywords => 'Semantic Web' }
req << { :operation => 'ItemSearch',
:search_index => AMAZON_SEARCH_INDEX,
:keywords => AMAZON_KEYWORDS }
resp = req.get
#puts resp.to_hash.to_json
#puts resp['Item'].to_json
# Connect to CouchDB
couch = CouchRest.new(URI.escape(COUCHDB_SERVER))
@db = couch.database(URI.escape(COUCHDB))
# Save Products in CouchDB Documents
if(resp.valid? && !resp.has_errors?)
resp.each('Item') do |item|
@db.save_doc({
'sku' => item['ASIN'],
'title' => item['ItemAttributes']['Title'],
'url' => item['DetailPageURL'],
'attributes' => item['ItemAttributes'],
'links' => item['ItemLinks']
})
end
end