Browse code

Adding a finder for unused images

Veselin Kolev authored on 02/04/2023 01:02:15
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,47 @@
1
+#!/usr/bin/env python3
2
+
3
+from bs4 import BeautifulSoup
4
+import os
5
+
6
+file_list = []
7
+
8
+for root, dirs, files in os.walk("."):
9
+    for file in files:
10
+        if file.endswith(".html"):
11
+             file_list += [os.path.join(root, file)]
12
+
13
+image_list = []
14
+image_folder_list = []
15
+
16
+for i in file_list:
17
+    dir_path = os.path.dirname(os.path.realpath(i))
18
+    file_in = open(i, 'r')
19
+    soup = BeautifulSoup(file_in, 'html.parser')
20
+    all_imgs = soup.find_all('img')
21
+
22
+    for image in all_imgs:
23
+        source = image['src']
24
+
25
+        if source[0] != "/" and os.path.isfile(dir_path + '/' + source):
26
+            img_path = os.path.realpath(dir_path + '/' + source)
27
+            img_dir_path = os.path.dirname(img_path)
28
+
29
+            if img_path not in image_list:
30
+                image_list += [img_path]
31
+
32
+            if img_dir_path not in image_folder_list:
33
+                image_folder_list += [img_dir_path]
34
+
35
+all_images = []
36
+
37
+for i in image_folder_list:
38
+
39
+    for root, dirs, files in os.walk(i):
40
+        for file in files:
41
+            if file.endswith(".png"):
42
+                all_images += [os.path.join(root, file)]
43
+
44
+for i in image_list:
45
+    if i not in all_images:
46
+        print(i)
47
+