200 lines
7.7 KiB
Python
200 lines
7.7 KiB
Python
import requests
|
|
import datetime as dt
|
|
import json
|
|
|
|
def find_event(token, calendar_id,timezone, name):
|
|
url = f"https://www.googleapis.com/calendar/v3/calendars/{calendar_id}/events"
|
|
params = {
|
|
"q": name,
|
|
"timeZone": timezone
|
|
}
|
|
header = {"Authorization": f"Bearer {token}"}
|
|
|
|
response = requests.get(url = url, headers = header, params = params)
|
|
if response.status_code == 401:
|
|
return "verify"
|
|
|
|
events_list = response.json()["items"]
|
|
for i in events_list:
|
|
if i["summary"] == name:
|
|
return i
|
|
return "not found"
|
|
|
|
def create_event(token, calendar_id, event):
|
|
|
|
duplicate_check = find_event(token, calendar_id, event["timezone"], event["name"])
|
|
if duplicate_check == "verify":
|
|
return "verify"
|
|
elif duplicate_check != "not found":
|
|
return "duplicate event"
|
|
|
|
url = f"https://www.googleapis.com/calendar/v3/calendars/{calendar_id}/events"
|
|
payload = {
|
|
"summary": event["name"],
|
|
"start": {
|
|
"dateTime": event["start"].strftime("%Y-%m-%dT%H:%M:%S"),
|
|
"timeZone": event["timezone"]
|
|
},
|
|
"end": {
|
|
"dateTime": event["end"].strftime("%Y-%m-%dT%H:%M:%S"),
|
|
"timeZone": event["timezone"]
|
|
},
|
|
"description": " ".join([(event["start"] + dt.timedelta(days = i)).strftime("%Y-%m-%d") for i in range((event["end"] - event["start"]).days + 1)])
|
|
}
|
|
header = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
|
response = requests.post(url = url, headers = header, json = payload)
|
|
if response.status_code == 401:
|
|
return "verify"
|
|
|
|
return "success"
|
|
|
|
def cancel_event(token, calendar_id, timezone, name):
|
|
|
|
event = find_event(token, calendar_id, timezone, name)
|
|
|
|
if event == "verify":
|
|
return "verify"
|
|
elif event == "not found":
|
|
return "no event"
|
|
|
|
url = f"https://www.googleapis.com/calendar/v3/calendars/{calendar_id}/events/{event['id']}"
|
|
header = {"Authorization": f"Bearer {token}"}
|
|
response = requests.delete(url = url, headers = header)
|
|
if response.status_code == 401:
|
|
return "verify"
|
|
|
|
return "success"
|
|
|
|
def shift_event(token, calendar_id, timezone, name, new_start):
|
|
|
|
event = find_event(token, calendar_id, timezone, name)
|
|
|
|
if event == "verify":
|
|
return "verify"
|
|
elif event == "not found":
|
|
return "no event"
|
|
|
|
if event["start"]["dateTime"][-1] == "Z":
|
|
event["start"]["dateTime"], event["end"]["dateTime"] = event["start"]["dateTime"][0:-1] + "+00:00", event["end"]["dateTime"][0:-1] + "+00:00"
|
|
|
|
event_start, event_end = dt.datetime.strptime(event["start"]["dateTime"][0:-6], "%Y-%m-%dT%H:%M:%S"), dt.datetime.strptime(event["end"]["dateTime"][0:-6], "%Y-%m-%dT%H:%M:%S")
|
|
new_end = new_start + (event_end - event_start)
|
|
event["start"]["dateTime"], event["end"]["dateTime"] = new_start.strftime("%Y-%m-%dT%H:%M:%S"), new_end.strftime("%Y-%m-%dT%H:%M:%S")
|
|
event["description"] = " ".join([(new_start + dt.timedelta(days = i)).strftime("%Y-%m-%d") for i in range((new_end - new_start).days + 1)])
|
|
|
|
url = f"https://www.googleapis.com/calendar/v3/calendars/{calendar_id}/events/{event['id']}"
|
|
header = {"Authorization": f"Bearer {token}"}
|
|
response = requests.put(url = url, headers = header, json = event)
|
|
if response.status_code == 401:
|
|
return "verify"
|
|
|
|
return "success"
|
|
|
|
def find_event_date(token, calendar_id, timezone, name):
|
|
|
|
event = find_event(token, calendar_id, timezone, name)
|
|
if event == "verify":
|
|
return "verify"
|
|
elif event == "not found":
|
|
return "no event"
|
|
|
|
if event["start"]["dateTime"][-1] == "Z":
|
|
event["start"]["dateTime"], event["end"]["dateTime"] = event["start"]["dateTime"][0:-1] + "+00:00", event["end"]["dateTime"][0:-1] + "+00:00"
|
|
|
|
return dt.datetime.strptime(event["start"]["dateTime"][0:-6], "%Y-%m-%dT%H:%M:%S"), dt.datetime.strptime(event["end"]["dateTime"][0:-6], "%Y-%m-%dT%H:%M:%S")
|
|
|
|
def find_date(token, calendar_id, timezone, date):
|
|
|
|
url = f"https://www.googleapis.com/calendar/v3/calendars/{calendar_id}/events"
|
|
params = {
|
|
"q": date.strftime("%Y-%m-%d"),
|
|
"timeZone": timezone
|
|
}
|
|
header = {"Authorization": f"Bearer {token}"}
|
|
response = requests.get(url = url, headers = header, params = params)
|
|
if response.status_code == 401:
|
|
return "verify"
|
|
|
|
events_list = response.json()["items"]
|
|
names = [f'{i["summary"]}: {i["start"]["dateTime"][11:13]}:{i["start"]["dateTime"][14:16]} - {i["end"]["dateTime"][11:13]}:{i["end"]["dateTime"][14:16]}' for i in events_list if date.strftime("%Y-%m-%d") in i["description"]]
|
|
return names
|
|
|
|
def get_calendar(token, timezone):
|
|
|
|
header = {"Authorization": f"Bearer {token}"}
|
|
calendar_response = requests.get(url = "https://www.googleapis.com/calendar/v3/users/me/calendarList", headers = header)
|
|
if calendar_response.status_code == 401:
|
|
return "verify"
|
|
|
|
calendars = calendar_response.json()["items"]
|
|
for i in calendars:
|
|
if i["summary"] == "Jackson Secretary":
|
|
return i["id"]
|
|
|
|
header["Content-Type"] = "application/json"
|
|
payload = {
|
|
"summary": "Jackson Secretary",
|
|
}
|
|
calendar_response = requests.post(url = "https://www.googleapis.com/calendar/v3/calendars", headers = header, json = payload)
|
|
if calendar_response.status_code == 401:
|
|
return "verify"
|
|
|
|
return calendar_response.json()["id"]
|
|
|
|
def execute_json(token, calendar_id, timezone, json_str):
|
|
|
|
try:
|
|
json_obj = json.loads(json_str)
|
|
except:
|
|
return "invalid"
|
|
|
|
result = {
|
|
"schedule": lambda token, calendar_id, timezone, json_obj: create_event(token, calendar_id, {
|
|
"name": json_obj[1],
|
|
"start": dt.datetime.strptime(json_obj[2], "%Y-%m-%d%H:%M"),
|
|
"end": (dt.datetime.strptime(json_obj[2], "%Y-%m-%d%H:%M") + dt.timedelta(hours = 1)) if len(json_obj) == 3 else dt.datetime.strptime(json_obj[3], "%Y-%m-%d%H:%M"),
|
|
"timezone": timezone
|
|
}),
|
|
"cancel": lambda token, calendar_id, timezone, json_obj: cancel_event(token, calendar_id, timezone, json_obj[1]),
|
|
"shift": lambda token, calendar_id, timezone, json_obj: shift_event(token, calendar_id, timezone, json_obj[1], dt.datetime.strptime(json_obj[2], "%Y-%m-%d%H:%M")),
|
|
"event-query": lambda token, calendar_id, timezone, json_obj: find_event_date(token, calendar_id, timezone, json_obj[1]),
|
|
"time-query": lambda token, calendar_id, timezone, json_obj: find_date(token, calendar_id, timezone, dt.datetime.strptime(json_obj[1], "%Y-%m-%d")),
|
|
}[json_obj[0]](token, calendar_id, timezone, json_obj)
|
|
|
|
if result == "verify":
|
|
return "verify"
|
|
|
|
if json_obj[0] == "schedule":
|
|
|
|
if result == "success":
|
|
return f"Event {json_obj[1]} scheduled successfully"
|
|
elif result == "duplicate event":
|
|
return "Event already exists"
|
|
|
|
elif json_obj[0] == "cancel":
|
|
|
|
if result == "success":
|
|
return f"Event {json_obj[1]} cancelled successfully"
|
|
elif result == "no event":
|
|
return "Event not found"
|
|
|
|
elif json_obj[0] == "shift":
|
|
|
|
if result == "success":
|
|
return f"Event {json_obj[1]} shifted successfully"
|
|
elif result == "no event":
|
|
return "Event not found"
|
|
|
|
elif json_obj[0] == "event-query":
|
|
|
|
if result == "no event":
|
|
return "Event not found"
|
|
else:
|
|
return f"Event {json_obj[1]} is scheduled from {result[0].strftime('%d/%m/%Y at %H:%M')} to {result[1].strftime('%d/%m/%Y at %H:%M')}"
|
|
|
|
elif json_obj[0] == "time-query":
|
|
|
|
if len(result):
|
|
return "Events: \n- " + "\n- ".join(result)
|
|
return "No events scheduled for the day"
|