tida_sina_weibo 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
@@ -7,14 +7,12 @@ module TidaSinaWeibo
|
|
7
7
|
|
8
8
|
attr_reader :access_token
|
9
9
|
attr_reader :request_data
|
10
|
-
attr_reader :token_provider
|
11
10
|
attr_reader :rest_client
|
12
11
|
|
13
|
-
def initialize(
|
12
|
+
def initialize(token, rc = nil)
|
14
13
|
rc = RestClientWrapper.new if rc.nil?
|
15
14
|
@rest_client = rc
|
16
|
-
@
|
17
|
-
@access_token = tp.token
|
15
|
+
@access_token = token
|
18
16
|
end
|
19
17
|
|
20
18
|
def status_update(content)
|
@@ -90,12 +88,6 @@ module TidaSinaWeibo
|
|
90
88
|
|
91
89
|
def rest_request(url, data, method_post)
|
92
90
|
result, code = do_request url, data, method_post
|
93
|
-
|
94
|
-
while code == -1
|
95
|
-
change_access_token
|
96
|
-
result, code = do_request url, data, method_post
|
97
|
-
end
|
98
|
-
|
99
91
|
return result
|
100
92
|
end
|
101
93
|
|
@@ -105,30 +97,25 @@ module TidaSinaWeibo
|
|
105
97
|
rd[:params] = data[:params].merge(params)
|
106
98
|
|
107
99
|
if method_post
|
108
|
-
|
109
|
-
return r, 0
|
100
|
+
return rest_client.post(url, rd)
|
110
101
|
else
|
111
|
-
|
112
|
-
return r, 0
|
102
|
+
return rest_client.get(url, rd)
|
113
103
|
end
|
114
104
|
rescue RestClientException => e
|
115
105
|
r = e.body
|
116
106
|
api_error_code = r["error_code"].to_i
|
117
107
|
|
118
|
-
if invalid_access_token? api_error_code
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
if reached_account_access_limit? api_error_code
|
123
|
-
return nil, -1
|
108
|
+
if invalid_access_token?(api_error_code) || reached_account_access_limit?(api_error_code)
|
109
|
+
ex = InvalidAccessTokenException.new self.access_token
|
110
|
+
raise ex
|
124
111
|
end
|
125
112
|
|
126
113
|
if reached_ip_access_limit? api_error_code
|
127
|
-
|
128
|
-
raise
|
114
|
+
ex = IPAccessLimitException.new
|
115
|
+
raise ex
|
129
116
|
end
|
130
117
|
|
131
|
-
|
118
|
+
raise e
|
132
119
|
end
|
133
120
|
|
134
121
|
def invalid_access_token?(error_code)
|
@@ -142,14 +129,5 @@ module TidaSinaWeibo
|
|
142
129
|
def reached_ip_access_limit?(error_code)
|
143
130
|
return error_code == 10022
|
144
131
|
end
|
145
|
-
|
146
|
-
def change_access_token
|
147
|
-
self.token_provider.change
|
148
|
-
@access_token = self.token_provider.token
|
149
|
-
if @access_token.nil?
|
150
|
-
e = NoAccessTokenAvaiableException.new
|
151
|
-
raise e
|
152
|
-
end
|
153
|
-
end
|
154
132
|
end
|
155
133
|
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
require "rest-client"
|
2
|
+
require 'tida_sina_weibo/api_settings'
|
3
|
+
|
4
|
+
module TidaSinaWeibo
|
5
|
+
|
6
|
+
class AutoTokenSwitchApiWrapper
|
7
|
+
|
8
|
+
attr_reader :access_token
|
9
|
+
attr_reader :request_data
|
10
|
+
attr_reader :token_provider
|
11
|
+
attr_reader :rest_client
|
12
|
+
|
13
|
+
def initialize(tp, rc = nil)
|
14
|
+
rc = RestClientWrapper.new if rc.nil?
|
15
|
+
@rest_client = rc
|
16
|
+
@token_provider = tp
|
17
|
+
@access_token = tp.token
|
18
|
+
end
|
19
|
+
|
20
|
+
def status_update(content)
|
21
|
+
post_request(ApiSettings.api.status.update, :status => content)
|
22
|
+
end
|
23
|
+
|
24
|
+
def status_upload(content, image_path)
|
25
|
+
post_request(ApiSettings.api.status.upload, :status => content, :pic => File.new(image_path, 'rb'))
|
26
|
+
end
|
27
|
+
|
28
|
+
def status_user_timeline(uid, page = 1, count = 100)
|
29
|
+
get_request(ApiSettings.api.status.user_timeline, :params => {:uid => uid, :page => page, :count => count})
|
30
|
+
end
|
31
|
+
|
32
|
+
def status_repost_timeline(post_id, page = 1)
|
33
|
+
get_request(ApiSettings.api.status.repost_timeline, :params => {:id => post_id, :page => page, :count => 50})
|
34
|
+
end
|
35
|
+
|
36
|
+
def comments_show(post_id, page = 1)
|
37
|
+
get_request(ApiSettings.api.comments.show, :params => {:id => post_id, :page => page, :count => 50})
|
38
|
+
end
|
39
|
+
|
40
|
+
def status_show(wb_post_id)
|
41
|
+
get_request(ApiSettings.api.status.show, :params => {:id => wb_post_id})
|
42
|
+
end
|
43
|
+
|
44
|
+
def user_show(uid, is_number_id = true)
|
45
|
+
if is_number_id
|
46
|
+
get_request(ApiSettings.api.user.show, {:params => {:uid => uid}})
|
47
|
+
else
|
48
|
+
get_request(ApiSettings.api.user.show, {:params => {:screen_name => uid}})
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def user_domain_show(domain)
|
53
|
+
get_request(ApiSettings.api.user.domain_show, :params => {:domain => domain})
|
54
|
+
end
|
55
|
+
|
56
|
+
def friendships_followers(uid, cursor)
|
57
|
+
get_request(ApiSettings.api.friendships.followers, :params => {:uid => uid, :cursor => cursor})
|
58
|
+
end
|
59
|
+
|
60
|
+
def friendships_followers_ids(uid, cursor)
|
61
|
+
get_request(ApiSettings.api.friendships.followers_ids, :params => {:uid => uid, :cursor => cursor})
|
62
|
+
end
|
63
|
+
|
64
|
+
def tags(uid)
|
65
|
+
get_request(ApiSettings.api.tags, :params => {:uid => uid})
|
66
|
+
end
|
67
|
+
|
68
|
+
def queryid(mid)
|
69
|
+
get_request(ApiSettings.api.status.queryid, {:params => {:mid => mid, :type => 1, :isBase62 => 1}})
|
70
|
+
end
|
71
|
+
|
72
|
+
def querymid(id, batch = false)
|
73
|
+
get_request(ApiSettings.api.status.querymid, {:params => {:id => id, :type => 1, :is_batch => batch ? 1 : 0}})
|
74
|
+
end
|
75
|
+
|
76
|
+
def querymids(ids)
|
77
|
+
ids_str = ids.join(',')
|
78
|
+
querymid(ids_str, true)
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def post_request(url, data)
|
84
|
+
rest_request(url, data, true)
|
85
|
+
end
|
86
|
+
|
87
|
+
def get_request(url, data)
|
88
|
+
rest_request(url, data, false)
|
89
|
+
end
|
90
|
+
|
91
|
+
def rest_request(url, data, method_post)
|
92
|
+
result, code = do_request url, data, method_post
|
93
|
+
|
94
|
+
while code == -1
|
95
|
+
change_access_token
|
96
|
+
result, code = do_request url, data, method_post
|
97
|
+
end
|
98
|
+
|
99
|
+
return result
|
100
|
+
end
|
101
|
+
|
102
|
+
def do_request(url, data, method_post)
|
103
|
+
rd = data.clone
|
104
|
+
params = {:access_token => @access_token}
|
105
|
+
rd[:params] = data[:params].merge(params)
|
106
|
+
|
107
|
+
if method_post
|
108
|
+
r = rest_client.post(url, rd)
|
109
|
+
return r, 0
|
110
|
+
else
|
111
|
+
r = rest_client.get(url, rd)
|
112
|
+
return r, 0
|
113
|
+
end
|
114
|
+
rescue RestClientException => e
|
115
|
+
r = e.body
|
116
|
+
api_error_code = r["error_code"].to_i
|
117
|
+
|
118
|
+
if invalid_access_token? api_error_code
|
119
|
+
return nil, -1
|
120
|
+
end
|
121
|
+
|
122
|
+
if reached_account_access_limit? api_error_code
|
123
|
+
return nil, -1
|
124
|
+
end
|
125
|
+
|
126
|
+
if reached_ip_access_limit? api_error_code
|
127
|
+
e = IPAccessLimitException.new self.access_token
|
128
|
+
raise e
|
129
|
+
end
|
130
|
+
|
131
|
+
return nil, -2
|
132
|
+
end
|
133
|
+
|
134
|
+
def invalid_access_token?(error_code)
|
135
|
+
return error_code == 21332 || error_code == 21327
|
136
|
+
end
|
137
|
+
|
138
|
+
def reached_account_access_limit?(error_code)
|
139
|
+
return error_code == 10023 || error_code == 10024
|
140
|
+
end
|
141
|
+
|
142
|
+
def reached_ip_access_limit?(error_code)
|
143
|
+
return error_code == 10022
|
144
|
+
end
|
145
|
+
|
146
|
+
def change_access_token
|
147
|
+
self.token_provider.change
|
148
|
+
@access_token = self.token_provider.token
|
149
|
+
if @access_token.nil?
|
150
|
+
e = NoAccessTokenAvaiableException.new
|
151
|
+
raise e
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
@@ -25,8 +25,14 @@ module TidaSinaWeibo
|
|
25
25
|
end
|
26
26
|
|
27
27
|
class IPAccessLimitException < Exception
|
28
|
+
def initialize
|
29
|
+
super "IP access limit reached"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class InvalidAccessTokenException < Exception
|
28
34
|
def initialize(token)
|
29
|
-
super "The access token #{token}
|
35
|
+
super "The access token #{token} is not valid"
|
30
36
|
end
|
31
37
|
end
|
32
38
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tida_sina_weibo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- lib/tida_sina_weibo.rb
|
81
81
|
- lib/tida_sina_weibo/api_settings.rb
|
82
82
|
- lib/tida_sina_weibo/api_wrapper.rb
|
83
|
+
- lib/tida_sina_weibo/auto_token_switch_api_wrapper.rb
|
83
84
|
- lib/tida_sina_weibo/exceptions.rb
|
84
85
|
- lib/tida_sina_weibo/rest_client_wrapper.rb
|
85
86
|
- lib/tida_sina_weibo/version.rb
|