-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.py
91 lines (78 loc) · 2.49 KB
/
main.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
#!/usr/bin/env python
#coding=utf-8
import sys, os
import time
import requests
try:
import simplejson as json
except:
import json
def save_file(path, data):
f = file(path, 'w')
f.write(data)
f.close()
def get_file_path(parent_dir, chi_list, path_list):
for chi in chi_list:
cur_path = os.path.join(parent_dir, chi['name'])
if chi.has_key('children'):
get_file_path(cur_path, chi['children'], path_list)
else:
path_list.append(cur_path[1:])
def get_file_list():
url = 'https://down.52pojie.cn/list.js'
headers = {
'Host': 'down.52pojie.cn',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
res = requests.get(url, headers = headers)
data = res.content
data = data.replace('__jsonpCallbackDown52PojieCn(', '')
data = data.replace(');', '')
data = json.loads(data)
path_list = []
get_file_path('', [data], path_list)
return path_list
def download(pdir, url):
try:
path = os.path.join(pdir, url)
if os.path.exists(path):
return
dir1 = os.path.dirname(path)
if not os.path.exists(dir1):
os.makedirs(dir1)
url = u'https://down.52pojie.cn/' + url
print 'downloading ', url
res = requests.get(url)
data = res.content
save_file(path, data)
except Exception as e:
print e
def filter_download(dir, filter, path_list):
download_list = []
for path in path_list:
ret = [f for f in filter if path.find(f) != -1]
if len(ret) > 0:
download_list.append(path)
download(dir, path)
data = '\n'.join(path for path in download_list)
save_file('download.txt', data.encode('utf8'))
if __name__ == '__main__':
print '*********************************'
print '* A downloader for 52pojie aipan '
print '* Site: https://down.52pojie.cn '
print '* Author: anhkgg'
print '* Site: github.com/anhkgg, anhkgg.com'
print '* CopyRight (c) 2019 '
print '*********************************'
print ' \n Start downloading ... '
filter = [
'Tools/',
]
print ' You want to download these dir: '
for f in filter:
print ' ->', f
start_time = time.time()
path_list = get_file_list()
pdir = os.getcwd()
filter_download(pdir, filter, path_list)
print ' Finish the job. %.2fs' % (time.time() - start_time)