-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.py
71 lines (50 loc) · 1.62 KB
/
tool.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
import re
import base64
from webptools import dwebp
import os
text=""
with open("sample.html","r") as f:
text=f.read()
if not os.path.exists('output'):
os.makedirs('output')
if not os.path.exists('output/assets'):
os.makedirs('output/assets')
if not os.path.exists('output/tmp'):
os.makedirs('output/tmp')
print("File is " + str(len(text)) + " characters")
objects=re.findall(r'(data:[a-z]+\/[a-z\+]+;base64,)([a-zA-Z0-9\!\$\&\'\,\*\+\,\;\=\-\.\_\~\:\@\/\?\%]*)[\s\)\"]+', text)
print("Found " + str(len(objects)) + " encoded objects")
x=0
for f in objects:
x=x+1
print("Writing dat file for file " + str(x))
with open("output/tmp/file" + str(x) + ".dat","w") as newFile:
newFile.write(f[0] + f[1])
if("webp" in f[0]):
type="webp"
elif("png" in f[0]):
type="png"
elif("svg" in f[0]):
type="svg"
if type != "":
finalPath="assets/image" + str(x) + "." + type
path="output/" + finalPath
print("Known file type. Writing " + path)
if type=="webp":
path="output/tmp/" + str(x) + ".webp"
print("Converting webp to png")
with open(path,"wb") as newFile:
encodedString=f[1].encode()
newFile.write(base64.decodebytes(encodedString))
finalPath="assets/image" + str(x) + ".png"
dwebp(path,"output/assets/image" + str(x) + ".png","-o")
print("Converted file, saved to " + "output/assets/image" + str(x) + ".png")
else:
with open(path,"wb") as newFile:
encodedString=f[1].encode()
newFile.write(base64.decodebytes(encodedString))
text=text.replace(f[0]+f[1],finalPath)
print("Writing HTML")
with open("output/output.html","w") as newFile:
newFile.write(text)
print("All done!")