Commit b7d37151 by vulehuan

get product from yahoo shopping api

parent 079a8c34
class Product < ActiveRecord::Base
end
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.text :description
t.string :headline
t.string :availability
t.string :code
t.string :condition
t.string :image_small
t.string :image_medium
t.decimal :review_rate
t.integer :review_count
t.string :price_currency
t.decimal :price
t.integer :product_category_id
t.timestamps
end
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20131023083438) do ActiveRecord::Schema.define(version: 20131023094033) do
create_table "product_categories", force: true do |t| create_table "product_categories", force: true do |t|
t.string "name" t.string "name"
...@@ -21,6 +21,24 @@ ActiveRecord::Schema.define(version: 20131023083438) do ...@@ -21,6 +21,24 @@ ActiveRecord::Schema.define(version: 20131023083438) do
t.datetime "updated_at" t.datetime "updated_at"
end end
create_table "products", force: true do |t|
t.string "name"
t.text "description"
t.string "headline"
t.string "availability"
t.string "code"
t.string "condition"
t.string "image_small"
t.string "image_medium"
t.decimal "review_rate", precision: 10, scale: 0
t.integer "review_count"
t.string "price_currency"
t.decimal "price", precision: 10, scale: 0
t.integer "product_category_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t| create_table "users", force: true do |t|
t.string "name" t.string "name"
t.string "email" t.string "email"
......
namespace :db do namespace :db do
desc "Fill database with Yahoo shopping data" desc "Fill database with Yahoo shopping data"
task populate: :environment do task populate: :environment do
# Product Category
category_url = 'http://dir.yahooapis.jp/Category/V1/Category'
category_path = '/Computers_and_Internet/Hardware/SmartPhone/'
request_url = category_url + '?appid=' + YAHOO_SHOPPING_DATA_APP_ID + '&path=' + category_path
require 'net/http' require 'net/http'
require 'rexml/document' require 'rexml/document'
# Product Category
# http://developer.yahoo.co.jp/webapi/dir/category/v1/category.html
category_url = 'http://shopping.yahooapis.jp/ShoppingWebService/V1/categorySearch'
request_url = category_url + '?appid=' + YAHOO_SHOPPING_DATA_APP_ID + '&category_id=1'
# get the XML data as a string # get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(request_url)).body xml_data = Net::HTTP.get_response(URI.parse(request_url)).body
# extract category information # extract category information
doc = REXML::Document.new(xml_data) doc = REXML::Document.new(xml_data)
i = 0 i = 0
doc.elements.each('CategoryResult/Category/Item') do |ele| category_ids = Array.new
doc.elements.each('ResultSet/Result/Categories/Children/Child') do |ele|
i += 1 i += 1
break if i > 5 break if i > 5
id = ele.elements['Id'].text id = ele.elements['Id'].text
title = ele.elements['Title'].text title = ele.elements['Title']
title.elements.each('Long') do |child_ele|
title = child_ele.text
end
category_ids.push(id)
ProductCategory.create!( ProductCategory.create!(
id: id, id: id,
...@@ -26,5 +33,67 @@ namespace :db do ...@@ -26,5 +33,67 @@ namespace :db do
status: true status: true
) )
end end
# Product list
# http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch
list_url = 'http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch'
category_ids.each do |category_id|
request_url = list_url + '?appid=' + YAHOO_SHOPPING_DATA_APP_ID + '&category_id=' + category_id
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(request_url)).body
# extract product information
doc = REXML::Document.new(xml_data)
i = 0
doc.elements.each('ResultSet/Result/Hit') do |ele|
next if ele.elements['Name'] == nil
i += 1
break if i > 20
name = ele.elements['Name'].text
description = ele.elements['Description'].text
headline = ele.elements['Headline'].text
availability = ele.elements['Availability'].text
code = ele.elements['Code'].text
condition = ele.elements['Condition'].text
image = ele.elements['Image']
image_small = ''
image_medium = ''
image.elements.each('Small') do |child_ele|
image_small = child_ele.text
end
image.elements.each('Medium') do |child_ele|
image_medium = child_ele.text
end
review = ele.elements['Review']
review_rate = 0
review_count = 0
review.elements.each('Rate') do |child_ele|
review_rate = child_ele.text
end
review.elements.each('Count') do |child_ele|
review_count = child_ele.text
end
price_currency = ele.attributes["currency"]
price = ele.elements['Price'].text
Product.create!(
name: name,
description: description,
headline: headline,
availability: availability,
code: code,
condition: condition,
image_small: image_small,
image_medium: image_medium,
review_rate: review_rate,
review_count: review_count,
price_currency: price_currency,
price: price,
product_category_id: category_id
)
end
end
end end
end end
\ No newline at end of file
require 'spec_helper'
describe Product do
pending "add some examples to (or delete) #{__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