git-fit 0.10.11 → 0.11.1
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 +4 -4
- data/lib/git_fit/config_template.rb +2 -0
- data/lib/git_fit/sync/xingzhe.rb +90 -8
- data/lib/git_fit/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 49c616974f43c0eef2a07460e664450cffb887629d63b0e7d9f64e9da73dec09
|
|
4
|
+
data.tar.gz: 8924a27a358440f1f1a2bd1b233d6a11589d5aca0eb9c68be63bd31679e5e9af
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 42ea6e2aaef24cfb9f3aecf136ac365542bca0ce9824ee0bbba2b1e970c28a1f7a3db201be81cd3717a3bc85283c89274051e6ecef7d83df89105599e83a588b
|
|
7
|
+
data.tar.gz: 30f1c4f88ac926e4581159e7a76ed92e0fc88ef764e9145d5901efacbab53baf387bbe7aad2864afdd46ecaa5d548043b1cd9e61ed2ed8009bf169d9d70145f8
|
data/lib/git_fit/sync/xingzhe.rb
CHANGED
|
@@ -22,14 +22,17 @@ module GitFit
|
|
|
22
22
|
|
|
23
23
|
RAW_EXT = 'json'
|
|
24
24
|
|
|
25
|
+
SESSION_TTL = 30 * 24 * 60 * 60
|
|
26
|
+
|
|
25
27
|
register_adapter
|
|
26
28
|
config_key 'xingzhe'
|
|
27
|
-
register_config :email, :password
|
|
29
|
+
register_config :email, :password, :session_path
|
|
28
30
|
|
|
29
31
|
def initialize(...)
|
|
30
32
|
super
|
|
31
33
|
@email = @config['email']
|
|
32
34
|
@password = @config['password']
|
|
35
|
+
@session_path = @config['session_path'] || File.join('data', 'cache', 'xingzhe_session.json')
|
|
33
36
|
end
|
|
34
37
|
|
|
35
38
|
def before_call
|
|
@@ -38,9 +41,15 @@ module GitFit
|
|
|
38
41
|
end
|
|
39
42
|
|
|
40
43
|
def authenticate
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
if load_session && session_valid?
|
|
45
|
+
@auth_method = 'cached'
|
|
46
|
+
return true
|
|
47
|
+
end
|
|
48
|
+
if login
|
|
49
|
+
@auth_method = 'login'
|
|
50
|
+
return true
|
|
51
|
+
end
|
|
52
|
+
false
|
|
44
53
|
end
|
|
45
54
|
|
|
46
55
|
def pending_ids
|
|
@@ -67,6 +76,14 @@ module GitFit
|
|
|
67
76
|
process_activity(pending[:summary], pending[:type])
|
|
68
77
|
end
|
|
69
78
|
|
|
79
|
+
def auth_error?
|
|
80
|
+
[302, 401, 403].include?(@last_http_code)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def last_auth_code
|
|
84
|
+
@last_http_code
|
|
85
|
+
end
|
|
86
|
+
|
|
70
87
|
private
|
|
71
88
|
|
|
72
89
|
def http_request(method, path, body: nil, params: nil, headers: {})
|
|
@@ -85,7 +102,26 @@ module GitFit
|
|
|
85
102
|
http.use_ssl = true
|
|
86
103
|
http.open_timeout = 30
|
|
87
104
|
http.read_timeout = 60
|
|
88
|
-
http.start { |h| h.request(req) }
|
|
105
|
+
resp = http.start { |h| h.request(req) }
|
|
106
|
+
@last_http_code = resp.code.to_i
|
|
107
|
+
resp
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def authed_get(path, params: nil)
|
|
111
|
+
resp = http_request(:get, path, params: params, headers: @headers || {})
|
|
112
|
+
return resp unless auth_failure?(resp)
|
|
113
|
+
|
|
114
|
+
remove_stale_session
|
|
115
|
+
return resp unless login
|
|
116
|
+
|
|
117
|
+
http_request(:get, path, params: params, headers: @headers)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def auth_failure?(resp)
|
|
121
|
+
return false unless resp
|
|
122
|
+
c = resp.code.to_i
|
|
123
|
+
(c == 400 && resp.body.to_s.include?('not allowed')) ||
|
|
124
|
+
[302, 401, 403].include?(c)
|
|
89
125
|
end
|
|
90
126
|
|
|
91
127
|
def login
|
|
@@ -102,10 +138,57 @@ module GitFit
|
|
|
102
138
|
cookie = extract_cookie(resp)
|
|
103
139
|
return false unless cookie
|
|
104
140
|
|
|
141
|
+
@session_expires_at = parse_max_age(resp)
|
|
142
|
+
@headers = { 'Cookie' => cookie, 'Accept' => 'application/json' }
|
|
143
|
+
save_session(cookie)
|
|
144
|
+
true
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def load_session
|
|
148
|
+
return false unless File.exist?(@session_path)
|
|
149
|
+
|
|
150
|
+
data = JSON.parse(File.read(@session_path))
|
|
151
|
+
cookie = data['cookie']
|
|
152
|
+
@session_expires_at = data['expires_at']
|
|
153
|
+
return false unless cookie
|
|
154
|
+
|
|
105
155
|
@headers = { 'Cookie' => cookie, 'Accept' => 'application/json' }
|
|
106
156
|
true
|
|
157
|
+
rescue StandardError => e
|
|
158
|
+
warn "XingZhe: failed to load cached session: #{e.message}"
|
|
159
|
+
remove_stale_session
|
|
160
|
+
false
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def session_valid?
|
|
164
|
+
return false unless @session_expires_at
|
|
165
|
+
Time.now.to_i < @session_expires_at - 60
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def save_session(cookie)
|
|
169
|
+
FileUtils.mkdir_p(File.dirname(@session_path))
|
|
170
|
+
File.write(@session_path, JSON.generate({
|
|
171
|
+
cookie: cookie,
|
|
172
|
+
expires_at: @session_expires_at,
|
|
173
|
+
}))
|
|
174
|
+
rescue StandardError => e
|
|
175
|
+
warn "XingZhe: failed to save session: #{e.message}"
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def remove_stale_session
|
|
179
|
+
File.delete(@session_path) if File.exist?(@session_path)
|
|
180
|
+
rescue StandardError => e
|
|
181
|
+
warn "XingZhe: failed to remove stale session: #{e.message}"
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def parse_max_age(resp)
|
|
185
|
+
set_cookie = resp['set-cookie'].to_s
|
|
186
|
+
ma = set_cookie[/Max-Age=(\d+)/, 1]
|
|
187
|
+
ma ? Time.now.to_i + ma.to_i : Time.now.to_i + SESSION_TTL
|
|
107
188
|
end
|
|
108
189
|
|
|
190
|
+
attr_reader :session_path
|
|
191
|
+
|
|
109
192
|
def rsa_encrypt(password)
|
|
110
193
|
pem = "-----BEGIN PUBLIC KEY-----\n#{RSA_PUBKEY}\n-----END PUBLIC KEY-----"
|
|
111
194
|
key = OpenSSL::PKey::RSA.new(pem)
|
|
@@ -124,9 +207,8 @@ module GitFit
|
|
|
124
207
|
end
|
|
125
208
|
|
|
126
209
|
def fetch_page(offset, limit)
|
|
127
|
-
resp =
|
|
210
|
+
resp = authed_get(WORKOUTS_PATH,
|
|
128
211
|
params: { offset: offset, limit: limit },
|
|
129
|
-
headers: @headers
|
|
130
212
|
)
|
|
131
213
|
return nil unless resp.code.to_i == 200
|
|
132
214
|
|
|
@@ -202,7 +284,7 @@ module GitFit
|
|
|
202
284
|
end
|
|
203
285
|
|
|
204
286
|
def fetch_stream(id)
|
|
205
|
-
resp =
|
|
287
|
+
resp = authed_get("#{STREAM_PATH}#{id}/stream/")
|
|
206
288
|
return nil unless resp.code.to_i == 200
|
|
207
289
|
|
|
208
290
|
JSON.parse(resp.body).dig('data')
|
data/lib/git_fit/version.rb
CHANGED