forked from wusuopu/vdisk-fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvdisk_api.py
378 lines (353 loc) · 14.1 KB
/
vdisk_api.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env python
#-*- coding:utf-8 -*-
##
##
# Copyright (C)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation.
# 本程序是免费软件,基于GPL许可发布。
#
# @文件名(file): vdisk_api.py
# @作者(author): 龙昌锦(LongChangjin)
# @博客(blog): http://www.xefan.com
# @邮箱(mail): [email protected]
# @QQ: 346202141
# @时间(date): 2012-06-05
#
#
# 新浪微盘API
import urllib, urllib2
import MultipartPostHandler
import json
import hashlib, hmac
import time
import thread
App_Key = "2375169550"
App_Secret = "657d018b5b50b7f4419ef1d2d6e71ac1"
class VDisk:
def __init__(self, account, password, app_type="local"):
self.opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
urllib2.install_opener(self.opener)
self.token = ''
self.dologid = None
self.dologdir = []
self.get_token(account, password, app_type)
thread.start_new_thread(self.keep_thread, ())
def log(self, msg):
print(msg.encode('utf-8'))
#获取token
def get_token(self, account, password, app_type):
t = int(time.time())
signature = hmac.new(App_Secret, "account=" + account + "&appkey=" +
App_Key + "&password=" + password + "&time=" + `t`, hashlib.sha256).hexdigest()
data = urllib.urlencode({"account":account, "password":password,
"appkey":App_Key, "time":t,
"signature":signature, "app_type":app_type})
url = "http://openapi.vdisk.me/?m=auth&a=get_token"
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
self.token = js['data']['token']
#保持同步
def keep(self):
url = "http://openapi.vdisk.me/?a=keep"
data = urllib.urlencode({"token":self.token})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
#保持token的线程
def keep_thread(self):
while True:
self.keep_token()
time.sleep(600)
#保持token,10-15分钟执行一次
def keep_token(self):
url = "http://openapi.vdisk.me/?m=user&a=keep_token"
data = urllib.urlencode({"token":self.token})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
#文件上传(10M以内)
def upload_file(self, name, is_cover="yes", is_share=False):
if is_share:
url = "http://openapi.vdisk.me/?m=file&a=upload_share_file"
else:
url = "http://openapi.vdisk.me/?m=file&a=upload_file"
data = {"token":self.token, "dir":"/test2",
"cover":is_cover, "file":open(name,"rb")}
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#创建目录
def create_dir(self, dir_name, parent=0):
url = "http://openapi.vdisk.me/?m=dir&a=create_dir"
data = urllib.urlencode({"token":self.token, "create_name":dir_name, "parent_id":parent})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#获得列表
def getlist(self, dir_id=0, page=1, pagesize=1024):
url = "http://openapi.vdisk.me/?m=dir&a=getlist"
data = urllib.urlencode({"token":self.token, "dir_id":dir_id,
"page":page, "pageSize":pagesize})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#获得容量信息
def get_quota(self):
url = "http://openapi.vdisk.me/?m=file&a=get_quota"
data = urllib.urlencode({"token":self.token})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#无文件上传(sha1)
def upload_with_sha1(self, name, sha1, dir_id=0):
url = "http://openapi.vdisk.me/?m=file&a=upload_with_sha1"
data = urllib.urlencode({"token":self.token, "dir_id":dir_id,
"sha1":sha1, "file_name":name})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#获得单个文件信息
def get_file_info(self, fid):
url = "http://openapi.vdisk.me/?m=file&a=get_file_info"
data = urllib.urlencode({"token":self.token, "fid":fid})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#删除目录/文件
def delete_file(self, fid, is_dir=True):
if is_dir:
url = "http://openapi.vdisk.me/?m=dir&a=delete_dir"
data = urllib.urlencode({"token":self.token, "dir_id":fid})
else:
url = "http://openapi.vdisk.me/?m=file&a=delete_file"
data = urllib.urlencode({"token":self.token, "fid":fid})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#复制文件
def copy_file(self, fid, new_name, to_dir_id=0):
url = "http://openapi.vdisk.me/?m=file&a=copy_file"
data = urllib.urlencode({"token":self.token, "fid":fid,
"new_name":new_name, "to_dir_id":to_dir_id})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#重命名
def rename_file(self, fid, new_name, is_dir=True):
if is_dir:
url = "http://openapi.vdisk.me/?m=dir&a=rename_dir"
data = urllib.urlencode({"token":self.token, "dir_id":fid, "new_name":new_name})
else:
url = "http://openapi.vdisk.me/?m=file&a=rename_file"
data = urllib.urlencode({"token":self.token, "fid":fid, "new_name":new_name})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#移动目录/文件
def move_file(self, fid, new_name, to_dir_id=0, is_dir=True):
if is_dir:
url = "http://openapi.vdisk.me/?m=dir&a=move_dir"
data = urllib.urlencode({"token":self.token, "dir_id":fid,
"new_name":new_name, "to_parent_id":to_dir_id})
else:
url = "http://openapi.vdisk.me/?m=file&a=move_file"
data = urllib.urlencode({"token":self.token, "fid":fid,
"new_name":new_name, "to_dir_id":to_dir_id})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#分享文件/取消分享
def share_file(self, fid, to_share=True):
if to_share:
url = "http://openapi.vdisk.me/?m=file&a=share_file"
else:
url = "http://openapi.vdisk.me/?m=file&a=cancel_share_file"
data = urllib.urlencode({"token":self.token, "fid":fid})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#回收站列表
def recycle_list(self, size=10, page=1):
url = "http://openapi.vdisk.me/?m=recycle&a=get_list"
data = urllib.urlencode({"token":self.token, "page":page, "page_size":size})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#清空回收站
def recycle_clean(self):
url = "http://openapi.vdisk.me/?m=recycle&a=truncate_recycle"
data = urllib.urlencode({"token":self.token})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#从回收站删除文件
def recycle_del(self, fid, is_dir=True):
if is_dir:
url = "http://openapi.vdisk.me/?m=recycle&a=delete_dir"
data = urllib.urlencode({"token":self.token, "dir_id":fid})
else:
url = "http://openapi.vdisk.me/?m=recycle&a=delete_file"
data = urllib.urlencode({"token":self.token, "fid":fid})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#从回收站还原文件
def recycle_restore(self, fid, is_dir=True):
if is_dir:
url = "http://openapi.vdisk.me/?m=recycle&a=restore_dir"
data = urllib.urlencode({"token":self.token, "dir_id":fid})
else:
url = "http://openapi.vdisk.me/?m=recycle&a=restore_file"
data = urllib.urlencode({"token":self.token, "fid":fid})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
#通过路径得到目录id
def get_dirid_with_path(self, path="/"):
url = "http://openapi.vdisk.me/?m=dir&a=get_dirid_with_path"
data = urllib.urlencode({"token":self.token, "path":path})
req = urllib2.Request(url, data)
fd = self.opener.open(req)
js = json.loads(fd.read())
fd.close()
if js['err_code'] != 0:
self.log(js['err_msg'])
else:
if self.dologid != js['dologid']: self.dologid = js['dologid']
if self.dologdir != js['dologdir']: self.dologdir = js['dologdir']
return js
if __name__ == "__main__":
app_type = ['sinat', 'local']
i = raw_input("请选择登陆帐号类型:[1]微博帐号 [2]微盘帐号 >")
if i!='1' and i!='2':
print("输入的帐号类型有误!")
exit(1)
user = raw_input("请输入登陆帐号 >")
pswd = raw_input("请输入登陆密码 >")
v = VDisk(user, pswd, app_type[int(i)-1])
print v.get_quota()