-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenapi.py
331 lines (264 loc) · 10.8 KB
/
openapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# coding: utf-8
import json
import urllib
from utils import generate_signature, quote, random_string,\
timestamp, to_string, get_request_url, safe_value
V = '1'
KUAIPAN_API_URL_ROOT = 'openapi.kuaipan.cn/'
REQUEST_TOKEN_BASE_URL = 'https://openapi.kuaipan.cn/open/requestToken'
ACCESS_TOKEN_BASE_URL = 'https://openapi.kuaipan.cn/open/accessToken'
AUTHORIZE_URL = 'https://www.kuaipan.cn/api.php?ac=open&op=authorise&oauth_token=%s'
ACCOUNT_INFO_BASE_URL = 'http://openapi.kuaipan.cn/1/account_info'
METADATA_BASE_URL = 'http://openapi.kuaipan.cn/1/metadata/%s/%s'
SHARE_BASE_URL = 'http://openapi.kuaipan.cn/1/shares/%s/%s'
FILEOPS_BASE_URL = 'http://openapi.kuaipan.cn/1/fileops/'
FILEOPS_CREATE_BASE_URL = FILEOPS_BASE_URL + 'create_folder'
FILEOPS_DELETE_BASE_URL = FILEOPS_BASE_URL + 'delete'
FILEOPS_MOVE_BASE_URL = FILEOPS_BASE_URL + 'move'
FILEOPS_COPY_BASE_URL = FILEOPS_BASE_URL + 'copy'
FILEOPS_DOWNLOAD_BASE_URL = 'http://api-content.dfs.kuaipan.cn/1/fileops/download_file'
FILEOPS_UPLOAD_LOCATE_BASE_URL = 'http://api-content.dfs.kuaipan.cn/1/fileops/upload_locate'
class OpenAPIError(Exception):
pass
class OpenAPIHTTPError(OpenAPIError):
def __init__(self, status, msg):
self.status = status
self.msg = msg
def __str__(self):
return "%s - %s" % (self.status, self.msg)
class OpenAPIArgumentError(OpenAPIError):
pass
class kpOpenAPI(object):
VERSION = "1.0"
SIG_METHOD = "HMAC-SHA1"
def __init__(self, consumer_key, consumer_secret):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
def requestToken(self, callback=None):
parameters = self._oauth_parameter(has_token=False)
if callback:
parameters['oauth_callback'] = callback
base_url = REQUEST_TOKEN_BASE_URL
s = self._sig_request_url(base_url, parameters)
rf = urllib.urlopen(s)
status = rf.getcode()
if status == 200:
d = json.loads(rf.read())
for k in (u"oauth_token",
u"oauth_token_secret",
u"oauth_callback_confirmed"):
if d.has_key(k):
v = d.get(k)
setattr(self, to_string(k), safe_value(v))
else:
raise OpenAPIHTTPError(status, rf.read())
def authorize(self):
s = AUTHORIZE_URL % self.oauth_token
return s
def accessToken(self):
parameters = self._oauth_parameter()
base_url = ACCESS_TOKEN_BASE_URL
s = self._sig_request_url(base_url, parameters)
rf = urllib.urlopen(s)
status = rf.getcode()
if status == 200:
d = json.loads(rf.read())
for k in (u"oauth_token",
u"oauth_token_secret",
u"user_id",
u"charged_dir"):
if d.has_key(k):
v = d.get(k)
setattr(self, to_string(k), safe_value(v))
else:
raise OpenAPIHTTPError(status, rf.read())
def account_info(self):
parameters = self._oauth_parameter()
base_url = ACCOUNT_INFO_BASE_URL
s = self._sig_request_url(base_url, parameters)
rf = urllib.urlopen(s)
status = rf.getcode()
if status == 200:
d = json.loads(rf.read())
for k in (u"max_file_size",
u"user_name",
u"user_id",
u"quota_total",
u"quota_used",
u"quota_recycled"):
if d.has_key(k):
v = d.get(k)
setattr(self, to_string(k), safe_value(v))
else:
raise OpenAPIHTTPError(status, rf.read())
def metadata(self, **kwargs):
parameters = self._oauth_parameter()
if not kwargs.get('list', True):
parameters['list'] = False
if kwargs.get('file_limit', 10000) < 10000:
parameters['list'] = kwargs['file_limit']
if kwargs.get('page', 0):
parameters['list'] = kwargs['page']
if kwargs.get('page_size', 20) != 20:
parameters['page_size'] = kwargs['page_size']
if kwargs.has_key('filter_ext'):
parameters['filter_ext'] = kwargs['filter_ext']
if kwargs.get('sort_by', '') and (
kwargs['sort_by'] in ('time', 'rtime', 'name', 'rname', 'size', 'rsize')):
parameters['sort_by'] = kwargs['sort_by']
root = kwargs.get('root', 'app_folder')
path = quote(kwargs.get('path', ''))
base_url = METADATA_BASE_URL % (root, path)
s = self._sig_request_url(base_url, parameters)
rf = urllib.urlopen(s)
status = rf.getcode()
if status == 200:
js = rf.read()
return js
else:
raise OpenAPIHTTPError(status, rf.read())
def shares(self, path, root='app_folder'):
parameters = self._oauth_parameter()
if root not in ('app_folder', 'kuaipan'):
root = 'app_folder'
path = quote(path)
base_url = SHARE_BASE_URL % (root, path)
s = self._sig_request_url(base_url, parameters)
rf = urllib.urlopen(s)
status = rf.getcode()
if status == 200:
d = json.loads(rf.read())
return to_string(d[u'url'])
else:
raise OpenAPIHTTPError(status, rf.read())
def create_folder(self, path, root='app_folder'):
if root not in ('app_folder', 'kuaipan'):
root = 'app_folder'
if type(path) not in (str, unicode) or not path :
raise OpenAPIArgumentError
parameters = self._oauth_parameter()
parameters['root'] = root
parameters['path'] = to_string(path)
base_url = FILEOPS_CREATE_BASE_URL
s = self._sig_request_url(base_url, parameters)
rf = urllib.urlopen(s)
status = rf.getcode()
if status == 200:
js = rf.read()
return js
else:
raise OpenAPIHTTPError(status, rf.read())
def delete(self, path, root='app_folder', to_recycle=True):
if root not in ('app_folder', 'kuaipan'):
root = 'app_folder'
if type(path) not in (str, unicode) or not path:
raise OpenAPIArgumentError
parameters = self._oauth_parameter()
parameters['root'] = root
parameters['path'] = to_string(path)
if not to_recycle:
parameters['to_recycle'] = False
base_url = FILEOPS_DELETE_BASE_URL
s = self._sig_request_url(base_url, parameters)
rf = urllib.urlopen(s)
status = rf.getcode()
if status == 200:
return True
else:
raise OpenAPIHTTPError(status, rf.read())
def move(self, from_path, to_path, root='app_folder'):
if root not in ('app_folder', 'kuaipan'):
root = 'app_folder'
if type(from_path) not in (str, unicode) or not from_path:
raise OpenAPIArgumentError
if type(from_path) not in (str, unicode) or not to_path:
raise OpenAPIArgumentError
parameters = self._oauth_parameter()
parameters['root'] = root
parameters['from_path'] = to_string(from_path)
parameters['to_path'] = to_string(to_path)
base_url = FILEOPS_MOVE_BASE_URL
s = self._sig_request_url(base_url, parameters)
rf = urllib.urlopen(s)
status = rf.getcode()
if status == 200:
return True
else:
raise OpenAPIHTTPError(status, rf.read())
def copy(self, from_path, to_path, root='app_folder'):
if root not in ('app_folder', 'kuaipan'):
root = 'app_folder'
if type(from_path) not in (str, unicode) or not from_path:
raise OpenAPIArgumentError
if type(from_path) not in (str, unicode) or not to_path:
raise OpenAPIArgumentError
parameters = self._oauth_parameter()
parameters['root'] = root
parameters['from_path'] = to_string(from_path)
parameters['to_path'] = to_string(to_path)
base_url = FILEOPS_COPY_BASE_URL
s = self._sig_request_url(base_url, parameters)
rf = urllib.urlopen(s)
status = rf.getcode()
if status == 200:
d = json.loads(rf.read())
return to_string(d[u'file_id'])
else:
raise OpenAPIHTTPError(status, rf.read())
def upload_locate(self, source_ip=None):
parameters = self._oauth_parameter()
if source_ip:
parameters['source_ip'] = source_ip
base_url = FILEOPS_UPLOAD_LOCATE_BASE_URL
s = self._sig_request_url(base_url, parameters)
rf = urllib.urlopen(s)
status = rf.getcode()
if status == 200:
d = json.loads(rf.read())
return to_string(d[u'url'])
else:
raise OpenAPIHTTPError(status, rf.read())
def upload_file(self, path, overwrite=True, root='app_folder'):
if root not in ('app_folder', 'kuaipan'):
root = 'app_folder'
if type(path) not in (str, unicode) or not path:
raise OpenAPIArgumentError
parameters = self._oauth_parameter()
parameters['overwrite'] = overwrite
parameters['root'] = root
parameters['path'] = to_string(path)
base_url = self.upload_locate() + '/1/fileops/upload_file'
s = self._sig_request_url(base_url, parameters, 'post')
return s
def download_file(self, path, root='app_folder', rev=''):
if root not in ('app_folder', 'kuaipan'):
root = 'app_folder'
if type(path) not in (str, unicode) or not path:
raise OpenAPIArgumentError
parameters = self._oauth_parameter()
parameters['root'] = root
parameters['path'] = path
base_url = FILEOPS_DOWNLOAD_BASE_URL
s = self._sig_request_url(base_url, parameters)
return s
def _oauth_parameter(self, has_token=True):
parameters = {
'oauth_consumer_key' : self.consumer_key,
'oauth_timestamp' : timestamp(),
'oauth_nonce' : random_string(),
'oauth_signature_method' : self.SIG_METHOD,
'oauth_version' : self.VERSION,
}
if has_token:
parameters['oauth_token'] = self.oauth_token
return parameters
def _secret_key(self, has_token=True):
s = self.consumer_secret + '&'
if has_token:
s += self.oauth_token_secret
return s
def _sig_request_url(self, base, p, method='get'):
has_token = True if p.has_key('oauth_token') else False
oauth_signature = generate_signature(base,
p, self._secret_key(has_token=has_token), method)
return get_request_url(base, p, oauth_signature)