-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
yunpan.360.cn.py
executable file
·353 lines (318 loc) · 12.1 KB
/
yunpan.360.cn.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
#!/usr/bin/env python2
# vim: set fileencoding=utf8
import os
import sys
from getpass import getpass
import requests
import urllib
import json
import re
import time
import argparse
import random
import md5
############################################################
# wget exit status
wget_es = {
0: "No problems occurred.",
2: "User interference.",
1<<8: "Generic error code.",
2<<8: "Parse error - for instance, when parsing command-line " \
"optio.wgetrc or .netrc...",
3<<8: "File I/O error.",
4<<8: "Network failure.",
5<<8: "SSL verification failure.",
6<<8: "Username/password authentication failure.",
7<<8: "Protocol errors.",
8<<8: "Server issued an error response."
}
############################################################
s = '\x1b[%d;%dm%s\x1b[0m' # terminual color template
cookie_file = os.path.join(os.path.expanduser('~'), '.360.cookies')
headers = {
"Accept":"text/html,application/xhtml+xml,application/xml; " \
"q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding":"text/html",
"Accept-Language":"en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2",
"Content-Type":"application/x-www-form-urlencoded",
"Referer":"http://yunpan.360.cn/",
"X-Requested-With":"XMLHttpRequest",
"User-Agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 "\
"(KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36"
}
ss = requests.session()
ss.headers.update(headers)
class yunpan360(object):
def init(self):
if os.path.exists(cookie_file):
try:
t = json.loads(open(cookie_file).read())
ss.cookies.update(t.get('cookies', t))
if not self.check_login():
print s % (1, 91, ' !! cookie is invalid, please login\n')
sys.exit(1)
except:
g = open(cookie_file, 'w')
g.close()
print s % (1, 97, ' please login')
sys.exit(1)
else:
print s % (1, 91, ' !! cookie_file is missing, please login')
sys.exit(1)
def get_path(self, url):
url = urllib.unquote_plus(url)
f = re.search(r'#(.+?)(&|$)', url)
if f:
return f.group(1)
else:
return '/'
def check_login(self):
#print s % (1, 97, '\n -- check_login')
url = 'http://yunpan.360.cn/user/login?st=774'
r = ss.get(url)
self.save_cookies()
if r.ok:
#print s % (1, 92, ' -- check_login success\n')
# get apihost
self.apihost = re.search(r'http://(.+?)/', r.url).group(1).encode('utf8')
self.save_cookies()
return True
else:
print s % (1, 91, ' -- check_login fail\n')
return False
def login(self, username, password):
print s % (1, 97, '\n -- login')
# get token
params = {
"o": "sso",
"m": "getToken",
"func": "QHPass.loginUtils.tokenCallback",
"userName": username,
"rand": random.random()
}
url = 'https://login.360.cn'
r = ss.get(url, params=params)
token = re.search(r'token":"(.+?)"', r.content).group(1)
# now loin
params = {
"o": "sso",
"m": "login",
"requestScema": "http",
"from": "pcw_cloud",
"rtype": "data",
"func": "QHPass.loginUtils.loginCallback",
"userName": username,
"pwdmethod": 1,
"isKeepAlive": 0,
"token": token,
"captFlag": 1,
"captId": "i360",
"captCode": "",
"lm": 0,
"validatelm": 0,
"password": md5.new(password).hexdigest(),
"r": int(time.time()*1000)
}
url = 'https://login.360.cn'
ss.get(url, params=params)
self.save_cookies()
def save_cookies(self):
with open(cookie_file, 'w') as g:
c = {'cookies': ss.cookies.get_dict()}
g.write(json.dumps(c, indent=4, sort_keys=True))
def get_dlink(self, i):
data = 'nid=%s&fname=%s&' % (i['nid'].encode('utf8'), \
urllib.quote_plus(i['path'].encode('utf8')))
apiurl = 'http://%s/file/download' % self.apihost
r = ss.post(apiurl, data=data)
j = r.json()
if j['errno'] == 0:
dlink = j['data']['download_url'].encode('utf8')
return dlink
def fix_json(self, ori):
# 万恶的 360,返回的json尽然不合法。
jdata = re.search(r'data:\s*\[.+?\]', ori).group()
jlist = re.split(r'\}\s*,\s*\{', jdata)
jlist = [l for l in jlist if l.strip()]
j = []
for item in jlist:
nid = re.search(r',nid: \'(\d+)\'', item)
path = re.search(r',path: \'(.+?)\',nid', item)
name = re.search(r'oriName: \'(.+?)\',path', item)
isdir = 'isDir: ' in item
if nid:
t = {
'nid': nid.group(1),
'path': path.group(1).replace("\\'", "'"),
'name': name.group(1).replace("\\'", "'"),
'isdir': 1 if isdir else 0
}
j.append(t)
return j
def get_infos(self):
apiurl = 'http://%s/file/list' % self.apihost
data = "type" + "=2" + "&" \
"t" + "=%s" % random.random() + "&" \
"order" + "=asc" + "&" \
"field" + "=file_name" + "&" \
"path" + "=%s" + "&" \
"page" + "=0" + "&" \
"page_size" + "=10000" + "&" \
"ajax" + "=1"
dir_loop = [self.path]
base_dir = os.path.split(self.path[:-1])[0] if self.path[-1] == '/' \
and self.path != '/' else os.path.split(self.path)[0]
for d in dir_loop:
data = data % urllib.quote_plus(d)
r = ss.post(apiurl, data=data)
j = self.fix_json(r.text.strip())
if j:
if args.type_:
j = [x for x in j if x['isdir'] \
or x['name'][-len(args.type_):] \
== unicode(args.type_)]
total_file = len([i for i in j if not i['isdir']])
if args.from_ - 1:
j = j[args.from_-1:] if args.from_ else j
nn = args.from_
for i in j:
if i['isdir']:
dir_loop.append(i['path'].encode('utf8'))
else:
t = i['path'].encode('utf8')
t = t.replace(base_dir, '')
t = t[1:] if t[0] == '/' else t
t = os.path.join(os.getcwd(), t)
infos = {
'file': t,
'dir_': os.path.split(t)[0],
'dlink': self.get_dlink(i),
'name': i['name'].encode('utf8'),
'apihost': self.apihost,
'nn': nn,
'total_file': total_file
}
nn += 1
self.download(infos)
else:
print s % (1, 91, ' error: get_infos')
sys.exit(0)
@staticmethod
def download(infos):
#### !!!! 注意:360不支持断点续传
## make dirs
if not os.path.exists(infos['dir_']):
os.makedirs(infos['dir_'])
else:
if os.path.exists(infos['file']):
return 0
num = random.randint(0, 7) % 8
col = s % (2, num + 90, infos['file'])
infos['nn'] = infos['nn'] if infos.get('nn') else 1
infos['total_file'] = infos['total_file'] if infos.get('total_file') else 1
print '\n ++ 正在下载: #', s % (1, 97, infos['nn']), '/', s % (1, 97, infos['total_file']), '#', col
cookie = '; '.join(['%s=%s' % (x, y) for x, y in ss.cookies.items()]).encode('utf8')
if args.aria2c:
if args.limit:
cmd = 'aria2c -c -s10 -x10 ' \
'--max-download-limit %s ' \
'-o "%s.tmp" -d "%s" ' \
'--user-agent "%s" ' \
'--header "Cookie:%s" ' \
'--header "Referer:http://%s/" "%s"' \
% (args.limit, infos['name'], infos['dir_'],\
headers['User-Agent'], cookie, infos['apihost'], infos['dlink'])
else:
cmd = 'aria2c -c -s10 -x10 ' \
'-o "%s.tmp" -d "%s" --user-agent "%s" ' \
'--header "Cookie:%s" ' \
'--header "Referer:http://%s/" "%s"' \
% (infos['name'], infos['dir_'], headers['User-Agent'], \
cookie, infos['apihost'], infos['dlink'])
else:
if args.limit:
cmd = 'wget -c --limit-rate %s ' \
'-O "%s.tmp" --user-agent "%s" ' \
'--header "Cookie:%s" ' \
'--header "Referer:http://%s/" "%s"' \
% (args.limit, infos['file'], headers['User-Agent'], \
cookie, infos['apihost'], infos['dlink'])
else:
cmd = 'wget -c -O "%s.tmp" --user-agent "%s" ' \
'--header "Cookie:%s" ' \
'--header "Referer:http://%s/" "%s"' \
% (infos['file'], headers['User-Agent'], \
cookie, infos['apihost'], infos['dlink'])
status = os.system(cmd)
if status != 0: # other http-errors, such as 302.
wget_exit_status_info = wget_es[status]
print('\n\n ---### \x1b[1;91mERROR\x1b[0m ==> '\
'\x1b[1;91m%d (%s)\x1b[0m ###--- \n\n' \
% (status, wget_exit_status_info))
print s % (1, 91, ' ===> '), cmd
sys.exit(1)
else:
os.rename('%s.tmp' % infos['file'], infos['file'])
def exists(self, filepath):
pass
def upload(self, path, dir_):
pass
def addtask(self):
pass
def do(self):
self.get_infos()
def main(argv):
if len(argv) <= 1:
sys.exit()
######################################################
# for argparse
p = argparse.ArgumentParser(description='download from yunpan.360.com')
p.add_argument('xxx', type=str, nargs='*', \
help='命令对象.')
p.add_argument('-a', '--aria2c', action='store_true', \
help='download with aria2c')
p.add_argument('-p', '--play', action='store_true', \
help='play with mpv')
p.add_argument('-f', '--from_', action='store', \
default=1, type=int, \
help='从第几个开始下载,eg: -f 42')
p.add_argument('-t', '--type_', action='store', \
default=None, type=str, \
help='要下载的文件的后缀,eg: -t mp3')
p.add_argument('-l', '--limit', action='store', \
default=None, type=str, help='下载速度限制,eg: -l 100k')
global args
args = p.parse_args(argv[1:])
xxx = args.xxx
if xxx[0] == 'login' or xxx[0] == 'g':
if len(xxx[1:]) < 1:
username = raw_input(s % (1, 97, ' username: '))
password = getpass(s % (1, 97, ' password: '))
elif len(xxx[1:]) == 1:
username = xxx[1]
password = getpass(s % (1, 97, ' password: '))
elif len(xxx[1:]) == 2:
username = xxx[1]
password = xxx[2]
else:
print s % (1, 91, ' login\n login username\n login username password')
x = yunpan360()
x.login(username, password)
is_signin = x.check_login()
if is_signin:
print s % (1, 92, ' ++ login succeeds.')
else:
print s % (1, 91, ' login failes')
elif xxx[0] == 'signout':
g = open(cookie_file, 'w')
g.close()
else:
urls = xxx
x = yunpan360()
x.init()
for url in urls:
x.path = x.get_path(url)
x.do()
if __name__ == '__main__':
argv = sys.argv
main(argv)