Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
venshop
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tan Phat Nguyen
venshop
Commits
771a14e4
Commit
771a14e4
authored
Nov 17, 2014
by
Tan Phat Nguyen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor db.rake: get item from amazon
parent
fbc9fcb1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
29 deletions
+57
-29
.gitignore
+1
-0
app/uploaders/image_uploader.rb
+1
-1
lib/tasks/db.rake
+55
-28
No files found.
.gitignore
View file @
771a14e4
...
@@ -15,3 +15,4 @@
...
@@ -15,3 +15,4 @@
/log/*.log
/log/*.log
/tmp
/tmp
/public/uploads
/public/uploads
/config/application.yml
app/uploaders/image_uploader.rb
View file @
771a14e4
...
@@ -28,7 +28,7 @@ class ImageUploader < CarrierWave::Uploader::Base
...
@@ -28,7 +28,7 @@ class ImageUploader < CarrierWave::Uploader::Base
end
end
def
default_url
def
default_url
"fallback/"
+
[
thumb
,
"default_photo.png"
].
compact
.
join
(
'_'
)
'fallback/default_photo.png'
end
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# Provide a default URL as a default if there hasn't been a file uploaded:
...
...
lib/tasks/db.rake
View file @
771a14e4
...
@@ -7,53 +7,80 @@ namespace :db do
...
@@ -7,53 +7,80 @@ namespace :db do
def
import_products
def
import_products
puts
'Call import_products method'
puts
'Call import_products method'
req
=
Vacuum
.
new
build_request
req
.
configure
(
Category
.
all
.
each
do
|
category
|
aws_access_key_id:
'AKIAI4FGZR4YFCAP4DVQ'
,
import_products_for
(
category
)
aws_secret_access_key:
'r+ZlSNRib0FLll9T0WAemasl0Z/dejn8Wyjn3eX0'
,
end
associate_tag:
'tag'
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
(
','
)
def
items_for
(
category
)
categories
=
Category
.
all
[
1
,
7
].
map
do
|
page_number
|
var_category
=
{
'Books'
=>
'1000'
,
'HealthPersonalCare'
=>
'3760931'
,
'OutdoorLiving'
=>
'286168'
,
res
=
@request
.
item_search
(
query:
{
'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.Condition'
=>
'New'
,
'ItemSearch.Shared.BrowseNode'
=>
var_category
[
category
.
name
],
'ItemSearch.Shared.BrowseNode'
=>
category_hash
[
category
.
name
],
'ItemSearch.Shared.ResponseGroup'
=>
'Large'
,
'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
})
})
@res
=
@res
.
to_h
res
=
res
.
to_h
@res
[
'ItemSearchResponse'
][
'Items'
][
0
][
'Item'
].
each
do
|
item
|
res
[
'ItemSearchResponse'
][
'Items'
][
0
][
'Item'
]
item_product
=
Product
.
new
end
.
flatten
puts
item
[
'ItemAttributes'
][
'Title'
]
end
item_product
.
name
=
item
[
'ItemAttributes'
][
'Title'
]
item_product
.
price
=
item
[
'ItemAttributes'
][
'ListPrice'
][
'FormattedPrice'
][
1
..-
1
]
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
)
if
item
[
'EditorialReviews'
][
'EditorialReview'
].
kind_of?
(
Array
)
item_
product
.
description
=
item
[
'EditorialReviews'
][
'EditorialReview'
][
0
][
'Content'
]
product
.
description
=
item
[
'EditorialReviews'
][
'EditorialReview'
][
0
][
'Content'
]
else
else
item_
product
.
description
=
item
[
'EditorialReviews'
][
'EditorialReview'
][
'Content'
]
product
.
description
=
item
[
'EditorialReviews'
][
'EditorialReview'
][
'Content'
]
end
end
if
item
.
has_key?
(
'MediumImage'
)
if
item
.
has_key?
(
'MediumImage'
)
#item_product[:remote_photo_url]
= item['MediumImage']['URL']
product
.
remote_photo_url
=
item
[
'MediumImage'
][
'URL'
]
else
else
if
item
[
'ImageSets'
][
'ImageSet'
].
kind_of?
(
Array
)
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
else
#item_product[:remote_photo_url]
= item['ImageSets']['ImageSet']['MediumImage']['URL']
product
.
remote_photo_url
=
item
[
'ImageSets'
][
'ImageSet'
][
'MediumImage'
][
'URL'
]
end
end
end
end
category
.
products
<<
item_product
puts
'OK'
end
end
category
.
save
product
end
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
end
def
category_hash
{
'Books'
=>
'1000'
,
'HealthPersonalCare'
=>
'3760931'
,
'OutdoorLiving'
=>
'286168'
,
'Appliances'
=>
'2619525011'
,
'Magazines'
=>
'599872'
}
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment