|
14 | 14 | import ssl |
15 | 15 |
|
16 | 16 | ssl._create_default_https_context = ssl._create_unverified_context |
17 | | -global dist_path |
18 | | -dist_path = '' |
| 17 | +global dist_path, url, user_agent, headers, cookie, opener, err404 |
| 18 | +dist_path = url = user_agent = headers = cookie = opener = err404 = None |
19 | 19 |
|
20 | | -url = 'https://learn.tsinghua.edu.cn' |
21 | | -user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' |
22 | | -headers = {'User-Agent': user_agent, 'Connection': 'keep-alive'} |
23 | | -cookie = http.cookiejar.MozillaCookieJar() |
24 | | -handler = urllib.request.HTTPCookieProcessor(cookie) |
25 | | -opener = urllib.request.build_opener(handler) |
26 | | -urllib.request.install_opener(opener) |
27 | | -err404 = '\r\n\r\n\r\n<script type="text/javascript">\r\n\tlocation.href="/";\r\n</script>' |
28 | 20 |
|
| 21 | +def build_global(args): |
| 22 | + global dist_path, url, user_agent, headers, cookie, opener, err404 |
| 23 | + dist_path = args.dist |
| 24 | + url = 'https://learn.tsinghua.edu.cn' |
| 25 | + user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' |
| 26 | + headers = {'User-Agent': user_agent, 'Connection': 'keep-alive'} |
| 27 | + handlers = [] |
| 28 | + if args.http_proxy: |
| 29 | + handlers.append(urllib.request.ProxyHandler({'http': args.http_proxy})) |
| 30 | + if args.https_proxy: |
| 31 | + handlers.append(urllib.request.ProxyHandler({'https': args.https_proxy})) |
| 32 | + cookie = http.cookiejar.MozillaCookieJar() |
| 33 | + handlers.append(urllib.request.HTTPCookieProcessor(cookie)) |
| 34 | + opener = urllib.request.build_opener(*handlers) |
| 35 | + urllib.request.install_opener(opener) |
| 36 | + err404 = '\r\n\r\n\r\n<script type="text/javascript">\r\n\tlocation.href="/";\r\n</script>' |
29 | 37 |
|
30 | 38 | def get_xsrf_token(): |
31 | 39 | cookie_obj = cookie._cookies.get('learn.tsinghua.edu.cn', dict()).get('/', dict()).get('XSRF-TOKEN', None) |
@@ -435,13 +443,16 @@ def get_args(): |
435 | 443 | parser.add_argument('-p', "--_pass", type=str, default='.pass') |
436 | 444 | parser.add_argument('-c', "--cookie", type=str, default='', help='Netscape HTTP Cookie File') |
437 | 445 | parser.add_argument('-d', '--dist', type=str, default='', help='download path') |
| 446 | + parser.add_argument('--http_proxy', type=str, default='', help='http proxy') |
| 447 | + parser.add_argument('--https_proxy', type=str, default='', help='https proxy') |
438 | 448 | args = parser.parse_args() |
439 | 449 | return args |
440 | 450 |
|
441 | 451 |
|
442 | 452 | def main(args): |
443 | 453 | global dist_path |
444 | | - dist_path = args.dist |
| 454 | + build_global(args) |
| 455 | + assert (dist_path is not None) and (url is not None) and (user_agent is not None) and (headers is not None) and (cookie is not None) and (opener is not None) and (err404 is not None) |
445 | 456 | if args.clear: |
446 | 457 | clear(args) |
447 | 458 | exit() |
|
0 commit comments