forked from mathewthe2/Game2Text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
272 lines (244 loc) · 9.89 KB
/
logger.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import time
import os
import re
import eel
import glob
import base64
import codecs
import threading
import platform
from pathlib import Path
from datetime import datetime
from config import r_config, LOG_CONFIG
from util import create_directory_if_not_exists, base64_to_image_path
from audio import play_audio_from_file
from gamescript import add_matching_script_to_logs
from tools import bundle_dir
TEXT_LOG_PATH = Path(bundle_dir, 'logs', 'text')
IMAGE_LOG_PATH = Path(bundle_dir, 'logs', 'images')
AUDIO_LOG_PATH = Path(bundle_dir, 'logs', 'audio')
game_script_matcher = None
def get_time_string():
return time.strftime('%Y%m%d-%H%M%S')
def parse_time_string(time_string):
return datetime.strptime(time_string, '%Y%m%d-%H%M%S')
def get_hours_string(datetime_object):
return datetime.strftime(datetime_object, '%I:%M%p')
def log_text(start_time,request_time, text):
parsed_text = text.replace('\n', '')
if (len(parsed_text) < 1):
return
filename = '{}/{}.txt'.format(TEXT_LOG_PATH, start_time)
create_directory_if_not_exists(filename)
with open(filename, 'a', encoding='utf-8', newline='') as f:
if(os.path.getsize(filename) > 0):
f.write('{}{}, {}'.format('\n', request_time, parsed_text))
else:
f.write('{}, {}'.format(request_time, parsed_text))
f.close()
def log_media(session_start_time, request_time, audio_recorder):
is_log_images = r_config(LOG_CONFIG, 'logimages').lower() == 'true'
is_log_audio = r_config(LOG_CONFIG, 'logaudio').lower() == 'true'
audio_duration = float(r_config(LOG_CONFIG, 'logaudioduration'))
if is_log_audio:
file_name = request_time + '.' + r_config(LOG_CONFIG, 'logaudiotype')
audio_file_path = str(Path(AUDIO_LOG_PATH, session_start_time, file_name))
create_directory_if_not_exists(audio_file_path)
audio_recorder.stop_recording(audio_file_path, audio_duration)
eel.restartAudioRecording()()
if is_log_images:
image_extension = r_config(LOG_CONFIG, 'logimagetype')
file_name = request_time + '.' + image_extension
full_image_path = str(Path(IMAGE_LOG_PATH, session_start_time, file_name))
thread = threading.Thread(target = log_video_image, args=[full_image_path])
thread.start()
else:
insert_newest_log_without_image()
def log_video_image(image_path):
create_directory_if_not_exists(image_path)
base64_image = eel.getVideoImage()()
# Manually add image data to log data because image is yet to be saved to file
insert_newest_log_with_image(base64_image, os.path.splitext(image_path)[1])
# Save image
base64_to_image_path(base64_image, image_path)
def get_image_type(log_id, folder_name):
path = Path(IMAGE_LOG_PATH, folder_name)
if not path.is_dir():
return None
file_name = next((f for f in os.listdir(path) if re.match('{}.(?:jpg|jpeg|png|tiff|webp)$'.format(log_id), f)), None)
if not file_name:
return None
return Path(file_name).suffix.split('.')[1]
def get_base64_image_with_log(log_id, folder_name):
imagePath = str(Path(IMAGE_LOG_PATH, folder_name, log_id + '.png'))
path = Path(IMAGE_LOG_PATH, folder_name)
if not path.is_dir():
return None
file_name = next((f for f in os.listdir(path) if re.match('{}.(?:jpg|jpeg|png|tiff|webp)$'.format(log_id), f)), None)
if not file_name:
return None
with open('{}/{}'.format(path, file_name), 'rb') as image_file:
base64_bytes = base64.b64encode(image_file.read())
base64_image_string = base64_bytes.decode('utf-8')
return base64_image_string
@eel.expose
def show_logs():
last_session_max_log_size = int(r_config(LOG_CONFIG, 'lastsessionmaxlogsize'))
saved_logs = get_logs(limit=last_session_max_log_size)
if len(saved_logs) > 0:
# Workaround to fix the problem first image data is not transferred to log window
image_data_list = eel.getCachedScreenshots()()
if image_data_list:
for log in saved_logs:
if log['id'] in image_data_list.keys():
# Remove cache if file is saved
if log['image']:
eel.removeCachedScreenshot(log['id'])()
# Get image from cache
else:
image_data = image_data_list[log['id']]
log['image'] = image_data['base64ImageString']
log['image_type'] = image_data['imageType']
return saved_logs
def text_to_log(text, file_path):
log_id = text[:15]
date = parse_time_string(log_id)
image = get_base64_image_with_log(log_id=log_id, folder_name=Path(file_path).stem)
image_type = get_image_type(log_id=log_id, folder_name=Path(file_path).stem)
log = {
'id': log_id,
'file': Path(file_path).name,
'folder': Path(file_path).stem,
'image': image,
'image_type': image_type,
'audio': get_audio_file_name(log_id, Path(file_path).stem),
'hours': get_hours_string(date),
'text': text[16:]
}
return log
def add_gamescript_to_logs(logs):
gamescript = r_config(LOG_CONFIG, 'gamescriptfile',)
if (gamescript):
if (Path(gamescript).is_file()):
logs = add_matching_script_to_logs(gamescript, logs)
for log in logs:
if ('matches' in log):
eel.updateLogDataById(log['id'], {'matches': log['matches'], 'autoMatch': True, 'isMatched': False})()
return
def get_logs(limit=0):
output = []
if not os.path.exists(TEXT_LOG_PATH):
return []
list_of_files = glob.glob(str(TEXT_LOG_PATH) + '/*.txt')
if len(list_of_files) < 1:
return []
latest_file = max(list_of_files, key=os.path.getctime)
with open(latest_file, 'r', encoding='utf-8') as f:
for line in f:
log = text_to_log(line, latest_file)
output.append(log)
f.close()
if limit > 0 and len(output) > limit:
output = output[-limit:]
# Start another thread to match logs to game script
is_matching = eel.isMatchingScript()()
if is_matching:
thread = threading.Thread(target = add_gamescript_to_logs, args=[output])
thread.start()
return output
def get_latest_log():
log = {}
if not os.path.exists(TEXT_LOG_PATH):
return {}
list_of_files = glob.glob(str(TEXT_LOG_PATH) + '/*.txt')
if len(list_of_files) < 1:
return {}
latest_file = max(list_of_files, key=os.path.getctime)
if not latest_file:
return None
with open(latest_file, 'r', encoding='utf-8') as f:
for line in f:
pass
last_line = line
log = text_to_log(last_line, latest_file)
f.close()
# Start another thread to match log to game script
is_matching = eel.isMatchingScript()()
if is_matching:
thread = threading.Thread(target = add_gamescript_to_logs, args=[[log],])
thread.start()
return log
@eel.expose
def delete_log(log_id, folder_name):
filename = '{}/{}.txt'.format(TEXT_LOG_PATH, folder_name)
if (Path(filename).is_file()):
temp_filename = '{}/temp.txt'.format(TEXT_LOG_PATH)
# lines = []
with open(filename, "r", encoding='utf-8') as file:
lines = file.readlines()
with open(temp_filename, "w", encoding='utf-8') as new_file:
newLines = [line.rstrip('\r\n') for line in lines if line[:15] != log_id]
for line in newLines:
if line != newLines[0]:
new_file.write('\n')
new_file.write(line)
# Remove original file and rename the temporary as the original one
os.remove(filename)
os.rename(temp_filename, filename)
return
return
@eel.expose
def update_log_text(log_id, folder_name, text):
parsed_text = text.replace('\n', '')
if (len(parsed_text) < 1):
return
filename = '{}/{}.txt'.format(TEXT_LOG_PATH, folder_name)
if (Path(filename).is_file()):
temp_filename = '{}/temp.txt'.format(TEXT_LOG_PATH)
with codecs.open(filename, 'r', encoding='utf-8') as fi, \
codecs.open(temp_filename, 'w', encoding='utf-8') as fo:
for line in fi:
line_id = line[:15]
if (line_id == log_id):
fo.write('{}, {}'.format(log_id, parsed_text))
else:
fo.write(line)
# Remove original file and rename the temporary as the original one
os.remove(filename)
os.rename(temp_filename, filename)
return
return
def insert_newest_log_with_image(base64_image_string, image_type):
log = get_latest_log()
log['image'] = base64_image_string
log['image_type'] = image_type
eel.addLogs([log])()
def insert_newest_log_without_image():
eel.addLogs([get_latest_log()])()
@eel.expose
def play_log_audio(file_name, folder_name):
file = Path(AUDIO_LOG_PATH, folder_name, file_name)
if file:
return play_audio_from_file(str(file))
@eel.expose
def delete_audio_file(log_id, folder_name):
file = get_audio_file_name(log_id, folder_name)
if (file):
full_path = Path(AUDIO_LOG_PATH, folder_name, file)
os.remove(full_path)
return True
else:
return False
def get_audio_file_name(log_id, folder_name):
path = Path(AUDIO_LOG_PATH, folder_name)
if not path.is_dir():
return None
file_name = next((f for f in os.listdir(path) if re.match('{}.(?:wav|mp3|mp4|ogg|wma|aac|aiff|flv|m4a|flac)$'.format(log_id), f)), None)
# Temporary fix for Mac OS: change file type since mp3 hasn't finished converting
if platform.system() == 'Darwin':
file_name = log_id + '.' + r_config(LOG_CONFIG, 'logaudiotype')
return file_name
# Middleman for selected main window to launch add card in log window
@eel.expose
def highlight_text_in_logs(text):
eel.showCardWithSelectedText(text)()