Commit fbc9fcb1 by Tan Phat Nguyen

Amazon get item

parent c1144e64
...@@ -6,6 +6,6 @@ ...@@ -6,6 +6,6 @@
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first) # Mayor.create(name: 'Emanuel', city: cities.first)
%w(Books Toys OutdoorLiving Appliances Magazines).each do |category| %w(Books HealthPersonalCare OutdoorLiving Appliances Magazines).each do |category|
Category.create(name: category) Category.create(name: category)
end end
...@@ -17,45 +17,43 @@ namespace :db do ...@@ -17,45 +17,43 @@ namespace :db do
rg = %w(ItemAttributes Images EditorialReview).join(',') rg = %w(ItemAttributes Images EditorialReview).join(',')
categories = Category.all categories = Category.all
var_category = {'Books' => '1000', 'Toys' => '165793011', 'OutdoorLiving' => '286168', var_category = {'Books' => '1000', 'HealthPersonalCare' => '3760931', 'OutdoorLiving' => '286168',
'Appliances' => '2619525011', 'Magazines' => '599872'} 'Appliances' => '2619525011', 'Magazines' => '599872'}
categories.each do |category| categories.each do |category|
[1, 7].each do |page_number| [1, 7].each do |page_number|
@res = req.item_search(query: { @res = req.item_search(query: {
'ItemSearch.Shared.Condition' => 'New', 'ItemSearch.Shared.Condition' => 'New',
'ItemSearch.Shared.BrowseNode' => var_category[category.name], 'ItemSearch.Shared.BrowseNode' => var_category[category.name],
'ItemSearch.Shared.ResponseGroup' => rg, 'ItemSearch.Shared.ResponseGroup' => 'Large',
'ItemSearch.SearchIndex' => category.name, 'ItemSearch.SearchIndex' => category.name,
'ItemSearch.1.ItemPage' => page_number, 'ItemSearch.1.ItemPage' => page_number,
'ItemSearch.2.ItemPage' => 10 'ItemSearch.2.ItemPage' => 10
}) })
@add_new = [] @res = @res.to_h
@res.to_h["ItemSearchResponse"].to_h["Items"].to_a.each do |item| @res['ItemSearchResponse']['Items'][0]['Item'].each do |item|
item.to_h["Item"].to_a.each do |value|
book_tmp = value.to_h
@add_new.push(details: book_tmp['DetailPageURL'], image: book_tmp['LargeImage'],
item: book_tmp['ItemAttributes'], editor: book_tmp['EditorialReviews'])
end
end
@add_new.each do |item_res|
item_product = Product.new item_product = Product.new
item_product.name = item_res[:item]['Title'] puts item['ItemAttributes']['Title']
if item_res[:item].has_key?('ListPrice') item_product.name = item['ItemAttributes']['Title']
item_product.price = item_res[:item]['ListPrice']['FormattedPrice'][1..-1] item_product.price = item['ItemAttributes']['ListPrice']['FormattedPrice'][1..-1]
if item['EditorialReviews']['EditorialReview'].kind_of?(Array)
item_product.description = item['EditorialReviews']['EditorialReview'][0]['Content']
else else
item_product.price = item_res[:item]['TradeInValue']['FormattedPrice'][1..-1] item_product.description = item['EditorialReviews']['EditorialReview']['Content']
end end
if item_res[:editor]['EditorialReview'].kind_of?(Array) if item.has_key?('MediumImage')
item_product.description = item_res[:editor]['EditorialReview'][0]['Content'] #item_product[:remote_photo_url] = item['MediumImage']['URL']
else
if item['ImageSets']['ImageSet'].kind_of?(Array)
#item_product[:remote_photo_url] = item['ImageSets']['ImageSet'][0]['MediumImage']['URL']
else else
item_product.description = item_res[:editor]['EditorialReview']['Content'] #item_product[:remote_photo_url] = item['ImageSets']['ImageSet']['MediumImage']['URL']
end
end end
# item_product[:remote_photo_url] = item_res[:image]['URL']
category.products << item_product category.products << item_product
puts 'OK' puts 'OK'
end end
end
category.save category.save
end end
end end
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment