-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiz.py
85 lines (75 loc) · 2.4 KB
/
wiz.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
#!/usr/bin/python
# -*- coding:utf-8 -*-
import re,sqlite3,zipfile,glob,os,sys,shutil
class wiz():
def extract(self,f):
try:
os.mkdir(f[:-4])
zfile = zipfile.ZipFile(f,'r')
zfile.extractall(path = f[:-4])
os.remove(f)
except:
#print f
pass
def extract_all(self,path):
file_list = glob.glob('%s/*'%path)
if '%s/index.html'%path in file_list and '%s/index_files'%path in file_list:
return
#print file_list
for f in file_list:
if f[-4:] == '.ziw':
#print f
self.extract(f)
elif os.path.isdir(f):
self.extract_all(f)
#print '-'*100
def zip(self,f):
pass
re_img = re.compile(r'src="(.*?)"')
ex_num = 0
def collect_img(self,path):
#print (re.escape(r'%s/*'%path))
#print path
try:
flist = os.listdir(path)
#print flist
except:
print ('%s/*'%path)
self.ex_num += 1
print self.ex_num
return
#print flist
if 'index.html' in flist:
content = open('%s/index.html'%path,'r').read().decode('utf-16')
#print content
imgs = self.re_img.findall(content)
imgs = [i.split('/')[-1] for i in imgs]
try:
for f in os.listdir('%s/index_files'%path):
#print f
if f.split('/')[-1] in imgs:
#print f
continue
else:
os.remove('%s/index_files/%s'%(path,f))
except:
pass
self.ziw(path)
shutil.rmtree(path)
else:
for f in flist:
if os.path.isdir('%s/%s'%(path,f)):
#print f,os.path.isdir('%s/%s'%(path,f))
self.collect_img('%s/%s'%(path,f))
def ziw(self,name):
f = zipfile.ZipFile('%s.ziw'%name,'w',zipfile.ZIP_DEFLATED)
for dirpath, dirnames, filenames in os.walk(name):
for filename in filenames:
f.write(os.path.join(dirpath,filename),'%s/%s'%(dirpath.replace(name,''),filename))
f.close()
def del_css(self,f):
pass
if __name__ == '__main__':
w = wiz()
#w.extract_all('.')
w.collect_img('.')