forked from Macuyiko/weiyun-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weiyun.py
390 lines (363 loc) · 12.5 KB
/
weiyun.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
379
380
381
382
383
384
385
386
387
388
389
390
import requests
import uuid
import json
import time
import os
import hashlib
import sys
import struct
import binascii
# Change the values below:
SKEY = '@Aci4ImJF0'
UIN = '2956912470'
PTCZ = '97f739d9b2ef812ced8e124937a6902b3c6fe1f2ac007bb847d956e893312e93'
PGV_INFO = 'ssid=s9869263628'
PGV_PVID = '4842874884'
EMAIL = '[email protected]'
current_milli_time = lambda: int(round(time.time() * 1000))
file_size = lambda x: os.stat(x).st_size
get_ordered_tuple = lambda x,y: next(a[1] for a in x if a[0] == y)
md5 = lambda x: hashlib.md5(open(x, 'rb').read()).hexdigest()
sha1 = lambda x: hashlib.sha1(open(x, 'rb').read()).hexdigest()
jsonprint = lambda x: json.dumps(x, sort_keys=True, indent=2, separators=(',', ': '))
def get_token(skey, md5key='tencentQQVIP123443safde&!%^%1282', salt=5381):
thash = []
num = salt<<5
thash.append(str(num))
for i in range(0, len(skey)):
ac = ord(skey[i])
num = (salt<<5)+ac
thash.append(str(num))
salt = ac
return hashlib.md5(''.join(thash)+md5key).hexdigest()
def get_tk(skey, salt=5381):
for i in range(0, len(skey)):
ac = ord(skey[i])
salt += (salt<<5)+ac
return salt&0x7fffffff;
def get_chunk(filename, offset, length=131072):
f = open(filename)
f.seek(offset)
chunk = f.read(length)
f.close()
return chunk
def encode_chunk(ukey, filekey, filename, offset, chunk):
data = ''
data = data + struct.pack('>I', 2882377846)
data = data + struct.pack('>I', 1000)
data = data + struct.pack('>I', 0)
hlen = 2*2 + 4*3 + len(ukey)/2 + len(filekey)/2 + len(chunk)
data = data + struct.pack('>I', hlen)
data = data + struct.pack('>H', len(ukey)/2)
data = data + binascii.unhexlify(ukey)
data = data + struct.pack('>H', len(filekey)/2)
data = data + binascii.unhexlify(filekey)
data = data + struct.pack('>I', file_size(filename))
data = data + struct.pack('>I', offset)
data = data + struct.pack('>I', len(chunk))
data = data + chunk
return data
TOKEN = get_token(SKEY)
GTK = get_tk(SKEY)
HEADERS = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36' \
' (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36',
'Referer': 'http://www.weiyun.com/disk/index-en.html'}
COOKIES = {'pgv_info': PGV_INFO,
'pgv_pvid': PGV_PVID,
'ptui_loginuin': EMAIL,
'pt2gguin': 'o'+UIN,
'uin': 'o'+UIN,
'skey': SKEY,
'ptisp': 'os',
'ptcz': PTCZ}
REQ_HEADER = {"cmd":"",
"main_v":11,
"proto_ver":10006,
"sub_v":1,
"encrypt":0,
"msg_seq":1,
"source":30234,
"appid":30234,
"client_ip":"127.0.0.1",
"token":TOKEN}
class BaseWeiyun(object):
def __init__(self, endpoint, cmd):
self.base = 'http://web.cgi.weiyun.com'
self.endpoint = endpoint
self.cmd = cmd
self.data = {"req_header":REQ_HEADER,
"req_body":{}}
self.data["req_header"]["cmd"] = cmd
def get_response(self):
url = self.base + self.endpoint
payload = self.get_payload()
r = requests.get(url, cookies=COOKIES, params=payload, headers=HEADERS)
response = r.text\
.replace('try{'+get_ordered_tuple(payload,"callback")+'(', '')\
.replace(')} catch(e){};', '')\
.replace(')}catch(e){};', '');
return json.loads(response)
def get_payload(self, isPost=False):
ruuid = str(uuid.uuid4()).upper().replace('-', '_')
prefix = 'post_callback' if isPost else 'get'
payload = (('cmd', self.cmd),
('g_tk', GTK),
('callback', prefix+'_'+ruuid),
('data', json.dumps(self.data)),
('_', str(current_milli_time())))
return payload;
class QueryUser(BaseWeiyun):
def __init__(self):
endpoint = '/wy_web_jsonp.fcg'
cmd = 'query_user'
super(QueryUser, self).__init__(endpoint, cmd)
class RootDirList(BaseWeiyun):
def __init__(self):
endpoint = '/weiyun_web_root_dir_list_cgi.fcg'
cmd = 'root_dir_list'
super(RootDirList, self).__init__(endpoint, cmd)
class GetDirList(BaseWeiyun):
def __init__(self, dirkey, parentdirkey):
endpoint = '/wy_web_jsonp.fcg'
cmd = 'get_dir_list'
super(GetDirList, self).__init__(endpoint, cmd)
self.data["req_body"] = {
"pdir_key":parentdirkey,
"dir_key":dirkey,
"dir_mtime":"2000-01-01 01:01:01",
"only_dir":0}
class DirCreate(BaseWeiyun):
def __init__(self, dirname, parentdirkey, parentparentdirkey):
endpoint = '/wy_web_jsonp.fcg'
cmd = 'dir_create'
super(DirCreate, self).__init__(endpoint, cmd)
self.data["req_body"] = {
"ppdir_key": parentparentdirkey,
"pdir_key": parentdirkey,
"dir_attr": {"dir_name":dirname, "dir_note":""}}
def get_response(self):
url = self.base + self.endpoint
payload = self.get_payload(True)
r = requests.post(url, cookies=COOKIES, params=payload, data=payload, headers=HEADERS)
response = r.text\
.replace('<script>document.domain="weiyun.com";'+\
'try{parent.'+get_ordered_tuple(payload,"callback")+'(', '')\
.replace(')} catch(e){};</script>', '');
return json.loads(response)
class BatchFolderDelete(BaseWeiyun):
def __init__(self, dirnames, dirkeys, parentdirkeys, parentparentdirkeys):
endpoint = '/wy_web_jsonp.fcg'
cmd = 'batch_folder_delete'
super(BatchFolderDelete, self).__init__(endpoint, cmd)
dellist = []
for i in range(0, len(dirnames)):
dellist.append({"ppdir_key":parentparentdirkeys[i],
"pdir_key":parentdirkeys[i],
"dir_key":dirkeys[i],
"flag":1,
"dir_name":dirnames[i]})
self.data["req_body"] = {"del_folders": dellist}
def get_response(self):
url = self.base + self.endpoint
payload = self.get_payload(True)
r = requests.post(url, cookies=COOKIES, params=payload, data=payload, headers=HEADERS)
response = r.text\
.replace('<script>document.domain="weiyun.com";'+\
'try{parent.'+get_ordered_tuple(payload,"callback")+'(', '')\
.replace(')} catch(e){};</script>', '');
return json.loads(response)
class BatchFileDelete(BaseWeiyun):
def __init__(self, fileids, filenames, filevers, parentdirkeys, parentparentdirkeys):
endpoint = '/wy_web_jsonp.fcg'
cmd = 'batch_file_delete'
super(BatchFileDelete, self).__init__(endpoint, cmd)
dellist = []
for i in range(0, len(fileids)):
dellist.append({"ppdir_key":parentparentdirkeys[i],
"pdir_key":parentdirkeys[i],
"file_id":fileids[i],
"file_name":filenames[i],
"file_ver":filevers[i]})
self.data["req_body"] = {"del_files": dellist}
def get_response(self):
url = self.base + self.endpoint
payload = self.get_payload(True)
r = requests.post(url, cookies=COOKIES, params=payload, data=payload, headers=HEADERS)
response = r.text\
.replace('<script>document.domain="weiyun.com";'+\
'try{parent.'+get_ordered_tuple(payload,"callback")+'(', '')\
.replace(')} catch(e){};</script>', '');
return json.loads(response)
class DownloadFile(BaseWeiyun):
def __init__(self, fileid, filename, parentdirkey, checksum):
endpoint = '/wy_down.fcg'
cmd = None
super(DownloadFile, self).__init__(endpoint, cmd)
self.base = 'http://download.cgi.weiyun.com'
self.data = None
self.fileid = fileid
self.filename = filename
self.parentdirkey = parentdirkey
self.checksum = checksum
def get_response(self, stream=True):
url = self.base + self.endpoint
payload = self.get_payload()
headers = HEADERS.copy()
headers['origin'] = 'http://www.weiyun.com'
r = requests.post(url, cookies=COOKIES, params=payload, headers=headers, stream=stream)
return r
def callback(self, ds, sofar, total):
w = 80
sys.stdout.write( "\b" * (w+10) )
speed = ds / ((current_milli_time() - self.tb)/1000)
sys.stdout.write( "[%s%s] %s" %
("#" * (int(w * float(sofar) / float(total))),
" " * (w-(int(w * float(sofar) / float(total)))),
speed) )
def get_file(self, filename, stream=True, callback=None):
if callback is None:
callback = self.callback
a = self.get_response()
self.tb = current_milli_time()-1000
if stream:
total = 0
with open(filename, 'wb') as fd:
ds = 1024*100
for chunk in a.iter_content(ds):
total += len(chunk)
fd.write(chunk)
callback(ds, total, str(a.headers['content-length']))
print
a.close()
else:
with open(filename, 'wb') as fd:
fd.write(a.content)
fd.close()
def get_payload(self):
payload = (('fid', self.fileid),
('pdir', self.parentdirkey),
('fn', self.filename),
('uuin', UIN),
('skey', SKEY),
('appid', REQ_HEADER["appid"]),
('token', TOKEN),
('checksum', self.checksum),
('err_callback', 'http://www.weiyun.com/web/callback/iframe_disk_down_fail.html'),
('ver', REQ_HEADER["main_v"]))
return payload
class FileUpload(BaseWeiyun):
def __init__(self, filename, filesize, parentdirkey, parentparentdirkey, md5="", sha=""):
endpoint = '/upload.fcg'
cmd = 'file_upload'
super(FileUpload, self).__init__(endpoint, cmd)
utype = 1 if md5 == "" else 0
self.data["req_body"] = {"ppdir_key":parentparentdirkey,
"pdir_key":parentdirkey,
"upload_type": utype,
"file_md5":md5,
"file_sha":sha,
"file_size":str(filesize),
"file_attr":{"file_name":filename}}
self.parentdirkey = parentdirkey
self.parentparentdirkey = parentparentdirkey
def send_file(self, filename):
a = self.get_response()
print a
if (a['rsp_body']['file_exist']):
return True
elif self.data["req_body"]["upload_type"] == 0:
# Delete the broken file first, this is a side effect of trying with hashes first
o = BatchFileDelete(
[a['rsp_body']['file_id']],
[self.data["req_body"]["file_attr"]["file_name"]],
[a['rsp_body']['file_ver']],
[self.parentdirkey],
[self.parentparentdirkey])
oo = o.get_response()
req = FileUpload(self.data["req_body"]["file_attr"]["file_name"],
self.data["req_body"]["file_size"],
self.parentdirkey,
self.parentparentdirkey)
return req.send_file(filename)
elif self.data["req_body"]["upload_type"] == 1:
url = 'http://' + a['rsp_body']['upload_svr_host']+':'+\
str(a['rsp_body']['upload_svr_port']) + '/ftn_handler/'+\
'?ver=12345&ukey='+a['rsp_body']['upload_csum']+\
'&filekey='+a['rsp_body']['file_key']+'&'
headers = HEADERS.copy()
headers['origin'] = 'http://www.weiyun.com'
data = {'Filename': self.data["req_body"]["file_attr"]["file_name"],
'mode': 'flashupload',
'file': ('filename', open(filename, 'rb'), 'application/octet-stream'),
'Upload': 'Submit Query'}
r = requests.post(url, headers=headers, files={'file':data['file']})
return r
class ChunkedFileUpload(BaseWeiyun):
def __init__(self, filename, filesize, parentdirkey, parentparentdirkey, fmd5, fsha):
utype = 1 if fmd5 == "" else 0
self.base = 'http://user.weiyun.com/newcgi'
self.endpoint = '/qdisk_upload.fcg'
self.cmd = '2301'
self.data = {"req_header":{
"cmd":int(self.cmd),
"appid":30013,
"version":2,
"major_version":2},
"req_body":{
"ReqMsg_body":{
"weiyun.DiskFileUploadMsgReq_body":{
"ppdir_key":parentparentdirkey,
"pdir_key":parentdirkey,
"upload_type":utype,
"file_md5":fmd5,
"file_sha":fsha,
"file_size":filesize,
"filename":filename,
"file_exist_option":4}}}}
self.parentdirkey = parentdirkey
self.parentparentdirkey = parentparentdirkey
def get_response(self):
url = self.base + self.endpoint
payload = self.get_payload()
headers = HEADERS.copy()
headers['Referer'] = 'http://user.weiyun.com/cdr_proxy.html'
r = requests.get(url, cookies=COOKIES, params=payload, headers=headers)
response = r.text\
.replace('try{X_GET(', '')\
.replace(')}catch(e){};', '');
return json.loads(response)
def get_payload(self):
payload = (('cmd', self.cmd),
('g_tk', GTK),
('data', json.dumps(self.data)),
('callback', 'X_GET'),
('_', str(current_milli_time())))
return payload;
def callback(self, send, total):
print send, total
def send_file(self, filename, callback=None):
if callback is None:
callback = self.callback
a = self.get_response()
rspbody = a['rsp_body']['RspMsg_body']['weiyun.DiskFileUploadMsgRsp_body']
print rspbody
if rspbody['file_exist'] and False:
return True
else:
print ''
url = 'http://' + rspbody['server_name']+':'+\
str(rspbody['server_port']) + '/ftn_handler/'+\
self.data['req_body']['ReqMsg_body']['weiyun.DiskFileUploadMsgReq_body']['file_md5']
headers = HEADERS.copy()
headers['origin'] = 'http://img.weiyun.com'
length = 131072
filekey = rspbody['file_key']
ukey = rspbody['check_key']
for start in range(0, file_size(filename), 131072):
chunk = get_chunk(filename, start, length)
encch = encode_chunk(ukey, filekey, filename, start, chunk)
bmd5 = '?' # TODO: requires conversion of crypto
print bmd5
r = requests.post(url+ '?bmd5=' + bmd5, headers=headers, data = encch)
if callback is not None:
callback(start + length, file_size(filename))