-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-beloved-script.py
57 lines (43 loc) · 1.72 KB
/
my-beloved-script.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
import os
from PIL import Image
import requests
from io import BytesIO
def findInParen(s):
return s[s.find("(")+1:s.find(")")]
def findInSquareBrackets(s):
return s[s.find("[")+1:s.find("]")]
def main():
dirs = os.listdir()
dirs.remove("script.py")
for dir in dirs:
print(f"In dir: <{dir}>")
for file in os.listdir(dir):
print(f" - In file: <{file}>")
newContent = ""
with open(os.path.join(dir, file), "r") as f:
raw = f.read().strip()
lines = raw.split("\n")
removeLineNums = []
for i, line in enumerate(lines):
if "<SizedImage" in line and "width={}" in line:
removeLineNums.append(i)
for i in removeLineNums:
lines[i] = ""
alterations = [] # (linenumber, newContent)
for i, line in enumerate(lines):
t = line.strip().strip("\t")
if "![" in t and "(" in t and ")" in t:
alt = findInSquareBrackets(t)
src = findInParen(t)
response = requests.get(src)
img = Image.open(BytesIO(response.content))
w, h = img.size
alterations.append(
(i, f'<SizedImage alt="{alt}" src="{src}" width="{w}" height="{h}" />'))
for i, alteration in alterations:
lines[i] = alteration
newContent = "\n".join(lines) + "\n"
with open(os.path.join(dir, file), "w") as f:
f.write(newContent)
if __name__ == "__main__":
main()