-
Notifications
You must be signed in to change notification settings - Fork 8
/
process-fonts.sh
executable file
·159 lines (133 loc) · 4.54 KB
/
process-fonts.sh
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/zsh
setopt ERR_EXIT
setopt PIPE_FAIL
setopt NO_UNSET
if [[ -z "${DISCORD_ACTIVITY_CLIENT_ID}" ]]; then
print "Error: DISCORD_ACTIVITY_CLIENT_ID environment variable is required" >&2
exit 1
fi
# Process arguments
test_mode=0
no_prompt=0
output_dir="/tmp/discord-fonts"
while [[ $# -gt 0 ]]; do
case $1 in
--test)
test_mode=1
shift
;;
--no-prompt)
no_prompt=1
shift
;;
--output-dir)
output_dir="$2"
shift 2
;;
*)
shift
;;
esac
done
# Clean/create output directory
if [[ -d "$output_dir" ]]; then
rm -rf "$output_dir"
fi
mkdir -p "$output_dir"
print "Starting directory search..." >&2
# Get directories
dirs=(*(/))
dirs=(${dirs:#sheets}) # Exclude sheets
dirs=(${dirs:#.git}) # Exclude .git
dirs=(${dirs:#node_modules}) # Exclude node_modules
if [[ ${#dirs} -eq 0 ]]; then
print "No directories found!" >&2
exit 1
fi
if [[ $test_mode -eq 1 ]]; then
print "Test mode: processing first 5 directories" >&2
dirs=(${dirs[1,5]})
fi
print "\nFound directories:" >&2
print -l " ${dirs[@]}" >&2
# Extract URLs function
extract_font_urls() {
perl -ne '
next if /^\s*\/\*/; # Skip comment lines
while (/url\(['"'"'"]?(https:\/\/fonts\.googleapis\.com[^'"'"'"\s\)]+)['"'"'"]?\)/g) {
print "$1\n";
}
' "$1"
}
# Process each directory
for dir in ${dirs[@]}; do
print "Checking directory: $dir" >&2
mkdir -p "$output_dir/$dir"
# Find fonts.css files recursively
fonts_files=($dir/**/fonts.css(.N))
if [[ ${#fonts_files} -gt 0 ]]; then
print "Found fonts.css files in $dir:" >&2
print -l " ${fonts_files[@]}" >&2
# Collect URLs for this directory
urls_file=$(mktemp)
trap "rm -f '$urls_file'" EXIT
for fonts_file in ${fonts_files[@]}; do
print "Processing: $fonts_file" >&2
found_urls=$(extract_font_urls "$fonts_file")
if [[ -n "$found_urls" ]]; then
print "Found font URLs in $fonts_file:" >&2
print -l " $found_urls" >&2
print "$found_urls" >> "$urls_file"
fi
done
# If we found URLs in this directory
unique_urls=($(sort -u "$urls_file"))
if [[ ${#unique_urls} -gt 0 ]]; then
print "\nFound ${#unique_urls} unique font URLs in $dir" >&2
if [[ $no_prompt -eq 0 ]]; then
print "Proceed with fetching and transforming URLs for $dir? [y/N] " >&2
read -q response || true
print >&2
if [[ "$response" != "y" ]]; then
print "Skipping $dir" >&2
touch "$output_dir/$dir/fonts.css"
continue
fi
fi
raw_file=$(mktemp)
trap "rm -f '$raw_file'" EXIT
# Process URLs for this directory
for font_url in $unique_urls; do
response=$(curl -sS -w "\n%{http_code}" -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" \
"$font_url")
if [[ $? -eq 0 ]]; then
http_code=$(print "$response" | tail -n1)
content=$(print "$response" | sed '$d')
if [[ "$http_code" = "200" ]]; then
print "$content" | \
sed "s/fonts\.gstatic\.com/${DISCORD_ACTIVITY_CLIENT_ID}.discordsays.com\/.proxy\/gstatic\/fonts/g" \
>> "$raw_file"
print >> "$raw_file"
else
print "Error: HTTP $http_code for $font_url" >&2
fi
else
print "Error: Failed to fetch $font_url" >&2
fi
done
if [[ -s "$raw_file" ]]; then
mv "$raw_file" "$output_dir/$dir/fonts.css"
print "Generated fonts.css for $dir" >&2
else
touch "$output_dir/$dir/fonts.css"
print "Generated empty fonts.css for $dir (no valid content)" >&2
fi
else
touch "$output_dir/$dir/fonts.css"
print "Generated empty fonts.css for $dir (no URLs found)" >&2
fi
else
touch "$output_dir/$dir/fonts.css"
print "Generated empty fonts.css for $dir (no fonts.css files found)" >&2
fi
done