forked from Poshushukaemsya/team-number-two-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiles.py
73 lines (68 loc) · 2.92 KB
/
Files.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
from datetime import datetime, date, timedelta
import Shushushu as sh
import pymongo
class Files:
HistoryFile = "source_pack./History.txt"
AdminHistoryFile = "source_pack/Admin_History.txt"
FilmFile = 'source_pack/Film Library.txt'
@staticmethod
def new_log(self, update, funcname):
sh.LOG_ACTIONS.append({
'user': update.effective_user.first_name,
'user id': update.message.chat.id,
'function': funcname,
'message': update.message.text,
'time': datetime.now().strftime("%Y-%m-%d %H.%M")})
sh.collection.insert_one({
'user': update.effective_user.first_name,
'user id': update.message.chat.id,
'function': funcname,
'message': update.message.text,
'time': datetime.now().strftime("%Y-%m-%d %H.%M")})
if str(sh.LOG_ACTIONS[-1]['function']).find('admin') == -1:
with open(Files.HistoryFile, "a", encoding="UTF-8") as file_h:
for key, value in sh.LOG_ACTIONS[-1].items():
file_h.write(key + ':' + (str(value)) + "\t")
file_h.write("\n")
elif str(sh.LOG_ACTIONS[-1]['function']).find('admin') != -1:
with open(Files.AdminHistoryFile, "a", encoding="UTF-8") as handler:
for key, value in sh.LOG_ACTIONS[-1].items():
handler.write(key + ':' + (str(value)) + "\t")
handler.write("\n")
@staticmethod
def history(self):
"""Send user last 5 records from history."""
# hist = ""
# with open(files.HistoryFile, "r", encoding="UTF-8") as file_h:
# hist_all = file_h.readlines()
# if len(hist_all) > 4:
# for i in range(-1, -6, -1):
# hist += hist_all[i]
# else:
# hist = "\t".join(hist_all)
# return hist
hist = []
for idx, user in enumerate(sh.collection.find().sort("_id", pymongo.DESCENDING).limit(5)):
if idx == 5:
break
hist.append("\t".join([key + "\t" + str(user[key]) for key in user if key != "_id"]))
hist_st = "\n".join(hist)
return hist_st
@staticmethod
def delete_logs(self):
k = 0
with open(Files.HistoryFile, "r", encoding="UTF-8") as file_h:
hist_all = file_h.readlines()
for hist_line in hist_all:
hist_line = hist_line.split('\t')
hist_time = hist_line[-2].replace('time:', '')
hist_time = datetime.strptime(hist_time, "%Y-%m-%d %H.%M")
now = datetime.now()
period = now - hist_time
print(period)
if period.days < 7:
break
k += 1
print(k)
with open(Files.HistoryFile, 'w') as file_h:
file_h.writelines(hist_all[k:])