-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_font.py
78 lines (62 loc) · 2.24 KB
/
extract_font.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
#!/usr/bin/env python3
import re, sys
OUTPUTDIR = "output" # output dir
base64array = [] # array containing fonts in base64 format
fontname = [] # font name
fontweight = [] # font weight
fontstyle = [] # font stule
fonts = [] # fonts
fontfile = [] # fonts file names
numf = 0 # number of fonts (counter)
# Check for errors
if len(sys.argv) < 2:
print ("This program needs at least one argument")
exit()
filename = str(sys.argv[1])
try:
file = open(filename, "r")
except IOError:
print ("Error opening file: " + filename)
exit()
regexa = re.compile(r"url\(data:application\/font-woff;charset=utf-8;base64,[\w+/=]+\)")
regexn = re.compile(r"font-family\:[\w\s]+\;")
regexw = re.compile(r"font-weight\:[\w\s]+\;")
regexs = re.compile(r"font-style\:[\w\s]+\;")
# Extract the arrays per line
for line in file:
base64array += regexa.findall(line)
fontname += regexn.findall(line)
fontweight += regexw.findall(line)
fontstyle += regexs.findall(line)
# Extract useful data
while numf < len(base64array):
x = base64array[numf]
if x == []:
continue
x = str(x)
filestring = ""
tmpstring = ""
if numf < len(fontname) and fontname[numf] != []:
tmpstring = str(fontname[numf])
filestring += tmpstring[12:len(tmpstring)-1] + "_"
if numf < len(fontweight) and fontweight[numf] != []:
tmpstring = str(fontweight[numf])
filestring += tmpstring[12:len(tmpstring)-1] + "_"
if numf < len(fontstyle) and fontstyle[numf] != []:
tmpstring = str(fontstyle[numf])
filestring += tmpstring[11:len(tmpstring)-1] + "_"
print("{:02d} - Font found: {}".format(numf+1, filestring.replace("_", " ")))
fonts.append(x[52:len(x)-1])
fontfile.append(filestring + str(numf+1).zfill(2) + ".txt")
numf += 1
# Write each base64 found into a seperate file
for i, l in enumerate(fonts):
# Move the individual files to corresponding files
outfile = open("./{}/{}".format(OUTPUTDIR, fontfile[i]), "w")
outfile.write(l)
outfile.close()
file.close()
if numf == 0:
print ("No font found in " + filename)
else:
print (str(numf) + " font(s) found in " + filename)