-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
52 lines (44 loc) · 1.48 KB
/
data.py
File metadata and controls
52 lines (44 loc) · 1.48 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
from os import path
from time import time
from random import randrange
from json import loads, dumps, decoder
root_dir = path.dirname(__file__)
storage_path = path.join(root_dir, "data.json")
def get_data(data_id) -> str | None:
try:
with open(storage_path, "r") as f:
c = loads(f.read())
return c[data_id]["data"]
except (FileNotFoundError, decoder.JSONDecodeError, KeyError):
return None
def register_data(data: str) -> str:
try:
with open(storage_path, "r") as f:
storage = loads(f.read())
except (FileNotFoundError, decoder.JSONDecodeError):
open(storage_path, "w").close()
storage = {}
with open(storage_path, "w") as f:
data_id = "".join([chr(randrange(97, 123)) for i in range(50)])
registry = {
"data" : data,
"time" : time()
}
storage[data_id] = registry
f.write(dumps(storage, indent=2))
return data_id
def purge_old_data(elapsed_time: int = 3600):
old_data_ids = []
try:
with open(storage_path, "r") as f:
c = loads(f.read())
for i in c.keys():
a = c[i]
if time() - a["time"] > elapsed_time:
old_data_ids.append(i)
with open(storage_path, "w") as f:
for id in old_data_ids:
del c[id]
f.write(dumps(c, indent=2))
except (FileNotFoundError, decoder.JSONDecodeError, KeyError):
return None