@
acpp 之前搞过php,python之类的
花了两个礼拜把ror guide看了一遍
@
lyhapple 这个是实验性质的,很多地方很粗糙,打算重构。 vps是香港的
api调用
def sign(app_secret, params)
str = []
str << app_secret
str << params.sort.join
str << app_secret
Digest::MD5.hexdigest(str.join).upcase
end
def api_url
api_url = '
http://gw.api.taobao.com/router/rest' end
def url(params)
url = []
params.sort.each {|k, v| url << (k.to_s << '=' << URI.encode(v.to_s).to_s)}
api_url << '?' << (url.join '&')
end
def get(url)
res = Net::HTTP.get_response(URI.parse(url))
res.body
end
def product_params(app_key, app_secret, cid)
params = {}
params[:format] = 'json'
params[:timestamp] = (Time.now).strftime("%Y-%m-%d %H:%M:%S")
params[:sign_method] = 'md5'
params[:v] = '2.0'
params[:method] = 'taobao.taobaoke.items.coupon.get'
params[:fields] = ['item_imgs', 'num_iid', 'seller_id', 'nick', 'title', 'price', 'item_location', 'seller_credit_score', 'click_url', 'shop_click_url', 'pic_url', 'taobaoke_cat_click_url', 'keyword_click_url', 'coupon_rate', 'coupon_price', 'coupon_start_time', 'coupon_end_time', 'commission_rate', 'commission', 'commission_num', 'commission_volume', 'volume', 'shop_type'].join ','
params[:cid] = cid
params[:keyword] = ''
params[:sort] = 'volume_desc'
params[:app_key] = app_key
params[:page_no] = 1
params[:page_size] = 100
params[:shop_type] = 'b'
params[:sign] = sign(app_secret, params)
params
end
task :product => :environment do
app_key = Setting.g 'app_key', 'taobao'
app_secret = Setting.g 'app_secret', 'taobao'
categories = Category.where("level = ? or id = ?", 2, 50008055).all
categories.each do |c|
next if TaobaoCategory.where(:cid =>
c.id).first.nil?
params = product_params app_key, app_secret,
c.id json = get(url(params))
p json
products = ActiveSupport::JSON.decode(json)
next if products.nil? || products['taobaoke_items_coupon_get_response'].nil? || products['taobaoke_items_coupon_get_response']['taobaoke_items'].nil?
items = products['taobaoke_items_coupon_get_response']['taobaoke_items']['taobaoke_item']
next unless items.respond_to? :each
items.each do |p|
img_json = get(url(img_params app_key, app_secret, p['num_iid']))
imgs = ActiveSupport::JSON.decode(img_json)
imgs = imgs['items_list_get_response']['items']['item']
unless imgs[0]['item_imgs'].empty?
p['imgs'] = []
imgs[0]['item_imgs']["item_img"].each do |i|
p['imgs'] << i['url']
end
end
save_product p,
c.id, Product
end
end
end
代码凑活看吧