This repository was archived by the owner on May 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
128 lines (97 loc) · 4.23 KB
/
Copy pathdatabase.py
File metadata and controls
128 lines (97 loc) · 4.23 KB
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
import json
from datetime import datetime, timedelta
from types import SimpleNamespace
import pymongo
allDataBases = pymongo.MongoClient("mongodb://localhost:27017/")
readmes = allDataBases["AnimatedReadme"]
ReadmeRefreshInterval_Minutes = 240 # 4 Hours
def NameSpaceToDict(namespace):
return json.dumps(vars(namespace), ensure_ascii=False, indent=4)
def DictToNameSpace(dct):
return json.loads(dct, object_hook=lambda d: SimpleNamespace(**d))
class DBSHelper:
@staticmethod
def CreateCollection(collectionName):
if collectionName not in readmes.list_collection_names():
readmes.create_collection(collectionName)
@staticmethod
def RemoveAny_id(jsonObject):
if jsonObject is not None: jsonObject.pop("_id", None)
return jsonObject
DBHelper = DBSHelper()
DBHelper.CreateCollection("Users")
Users = readmes["Users"]
class ReadmeDatabase:
@staticmethod
def IsUserExists(userName):
return Users.find_one({"username": userName}) is not None
@staticmethod
def GetAllUsers():
return list(Users.find())
@staticmethod
def CreateNewUser(userName):
user = Users.find_one({"username": userName})
if user is None:
Users.insert_one({"username": userName})
return True
return False
@staticmethod
def SetReadmeState(userName, state):
if not ReadmeDatabase.IsUserExists(userName): return None
Users.update_one({"username": userName}, {"$set": {"state": state}})
return True
@staticmethod
def GetReadmeState(userName):
if not ReadmeDatabase.IsUserExists(userName): return None
return Users.find_one({"username": userName})
@staticmethod
def GetCurrentReadme(userName):
if not ReadmeDatabase.IsUserExists(userName): return None
user = Users.find_one({"username": userName})
return user.get("ReadmePath")
@staticmethod
def SetCurrentReadme(userName, ReadmePath):
if not ReadmeDatabase.IsUserExists(userName): return None
Users.update_one({"username": userName}, {"$set": {"ReadmePath": ReadmePath}})
return True
@staticmethod
def SetReadmeTime(userName, time):
if not ReadmeDatabase.IsUserExists(userName): return None
Users.update_one({"username": userName}, {"$set": {"ReadmeTime": time}})
return True
@staticmethod
def HaveActualReadme(userName):
if not ReadmeDatabase.IsUserExists(userName): return False
return ReadmeDatabase.IsFreshReadme(userName)
@staticmethod
def IsFreshReadme(userName):
if not ReadmeDatabase.IsUserExists(userName): return False
UserObject = Users.find_one({"username": userName})
dt = datetime.strptime(UserObject.get("ReadmeTime"), "%Y-%m-%d %H:%M")
return datetime.now() - dt < timedelta(hours=ReadmeRefreshInterval_Minutes/60)
@staticmethod
def SetCooked(userName, state):
if not ReadmeDatabase.IsUserExists(userName): return None
Users.update_one({"username": userName}, {"$set": {"cooked": state}})
return True
@staticmethod
def IsCooked(userName):
if not ReadmeDatabase.IsUserExists(userName): return False
return Users.find_one({"username": userName}).get("cooked")
@staticmethod
def IsAnyOneCooking():
return len(list(Users.find({"cooked": False}))) > 0 # Someone is not "cooked" yet, so he`s cooking right now
@staticmethod
def UpdateReadmeLineOptions(userName, options):
if not ReadmeDatabase.IsUserExists(userName): return None
Users.update_one({"username": userName}, {"$set": {"options": NameSpaceToDict(options)}})
return True
@staticmethod
def GetReadmeLineOptions(userName):
if not ReadmeDatabase.IsUserExists(userName): return None
options = Users.find_one({"username": userName}).get("options")
return None if options is None else DictToNameSpace(options)
for __tmp__user__ in ReadmeDatabase.GetAllUsers():
ReadmeDatabase.SetCooked(__tmp__user__.get("username"), True)
ReadmeDatabase.SetReadmeState(__tmp__user__.get("username"), " [ SINCE SERVER RELOAD WAS NOT ANY EVENT ABOUT IMAGE REFRESH ]")
print("0mnr0 Options: ", ReadmeDatabase.GetReadmeLineOptions("0mnr0"))