Commit 771a14e4 by Tan Phat Nguyen

refactor db.rake: get item from amazon

parent fbc9fcb1
......@@ -15,3 +15,4 @@
/log/*.log
/tmp
/public/uploads
/config/application.yml
......@@ -28,7 +28,7 @@ class ImageUploader < CarrierWave::Uploader::Base
end
def default_url
"fallback/" + [thumb, "default_photo.png"].compact.join('_')
'fallback/default_photo.png'
end
# Provide a default URL as a default if there hasn't been a file uploaded:
......
......@@ -7,53 +7,80 @@ namespace :db do
def import_products
puts 'Call import_products method'
req = Vacuum.new
build_request
req.configure(
aws_access_key_id: 'AKIAI4FGZR4YFCAP4DVQ',
aws_secret_access_key: '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',
associate_tag: 'tag'
)
Category.all.each do |category|
import_products_for(category)
end
end
private
def import_products_for(category)
items_for(category).each do |item|
product = build_product_from(item, category)
if product.save
puts "imported #{product.name}"
else
puts 'import fail'
end
end
end
rg = %w(ItemAttributes Images EditorialReview).join(',')
categories = Category.all
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: {
def items_for(category)
[1, 7].map do |page_number|
res = @request.item_search(query: {
'ItemSearch.Shared.Condition' => 'New',
'ItemSearch.Shared.BrowseNode' => var_category[category.name],
'ItemSearch.Shared.BrowseNode' => category_hash[category.name],
'ItemSearch.Shared.ResponseGroup' => 'Large',
'ItemSearch.SearchIndex' => category.name,
'ItemSearch.1.ItemPage' => page_number,
'ItemSearch.2.ItemPage' => 10
})
@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]
res = res.to_h
res['ItemSearchResponse']['Items'][0]['Item']
end.flatten
end
def build_product_from(item, category)
product = Product.find_or_initialize_by(name: item['ItemAttributes']['Title'])
if product.new_record?
product.category = category
product.price = item['ItemAttributes']['ListPrice']['FormattedPrice'][1..-1]
if item['EditorialReviews']['EditorialReview'].kind_of?(Array)
item_product.description = item['EditorialReviews']['EditorialReview'][0]['Content']
product.description = item['EditorialReviews']['EditorialReview'][0]['Content']
else
item_product.description = item['EditorialReviews']['EditorialReview']['Content']
product.description = item['EditorialReviews']['EditorialReview']['Content']
end
if item.has_key?('MediumImage')
#item_product[:remote_photo_url] = item['MediumImage']['URL']
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']
product.remote_photo_url = item['ImageSets']['ImageSet'][0]['MediumImage']['URL']
else
#item_product[:remote_photo_url] = item['ImageSets']['ImageSet']['MediumImage']['URL']
product.remote_photo_url = item['ImageSets']['ImageSet']['MediumImage']['URL']
end
end
category.products << item_product
puts 'OK'
end
category.save
product
end
def build_request
@request = Vacuum.new
@request.configure(
aws_access_key_id: 'AKIAI4FGZR4YFCAP4DVQ',
aws_secret_access_key: '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',
associate_tag: 'tag'
)
end
def category_hash
{ 'Books' => '1000', 'HealthPersonalCare' => '3760931', 'OutdoorLiving' => '286168',
'Appliances' => '2619525011', 'Magazines' => '599872' }
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