Spaces:
Running
on
Zero
Running
on
Zero
Delete history_manager.py
Browse files- history_manager.py +0 -88
history_manager.py
DELETED
@@ -1,88 +0,0 @@
|
|
1 |
-
|
2 |
-
from datetime import datetime
|
3 |
-
import json
|
4 |
-
import os
|
5 |
-
import pytz
|
6 |
-
import traceback
|
7 |
-
|
8 |
-
class UserHistoryManager:
|
9 |
-
def __init__(self):
|
10 |
-
"""初始化歷史紀錄管理器"""
|
11 |
-
self.history_file = "user_history.json"
|
12 |
-
print(f"Initializing UserHistoryManager with file: {os.path.abspath(self.history_file)}")
|
13 |
-
self._init_file()
|
14 |
-
|
15 |
-
def _init_file(self):
|
16 |
-
"""初始化JSON檔案"""
|
17 |
-
try:
|
18 |
-
if not os.path.exists(self.history_file):
|
19 |
-
print(f"Creating new history file: {self.history_file}")
|
20 |
-
with open(self.history_file, 'w', encoding='utf-8') as f:
|
21 |
-
json.dump([], f)
|
22 |
-
else:
|
23 |
-
print(f"History file exists: {self.history_file}")
|
24 |
-
# 驗證檔案內容
|
25 |
-
with open(self.history_file, 'r', encoding='utf-8') as f:
|
26 |
-
data = json.load(f)
|
27 |
-
print(f"Current history entries: {len(data)}")
|
28 |
-
except Exception as e:
|
29 |
-
print(f"Error in _init_file: {str(e)}")
|
30 |
-
print(traceback.format_exc())
|
31 |
-
|
32 |
-
|
33 |
-
def save_history(self, user_preferences: dict, results: list) -> bool:
|
34 |
-
"""儲存搜尋歷史"""
|
35 |
-
try:
|
36 |
-
# 調試輸出
|
37 |
-
print("\nSaving history:")
|
38 |
-
print("Results to save:", results)
|
39 |
-
|
40 |
-
history_entry = {
|
41 |
-
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
42 |
-
"preferences": user_preferences,
|
43 |
-
"results": results # 確保這裡包含完整的結果資訊
|
44 |
-
}
|
45 |
-
|
46 |
-
with open(self.history_file, 'r', encoding='utf-8') as f:
|
47 |
-
history = json.load(f)
|
48 |
-
|
49 |
-
# 添加新紀錄
|
50 |
-
history.append(history_entry)
|
51 |
-
|
52 |
-
# 限制保存最近的20筆記錄
|
53 |
-
if len(history) > 20:
|
54 |
-
history = history[-20:]
|
55 |
-
|
56 |
-
with open(self.history_file, 'w', encoding='utf-8') as f:
|
57 |
-
json.dump(history, f, ensure_ascii=False, indent=2)
|
58 |
-
|
59 |
-
return True
|
60 |
-
except Exception as e:
|
61 |
-
print(f"Error saving history: {str(e)}")
|
62 |
-
return False
|
63 |
-
|
64 |
-
def get_history(self) -> list:
|
65 |
-
"""獲取搜尋歷史"""
|
66 |
-
try:
|
67 |
-
print("Attempting to read history") # Debug
|
68 |
-
with open(self.history_file, 'r', encoding='utf-8') as f:
|
69 |
-
data = json.load(f)
|
70 |
-
print(f"Read {len(data)} history entries") # Debug
|
71 |
-
return data if isinstance(data, list) else []
|
72 |
-
except Exception as e:
|
73 |
-
print(f"Error reading history: {str(e)}")
|
74 |
-
print(traceback.format_exc())
|
75 |
-
return []
|
76 |
-
|
77 |
-
def clear_all_history(self) -> bool:
|
78 |
-
"""清除所有歷史紀錄"""
|
79 |
-
try:
|
80 |
-
print("Attempting to clear all history") # Debug
|
81 |
-
with open(self.history_file, 'w', encoding='utf-8') as f:
|
82 |
-
json.dump([], f)
|
83 |
-
print("History cleared successfully") # Debug
|
84 |
-
return True
|
85 |
-
except Exception as e:
|
86 |
-
print(f"Error clearing history: {str(e)}")
|
87 |
-
print(traceback.format_exc())
|
88 |
-
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|