-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate.py
More file actions
37 lines (27 loc) · 1007 Bytes
/
Copy pathupdate.py
File metadata and controls
37 lines (27 loc) · 1007 Bytes
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
from os import rename
from os import listdir
from os.path import join
from json import dumps
from hashlib import sha256
PATH = join(".", "cats")
def main():
path_list = [x for x in listdir(PATH) if x.endswith(".webp")]
print("total imgs:", len(path_list))
for path in path_list:
old_img_path = join(PATH, path)
with open(old_img_path, mode="rb") as img_reader:
image = img_reader.read()
new_name = sha256(image).hexdigest() + ".webp"
if path != new_name:
print("renamed:", path)
rename(old_img_path, join(PATH, new_name))
json = "// auto generated with update.py\nexport default " + \
dumps(
obj=[f"/cats/{x}" for x in [x for x in listdir(PATH) if x.endswith(".webp")]],
indent=4,
sort_keys=True
) + ";"
with open(join(".", "tab", "cats.js"), mode="w", encoding="utf-8") as cats_writer:
cats_writer.write(json)
if __name__ == "__main__":
main()