From 9133de06842e1915772b2411d9b7789e3ef21240 Mon Sep 17 00:00:00 2001 From: CitingNutria <37170862+CitingNutria@users.noreply.github.com> Date: Fri, 18 Mar 2022 01:29:49 +0800 Subject: [PATCH] optimised encoding issues and added refresh function --- kedama_bill_reader.py | 70 ++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/kedama_bill_reader.py b/kedama_bill_reader.py index d09a143..f4c9f56 100644 --- a/kedama_bill_reader.py +++ b/kedama_bill_reader.py @@ -11,6 +11,9 @@ def create_folder(folder_name): os.makedirs(user_documents + '/' + folder_name) return user_documents + '/' + folder_name + + + cache_path = create_folder('Kedama Logs Reader') if not os.path.exists(cache_path+ '\\logs.txt'): @@ -24,47 +27,46 @@ def create_folder(folder_name): logs_path = f.read() +while True: + # create a logs list + logs_list = [] + gz_list = [] + # create a list of files with .gz in the logs folder + for file in os.listdir(logs_path): + if file.endswith(".gz"): + try: + with gzip.open(logs_path + "\\" + file, 'rt', encoding='utf-8') as f: + file_content = f.read() + for line in file_content.splitlines(): + if '您收到了' in line: + info = file[0:-9] + ' ' + line[0:11] + line[39:] + logs_list.append(info) + except: + with gzip.open(logs_path + "\\" + file, 'rt', encoding='gbk') as f: + file_content = f.read() + for line in file_content.splitlines(): + if '您收到了' in line: + info = file[0:-9] + ' ' + line[0:11] + line[39:] + logs_list.append(info) + latest_log = logs_path + "\\latest.log" + # get now date + now = datetime.datetime.now() + # read the latest log file + with open(latest_log, 'r') as f: + for line in f: + if "您收到了" in line: + logs_list.append(str(now)[0:10] + ' ' + line[0:11] + line[39:]) + # print the logs list line by line + for line in logs_list: + print(line) -# create a logs list -logs_list = [] -gz_list = [] -# create a list of files with .gz in the logs folder -for file in os.listdir(logs_path): - if file.endswith(".gz"): - with gzip.open(logs_path+"\\"+file, 'rt') as f: - file_content = f.read() - for line in file_content.splitlines(): - if '您收到了' in line: - info = file[0:-9]+' '+line[0:11] + line[39:] - logs_list.append(info) - - - - - - -latest_log = logs_path + "\\latest.log" - -# get now date -now = datetime.datetime.now() - - -# read the latest log file -with open(latest_log, 'r') as f: - for line in f: - if "您收到了" in line: - logs_list.append(str(now)[0:10]+' '+line[0:11] + line[39:]) - + exit = input('按回车刷新') -# print the logs list line by line -for line in logs_list: - print(line) -exit = input('按任意键退出')