TaoBaoApi 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/lib/TaoBaoApi.rb +61 -37
- data/lib/TaoBaoApi/version.rb +1 -1
- data/test.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
OWZkNWYzMzYwZjIwZWIzNDE3ZTdjNTk2MDM5ZWMwMDBlYmIzOGQ3Yg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZGUzY2ZiMzNiYmE2MzU2YWEwODZhZTUxYjUyZTlmNjgyZmY4ZTA0ZQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NjFhNWU1YjA1YzExYTY5MmUyODY1YWZlZWM1NzVjOGQxNTUwNTYzMzFkY2U4
|
10
|
+
OGNlNzU5Y2ZjYzAyZDNjNzU1ZmMxY2M0NTc5NmU0NTZjZjZkYTViMzliODI3
|
11
|
+
NDI4ZjJmMDM3MmQ1NmI2NzA2ODUyNmU3ODk4ODFiNzQxMmNiYTM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NTVhNGY1YTllMTk4ZTE4NDNkYzk4NDQ1NmRlMTU2ODMwMmRkYzI2OTc0OTc2
|
14
|
+
ODRiZGJkZmIwNThmNWJlZjM2ZjcyMDU1MjBmYWY0MjJmZTUwYWU0YWZiZTc1
|
15
|
+
ZWRjZjVjYjViYjE5OGM3YzIzYmM0MWFjZjEzOTY5NDQzYjY3NzA=
|
data/lib/TaoBaoApi.rb
CHANGED
@@ -1,58 +1,61 @@
|
|
1
1
|
require "TaoBaoApi/version"
|
2
2
|
require 'nokogiri'
|
3
3
|
require 'faraday'
|
4
|
-
# require 'faraday-cookie_jar'
|
5
4
|
require 'faraday_middleware'
|
5
|
+
require 'json'
|
6
|
+
require 'open-uri'
|
6
7
|
|
7
8
|
module TaoBaoApi
|
8
9
|
|
9
10
|
class Good
|
10
11
|
def initialize(url)
|
11
|
-
@
|
12
|
+
@url_info = filter_url(url)
|
12
13
|
|
13
|
-
|
14
|
-
init_url = 'http://www.tmall.com'
|
15
|
-
else
|
16
|
-
init_url = 'http://www.taobao.com/'
|
17
|
-
end
|
18
|
-
|
19
|
-
@conn = Faraday.new(:url => init_url) do |f|
|
14
|
+
@conn = Faraday.new(:url => @init_url) do |f|
|
20
15
|
f.use FaradayMiddleware::FollowRedirects , limit: 10
|
21
16
|
|
22
17
|
f.request :url_encoded
|
23
18
|
f.adapter :net_http
|
24
|
-
f.headers[:referer] = init_url
|
19
|
+
f.headers[:referer] = @init_url
|
25
20
|
f.headers[:user_agent] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:26.0) Gecko/20100101 Firefox/26.0'
|
26
21
|
# f.response :logger
|
27
22
|
# f.use :cookie_jar
|
28
|
-
end
|
23
|
+
end if @url_info
|
29
24
|
end
|
30
25
|
|
26
|
+
#得到产品信息
|
31
27
|
def get_info
|
32
|
-
return '
|
28
|
+
return 'good.url.error' if !@url_info
|
33
29
|
|
34
|
-
response = @conn.get @url
|
30
|
+
response = @conn.get @url_info[:url]
|
35
31
|
|
36
32
|
doc = Nokogiri::HTML(response.body)
|
37
33
|
doc.encoding = 'utf-8'
|
38
34
|
|
35
|
+
#处理标题
|
39
36
|
begin
|
40
37
|
title = title_filter(doc.css('title').first.text)
|
41
38
|
rescue NoMethodError
|
42
|
-
return '
|
39
|
+
return 'good.not.exists'
|
43
40
|
end
|
44
41
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
price = price_filter(doc.css('.J_originalPrice').first.text.strip)
|
51
|
-
# img_src = 'src'
|
42
|
+
#处理价格
|
43
|
+
price_info = get_price(@url_info[:id])
|
44
|
+
if price_info
|
45
|
+
price = price_info[:price]
|
46
|
+
promo_price = price_info[:promo_price]
|
52
47
|
else
|
53
|
-
|
48
|
+
if @url_info[:url].include? 'tmall'
|
49
|
+
price = price_filter(doc.css('.J_originalPrice').first.text.strip)
|
50
|
+
promo_price = 0
|
51
|
+
else
|
52
|
+
price = price_filter(doc.css('em.tb-rmb-num').first.text)
|
53
|
+
promo_price = 0
|
54
|
+
end
|
54
55
|
end
|
55
|
-
|
56
|
+
|
57
|
+
#处理图片
|
58
|
+
images = []
|
56
59
|
doc.css('#J_UlThumb').first.css('img').each do |img|
|
57
60
|
img = img.attr('data-src').to_s == '' ? img.attr('src') : img.attr('data-src')
|
58
61
|
images << image_filter(img)
|
@@ -60,29 +63,35 @@ module TaoBaoApi
|
|
60
63
|
|
61
64
|
{:title => title,
|
62
65
|
:price => price,
|
66
|
+
:promo_price => promo_price,
|
63
67
|
:images => images,
|
64
|
-
:url => @url}
|
68
|
+
:url => @url_info[:url]}
|
65
69
|
end
|
66
70
|
|
67
71
|
private
|
68
72
|
#小图转大图链接
|
69
73
|
def image_filter(img_url)
|
70
|
-
img_url = "#{img_url.split('.jpg').first}.jpg"
|
71
|
-
|
72
|
-
|
73
|
-
img_url = img_url.gsub('60x60','460x460')
|
74
|
-
end
|
75
|
-
|
74
|
+
img_url = "#{img_url.split('.jpg').first}.jpg"
|
75
|
+
img_url = img_url.gsub('60x60','460x460') if img_url.include? '60x60'
|
76
|
+
|
76
77
|
img_url
|
77
78
|
end
|
78
79
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
80
|
+
#过滤url得到产品hash
|
81
|
+
def filter_url(url)
|
82
|
+
ids = /id=(\d+)/.match(url)
|
83
|
+
return false if ids.to_a.size == 0
|
84
|
+
|
85
|
+
id = ids.to_a.last
|
86
|
+
if url.include? 'tmall'
|
87
|
+
url = "http://detail.tmall.com/item.htm?id=#{id}"
|
88
|
+
@init_url = 'http://www.tmall.com'
|
83
89
|
else
|
84
|
-
url =
|
90
|
+
url = "http://item.taobao.com/item.htm?id=#{id}"
|
91
|
+
@init_url = 'http://www.taobao.com'
|
85
92
|
end
|
93
|
+
|
94
|
+
{:url => url,:id => id}
|
86
95
|
end
|
87
96
|
|
88
97
|
#去除标题空格
|
@@ -90,11 +99,26 @@ module TaoBaoApi
|
|
90
99
|
title = title.split('-').first.strip.gsub(' ','') if !title.nil?
|
91
100
|
end
|
92
101
|
|
93
|
-
|
102
|
+
#促销价获取失败的情况下调用此方法
|
94
103
|
def price_filter price
|
95
104
|
price = price.split('-').first.strip if !price.nil? && price.include?('-')
|
105
|
+
end
|
106
|
+
|
107
|
+
#可获取促销价
|
108
|
+
def get_price id
|
109
|
+
response = open("http://a.m.tmall.com/ajax/sku.do?item_id=#{id}").read
|
110
|
+
|
111
|
+
begin
|
112
|
+
good_json =JSON.parse response
|
113
|
+
price_info = good_json['availSKUs'].first.last
|
114
|
+
|
115
|
+
return {:price => price_info['price'].to_s,
|
116
|
+
:promo_price =>price_info['promoPrice'].to_s}
|
117
|
+
rescue JSON::ParserError
|
118
|
+
'get.price.error'
|
119
|
+
end
|
96
120
|
|
97
|
-
|
121
|
+
false
|
98
122
|
end
|
99
123
|
|
100
124
|
end
|
data/lib/TaoBaoApi/version.rb
CHANGED
data/test.rb
CHANGED
@@ -5,8 +5,8 @@ require File.join(File.dirname(__FILE__), 'lib', 'TaoBaoApi')
|
|
5
5
|
# url = 'http://item.taobao.com/item.htm?spm=a1z09.5.0.0.x2gUPA&id=16718391999'
|
6
6
|
# url = 'http://item.taobao.com/item.htm?id=20132398689'
|
7
7
|
# url ='http://detail.tmall.com/item.htm?id=9153380600'
|
8
|
-
|
9
|
-
url = 'http://item.taobao.com/item.htm?spm=0.0.0.0.g5BwfC&id=18769075753'
|
8
|
+
url ='http://detail.tmall.com/item.htm?id=17350703554&spm=a1z09.5.0.0.PCBxNl'
|
9
|
+
# url = 'http://item.taobao.com/item.htm?spm=0.0.0.0.g5BwfC&id=18769075753'
|
10
10
|
good = TaoBaoApi::Good.new url
|
11
11
|
|
12
12
|
info = good.get_info
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: TaoBaoApi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- wikimo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|