Commit fbc9fcb1 by Tan Phat Nguyen

Amazon get item

parent c1144e64
......@@ -6,6 +6,6 @@
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# 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)
end
......@@ -17,45 +17,43 @@ namespace :db do
rg = %w(ItemAttributes Images EditorialReview).join(',')
categories = Category.all
var_category = {'Books' => '1000', 'Toys' => '165793011', 'OutdoorLiving' => '286168',
var_category = {'Books' => '1000', 'HealthPersonalCare' => '3760931', 'OutdoorLiving' => '286168',
'Appliances' => '2619525011', 'Magazines' => '599872'}
categories.each do |category|
[1, 7].each do |page_number|
@res = req.item_search(query: {
'ItemSearch.Shared.Condition' => 'New',
'ItemSearch.Shared.BrowseNode' => var_category[category.name],
'ItemSearch.Shared.ResponseGroup' => rg,
'ItemSearch.Shared.ResponseGroup' => 'Large',
'ItemSearch.SearchIndex' => category.name,
'ItemSearch.1.ItemPage' => page_number,
'ItemSearch.2.ItemPage' => 10
})
@add_new = []
@res.to_h["ItemSearchResponse"].to_h["Items"].to_a.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.name = item_res[:item]['Title']
if item_res[:item].has_key?('ListPrice')
item_product.price = item_res[:item]['ListPrice']['FormattedPrice'][1..-1]
else
item_product.price = item_res[:item]['TradeInValue']['FormattedPrice'][1..-1]
end
if item_res[:editor]['EditorialReview'].kind_of?(Array)
item_product.description = item_res[:editor]['EditorialReview'][0]['Content']
else
item_product.description = item_res[:editor]['EditorialReview']['Content']
end
# item_product[:remote_photo_url] = item_res[:image]['URL']
category.products << item_product
puts 'OK'
end
end
category.save
end
@res = @res.to_h
@res['ItemSearchResponse']['Items'][0]['Item'].each do |item|
item_product = Product.new
puts item['ItemAttributes']['Title']
item_product.name = item['ItemAttributes']['Title']
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
item_product.description = item['EditorialReviews']['EditorialReview']['Content']
end
if item.has_key?('MediumImage')
#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
#item_product[:remote_photo_url] = item['ImageSets']['ImageSet']['MediumImage']['URL']
end
end
category.products << item_product
puts 'OK'
end
category.save
end
end
end
end
\ No newline at end of file
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