nerve/features.py

135 lines
3.7 KiB
Python
Raw Normal View History

2022-05-18 03:59:32 -04:00
import json
import requests
2022-08-09 15:39:51 -04:00
import easyappointments
2022-05-18 03:59:32 -04:00
"""
Features
This is the main API for executing functions with data
"""
class API:
def __init__(self):
with open('data.json') as f:
self.data = json.loads(f.read())
2022-08-09 15:39:51 -04:00
self.appointments = easyappointments.easyappointments(self.data['easyappointments']['token'], self.data['easyappointments']['url'])
2022-05-18 03:59:32 -04:00
# Only refresh data when a change is made
def update_data(self):
with open('data.json','r') as f:
self.data = json.loads(f.read())
2022-05-18 03:59:32 -04:00
def save(self):
with open('data.json','w') as f:
f.write(json.dumps(self.data,indent=2))
return True
return False
#Administration
2022-05-18 03:59:32 -04:00
def is_admin(self,handle):
self.update_data()
2022-05-18 03:59:32 -04:00
#return bool
pass
def add_admin(self,handle):
self.update_data()
2022-05-18 03:59:32 -04:00
#return bool
if self.is_admin(handle):
return True
else:
self.data["admins"].append(handle)
self.save()
return True
def remove_admin(self,handle):
self.update_data()
2022-05-18 03:59:32 -04:00
#return bool
if self.is_admin(handle):
i = self.data["admins"].index(handle)
self.data["admins"].pop(i)
self.save()
return True
else:
return True
#state management
def get_user_state(self,handle):
self.update_data()
if handle not in self.data["users"]:
self.data["users"][handle]={"state":[]}
self.save()
return self.data["users"][handle]["state"]
def update_user_state(self,handle,state):
self.update_data()
current_state = self.get_user_state(handle)
self.data["users"][handle]["state"] = state
self.save()
return True
2022-05-18 03:59:32 -04:00
#FAQ
def get_faq_string(self):
self.update_data()
faq = self.data["faq"]
return faq["header"] + "".join(
["\n# "+q["question"]+"\n"+q["answer"]+"\n" for q in faq["questions"]]
) + "\n"+ faq["footer"]
def get_faq_questions(self):
self.update_data()
questions = self.data['faq']['questions']
response = ""
index = 0
for question in questions:
response += f"[{index}]:{question['question']}\n"
index += 1
return response
2022-05-18 03:59:32 -04:00
def get_header(self):
self.update_data()
2022-05-18 03:59:32 -04:00
return self.data["faq"]["header"]
def set_header(self,hdr):
self.update_data()
2022-05-18 03:59:32 -04:00
#return bool
self.data["faq"]["header"] = hdr
self.save()
return True
def get_questions(self):
self.update_data()
2022-05-18 03:59:32 -04:00
#return questions
return self.data["faq"]["questions"]
def add_question(self,qtn):
self.update_data()
2022-05-18 03:59:32 -04:00
#return bool
self.data["faq"]["questions"].append(qtn)
self.save()
return True
def remove_question(self,qtn_i):
self.update_data()
2022-05-18 03:59:32 -04:00
#return bool
self.data["faq"]["questions"].pop(qtn_i)
self.save()
return True
def update_question(self,qtn_i,qtn):
self.update_data()
2022-05-18 03:59:32 -04:00
#return bool
self.data["faq"]["questions"][qtn_i]=qtn
return True
#Meetings
2022-08-09 15:39:51 -04:00
def get_meetings(self,mxid):
self.update_data()
2022-08-09 15:39:51 -04:00
self.appointments.list_upcoming_appointments(mxid)
2022-05-18 03:59:32 -04:00
pass
def request_meeting(self):
self.update_data()
2022-05-18 03:59:32 -04:00
pass
def accept_meeting(self):
self.update_data()
2022-05-18 03:59:32 -04:00
pass
if __name__ == '__main__':
storeAPI = API()
print("Storage loaded.\n",storeAPI.data['username'])