-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataBase.py
133 lines (110 loc) · 5.07 KB
/
dataBase.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
import json
import os
import time
from datetime import datetime
class chats:
def __init__(self)->None:
self.path = 'chats'
self.chats_path = [os.path.join(self.path, i) for i in os.listdir(self.path) if i!='chatHistory.json']
self.newChatID = None
@property
def chatHistory(self)->dict:
with open(os.path.join('chats', 'chatHistory.json'), 'rb') as f: data = json.load(f)
return data
def getNewID(self):
return len(os.listdir(self.path))-1
def newChat(self, Prompt:str, Response:str)->None:
dateFormat = time.strftime(r"%Y-%m-%d %H:%M:%S")
# name = "New chat"
name = ' '.join(Prompt.split(' ')[:3])
chatsData = [
[Prompt, Response]
]
data = {
"name": name,
"dateFormat": dateFormat,
"chats": chatsData#{1:chatsData}
}
ID = self.getNewID()
with open(os.path.join(self.path, f"{ID}.json"), 'w') as f:
json.dump(data, f, indent=4)
chatHistory = self.chatHistory
chatHistory.update({
f'{ID}.json': {
"name": name,
"dateFormat": dateFormat
}
})
with open(os.path.join(self.path, "chatHistory.json"), 'w') as f:
json.dump(chatHistory, f, indent=4)
self.newChatID = ID
return ID
def addChat(self, ID, Prompt:str, Response:str)->None: # ResponseID = [1, 2, 1]
dateFormat = time.strftime(r"%Y-%m-%d %H:%M:%S")
with open(os.path.join(self.path, f"{ID}.json"), 'rb') as f:
data = json.load(f)
data['dateFormat'] = dateFormat
data['chats'].append([Prompt, Response])
with open(os.path.join(self.path, f"{ID}.json"), 'w') as f:
json.dump(data, f, indent=4)
chatHistory = self.chatHistory
chatHistory[f"{ID}.json"]["dateFormat"] = dateFormat
with open(os.path.join(self.path, "chatHistory.json"), 'w') as f:
json.dump(chatHistory, f, indent=4)
def updateChats_path(self)->None:
self.chats_path = [os.path.join(self.path, i) for i in os.listdir(self.path) if i!='chatHistory.json']
def deleteChat(self, ID):
os.remove(os.path.join(self.path, f"{ID}.json"))
chatHistory = self.chatHistory
chatHistory.pop(f"{ID}.json")
with open(os.path.join(self.path, "chatHistory.json"), 'w') as f:
json.dump(chatHistory, f, indent=4)
self.updateChats_path()
def deleteAll(self):
for i in self.chats_path: os.remove(i)
with open(os.path.join(self.path, "chatHistory.json"), 'w') as f:
json.dump({}, f, indent=4)
self.updateChats_path()
def getChatData(self, ID):
with open(os.path.join(self.path, f"{ID}.json"), 'rb') as f:
data = json.load(f)
# name = data["name"]
# dateFormat = data["dateFormat"]
return data['chats']
def getChatHistory(self, ID=None):
chatHistory = [(k, v["name"], v["dateFormat"]) for k, v in self.chatHistory.items()]
chatHistory.sort(key=lambda x:datetime.strptime(x[2], r"%Y-%m-%d %H:%M:%S"), reverse=True)
def timeGroup(x:str):
target_datetime_obj = datetime.strptime(x, r"%Y-%m-%d %H:%M:%S")
time_difference = datetime.now() - target_datetime_obj
seconds_difference = time_difference.total_seconds()
month = ["January", "February", "March", "April", "May", "June", "July", 'August', 'September', 'October', 'November', "December"][int(x.split("-")[1])-1]
if(time.strftime(r"%Y")==x.split("-")[0]):
if(time.strftime(r"%m")==x.split("-")[1]):
if(seconds_difference < 86400): group = "Today"
elif(seconds_difference < 86400*2): group = "Yesterday"
elif(seconds_difference < 86400*7): group = "Previous 7 Days"
elif(seconds_difference < 86400*30): group = "Previous 30 Days"
else: group = month
else: group = f"{month}, {x.split('-')[0]}"
return group
result = {}
for i in chatHistory:
i_ID = i[0].removesuffix('.json')
try:
result[timeGroup(i[2])].append((i[1], i_ID, i_ID == ID))
except:
result.update({
timeGroup(i[2]) : [(i[1], i_ID, i_ID == ID)]
})
return result
if __name__ == '__main__':
dataBase = chats()
# print(dataBase.chats_path)
# print(dataBase.chatHistory)
dataBase.newChat('Make a textarea and add it scrollbar 30px to right', 'To create a textarea with a scrollbar positioned 30px to the right, you can use HTML and CSS. Here\'s an example:')
# dataBase.addChat(1, 'Prompt', 'Response')
# dataBase.deleteChat(1)
# print(dataBase.getChatData(1))
print(dataBase.getChatHistory(0))
# dataBase.deleteAll()