2023-08-22 23:51:39 +00:00
|
|
|
import json
|
|
|
|
import requests
|
|
|
|
# from datetime import datetime
|
|
|
|
import time
|
|
|
|
import random
|
2023-08-24 18:34:08 +00:00
|
|
|
import copy
|
2023-08-22 23:51:39 +00:00
|
|
|
|
|
|
|
class TodoDatabase:
|
|
|
|
def __init__(self, filename="data.json"):
|
|
|
|
self.filename = filename
|
|
|
|
self.database = {}
|
|
|
|
try:
|
|
|
|
with open(self.filename) as f:
|
|
|
|
self.database = json.load(f)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def save_todo(self, date, todo, save=True):
|
2023-08-24 18:34:08 +00:00
|
|
|
if "recurring" not in self.database:
|
|
|
|
self.database["recurring"] = []
|
2023-08-22 23:51:39 +00:00
|
|
|
if date not in self.database:
|
|
|
|
# if not len(self.database[date]) > 0:
|
2023-08-24 18:34:08 +00:00
|
|
|
self.database[date] = copy.deepcopy(self.database["recurring"])
|
2023-08-22 23:51:39 +00:00
|
|
|
if todo.recurring:
|
|
|
|
self.database["recurring"].append(todo.model_dump())
|
|
|
|
self.database[date].append(todo.model_dump())
|
|
|
|
|
|
|
|
if save:
|
|
|
|
self._save_database()
|
|
|
|
|
|
|
|
def save_todos(self, date, todos):
|
|
|
|
for todo in todos:
|
|
|
|
self.save_todo(date, todo, save=False)
|
|
|
|
self._save_database()
|
|
|
|
|
|
|
|
def remove_todos(self, date):
|
|
|
|
if date in self.database:
|
|
|
|
del self.database[date]
|
|
|
|
|
|
|
|
def read_todos(self, date):
|
|
|
|
# if self.datebase[date] is None:
|
|
|
|
# return []
|
|
|
|
if date not in self.database:
|
|
|
|
return []
|
|
|
|
return self.database[date]
|
|
|
|
|
|
|
|
def _update_quotes(self):
|
|
|
|
r = requests.get("https://zenquotes.io/api/quotes")
|
|
|
|
res = r.json()
|
|
|
|
|
|
|
|
# if "quotes" not in self.database:
|
|
|
|
self.database["quotes"] = res
|
|
|
|
self.database["quotes_last_updated"] = time.time()
|
|
|
|
self._save_database()
|
|
|
|
|
|
|
|
def get_random_quote(self):
|
|
|
|
# print(time.time())
|
2023-08-23 02:47:20 +00:00
|
|
|
if ("quotes_last_updated" not in self.database) or ("quotes" not in self.database) or (time.time()-self.database["quotes_last_updated"] > 86400):
|
2023-08-22 23:51:39 +00:00
|
|
|
self._update_quotes()
|
|
|
|
quote = random.choice(self.database["quotes"])
|
|
|
|
|
|
|
|
return [quote['q'], "- " + quote['a']]
|
|
|
|
|
|
|
|
|
|
|
|
def _save_database(self):
|
|
|
|
with open(self.filename, 'w') as f:
|
|
|
|
json.dump(self.database, f)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
data = TodoDatabase()
|
|
|
|
# data._update_quotes()
|
|
|
|
print(data.get_random_quote())
|
|
|
|
|
|
|
|
|
|
|
|
|