-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadTools.py
74 lines (68 loc) · 2.16 KB
/
DownloadTools.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
import requests,re,os,time,shutil
url="https://unpkg.com/"
mod="vditor"
version=""
headers={ 'Accept-Language': 'zh-CN,zh;q=0.8',
'Content-Type': 'text/html;Charset=utf-8',
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
}
#获取HTML
def getHTML(url,encoding='utf-8'):
rd = requests.get(url, params=None, headers=headers)
rd.encoding = encoding
return rd.text
#获取版本
def getVsions(m):
h=getHTML(url+m+'/')
j=re.findall(r'<select name="version"(.*?)</select>', h,re.S)[0]
patt = re.compile(r'<option.+?>(.+?)</option>')
option = patt.findall(j)
return option
#扫描目录
def getPaths(v,p='/',files=[],folders=[]):
h=getHTML(url+v+p)
t=re.findall(r'<table(.*?)</table>', h,re.S)[0]
href = re.findall('href="(.*?)"',t)
for name in href:
path=p+name
#if name in ['../','LICENSE'] or not 'iconfont' in path:#material-design-icons
if name in ['../','LICENSE'] or path in ['/packages/','/types/','/dist/docs/','/docs/','/samples/',"/test/","/locale/"] :#跳过
continue
print(path)
if name[-1]=='/':
folders.append(path)
getPaths(v,path,files,folders)
else:
files.append(path)
return {"files":files,"folders":folders}
#创建目录
def makeDirs(dirs,p):
if p==None:
p='./'
for i in dirs:
path=p+i
if not os.path.exists(path):
print("创建目录",path)
os.makedirs(path)
#下载文件
def download(url,path=None):#dir=保存文件夹路径
if not os.path.exists(path):
print("下载:",url)
r=requests.get(url)
t=str(time.time())+'.'+str(pid)+'.tmp'
open(t, 'wb').write(r.content)
shutil.move(t, path)
else:
print("文件已存在")
pid=os.getpid()
print(url+mod+'/')
versions=getVsions(mod)
print("所有版本:",versions)
version=mod+'@'+versions[-1]
print("默认版本:",version)
paths=getPaths(version)
makeDirs(paths["folders"],version)
for i in paths["files"]:
u=url+version+i
download(u,version+'/'+i)
print("完成")