DawnC commited on
Commit
15b989f
1 Parent(s): 450e465

Delete history_manager.py

Browse files
Files changed (1) hide show
  1. history_manager.py +0 -92
history_manager.py DELETED
@@ -1,92 +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
- print("\nSaving history:")
37
- print("Results to save:", results)
38
-
39
- # 使用 pytz 創建台北時區
40
- taipei_tz = pytz.timezone('Asia/Taipei')
41
- # 獲取當前時間並轉換為台北時間
42
- taipei_time = datetime.now(taipei_tz)
43
-
44
- history_entry = {
45
- "timestamp": taipei_time.strftime("%Y-%m-%d %H:%M:%S"),
46
- "preferences": user_preferences,
47
- "results": results
48
- }
49
-
50
- with open(self.history_file, 'r', encoding='utf-8') as f:
51
- history = json.load(f)
52
-
53
- # 添加新紀錄
54
- history.append(history_entry)
55
-
56
- # 限制保存最近的20筆記錄
57
- if len(history) > 20:
58
- history = history[-20:]
59
-
60
- with open(self.history_file, 'w', encoding='utf-8') as f:
61
- json.dump(history, f, ensure_ascii=False, indent=2)
62
-
63
- return True
64
- except Exception as e:
65
- print(f"Error saving history: {str(e)}")
66
- return False
67
-
68
- def get_history(self) -> list:
69
- """獲取搜尋歷史"""
70
- try:
71
- print("Attempting to read history") # Debug
72
- with open(self.history_file, 'r', encoding='utf-8') as f:
73
- data = json.load(f)
74
- print(f"Read {len(data)} history entries") # Debug
75
- return data if isinstance(data, list) else []
76
- except Exception as e:
77
- print(f"Error reading history: {str(e)}")
78
- print(traceback.format_exc())
79
- return []
80
-
81
- def clear_all_history(self) -> bool:
82
- """清除所有歷史紀錄"""
83
- try:
84
- print("Attempting to clear all history") # Debug
85
- with open(self.history_file, 'w', encoding='utf-8') as f:
86
- json.dump([], f)
87
- print("History cleared successfully") # Debug
88
- return True
89
- except Exception as e:
90
- print(f"Error clearing history: {str(e)}")
91
- print(traceback.format_exc())
92
- return False