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| item_product = Product.new
book_tmp = value.to_h puts item['ItemAttributes']['Title']
@add_new.push(details: book_tmp['DetailPageURL'], image: book_tmp['LargeImage'], item_product.name = item['ItemAttributes']['Title']
item: book_tmp['ItemAttributes'], editor: book_tmp['EditorialReviews']) item_product.price = item['ItemAttributes']['ListPrice']['FormattedPrice'][1..-1]
end if item['EditorialReviews']['EditorialReview'].kind_of?(Array)
end item_product.description = item['EditorialReviews']['EditorialReview'][0]['Content']
@add_new.each do |item_res| else
item_product = Product.new item_product.description = item['EditorialReviews']['EditorialReview']['Content']
item_product.name = item_res[:item]['Title'] end
if item_res[:item].has_key?('ListPrice') if item.has_key?('MediumImage')
item_product.price = item_res[:item]['ListPrice']['FormattedPrice'][1..-1] #item_product[:remote_photo_url] = item['MediumImage']['URL']
else else
item_product.price = item_res[:item]['TradeInValue']['FormattedPrice'][1..-1] if item['ImageSets']['ImageSet'].kind_of?(Array)
end #item_product[:remote_photo_url] = item['ImageSets']['ImageSet'][0]['MediumImage']['URL']
if item_res[:editor]['EditorialReview'].kind_of?(Array) else
item_product.description = item_res[:editor]['EditorialReview'][0]['Content'] #item_product[:remote_photo_url] = item['ImageSets']['ImageSet']['MediumImage']['URL']
else end
item_product.description = item_res[:editor]['EditorialReview']['Content'] end
end category.products << item_product
# item_product[:remote_photo_url] = item_res[:image]['URL'] puts 'OK'
category.products << item_product end
puts 'OK' category.save
end end
end end
category.save
end
end end
end end
\ No newline at end of file
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