-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_images.py
134 lines (116 loc) · 4.89 KB
/
update_images.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
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
#!/usr/bin/env python
"""Creates the config file for index.html to use.
################################################
IF YOU ARE ON MAC PRESS 'Run' UP TOP OR PRESS F5
################################################
Make sure to have 2 folders in the same folder as this file. images and important_files
- this folder
|--> picture_fall.html
|--> create_config.py
|--> UPDATE_IMAGES.bat
|--> important_files/
|--> keywords_config.js
|--> logo.png
|--> picturefall_logic.js
|--> images/
|--> in this folder you can have any .png, .jpg, and .webp images. You can also add them to a folder e.g.:
|--> x/
|--> box.png <-- box.png will be used as an 'x' word even though it doesn't start with 'x'
|--> /animals/ <-- this folder is used for the animal mode
|--> /characters/ <-- this folder is used for characters
images in the /images/ folder will be used by their names. If the image is named e.g 'dinosaur.png' it will be added
to the 'D' section. If you want to override this behaviour, for example adding 'fox.png' to 'X' rather than 'F', make
sure to add the image to the specific letter folder.
"""
import os
import sys
from datetime import datetime
__author__ = "Artem Nikitin"
__copyright__ = "Copyright 2022"
__credits__ = ["Artem Niktin", "whatstheword.io"]
__license__ = "MIT"
__version__ = "1.0.0"
__maintainer__ = "None"
__email__ = "[email protected]"
__status__ = "Production"
main_dir = os.path.dirname(os.path.abspath(__file__))
def get_images()->dict:
all_image_filenames = {}
"""
creates the dict of all the images in the /images/ folder
"""
print(f"Searching for images...")
for folder in os.walk(main_dir + '/images/'):
dir = os.path.basename(folder[0])
if len(dir) == 1:
#is one of the letter dirs
all_image_filenames[dir] = [{"name": name.split('.')[0], "path": './images/' + dir + '/' + name} for name in folder[2]]
elif dir == 'animals':
all_image_filenames['animals'] = [{"name": name.split('.')[0], "path": './images/' + dir + '/' + name} for name in folder[2]]
elif dir == 'characters':
all_image_filenames['characters'] = [{"name": name.split('.')[0], "path": './images/' + dir + '/' + name} for name in folder[2]]
for file in os.listdir(main_dir + '/images/'):
if file.split('.')[-1] in ['png','jpg','jpeg','webp']:
all_image_filenames[file[0]].append({"name": file.split('.')[0], "path": './images/' + file})
return all_image_filenames
def get_custom_words()->dict:
"""
creates a list of all the wordlists added in CUSTOM_WORDS.txt
"""
all_custom_words = []
print('Searching for word lists...')
try:
with open(main_dir + '/CUSTOM_WORDS.txt','r') as f:
word_lists = f.readlines()
for line in word_lists:
if not line.strip().startswith("#") and len(line.strip())!=0:
words = line.strip().split(',')
words = [word.strip().lower() for word in words]
all_custom_words.append(words)
except Exception:
print('ERROR: Unable to open CUSTOM_WORDS.txt. Did you accidentally delete it?')
exit()
return all_custom_words
def create_config():
"""
writes the info into a js file.
"""
all_images = get_images()
all_words = get_custom_words()
lines = [f"//This file was autogenerated from the file {sys.argv[0]}.","//Please do not manually edit it to avoid errors.", f"//Last generated on: {str(datetime.today().strftime('%Y-%m-%d-%H:%M:%S'))}"]
total_words = 0
animals_num = 0
characters_num = 0
lines.append('\n\nconst KEYWORDS_CONFIG = {\n')
for key,value in all_images.items():
lines.append(f'\t{key}: {"{"}')
for item in value:
lines.append(f'\t\t"{item["name"]}":"{item["path"]}",')
lines.append('\t},')
if key == 'animals':
animals_num += len(value)
elif key == 'characters':
characters_num += len(value)
else:
total_words += len(value)
lines.append(f'\n\ttotal_words: {total_words},')
lines.append('\n};')
print(f"Found {total_words} keywords.")
print(f"Found {animals_num} animals.")
print(f"Found {characters_num} characters.")
lines.append('\n\nconst WORD_ARRAYS = [\n')
for words in all_words:
lines.append(f'\t{words},')
lines.append('\n];\n')
print(f'Found {len(all_words)} custom wordlists.')
print('Creating file...')
try:
with open(os.path.join(main_dir + '/important_files/','keywords_config.js'),"w") as f:
for line in lines:
f.write(line + '\n')
except Exception:
print('ERROR: Unable to make a file. Are you sure there is a folder named "important_files"?')
exit()
print(f"Finished.")
if __name__ == '__main__':
create_config()