Last active
December 18, 2024 10:17
-
-
Save danielgross/ca65c8b4e6e7e7483ed1aa8565305a5f to your computer and use it in GitHub Desktop.
Quick Add to Google Calendar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Use GPT-3 to generate a calendar event based on natural language input. | |
I tried having GPT-3 generate the URL directly, but, amusingly, that didn't work well. | |
""" | |
import os | |
import sys | |
import openai | |
import datetime | |
import pytz | |
openai.api_key = os.environ.get("OPENAI_API_KEY") or open(os.path.expanduser("~/.openai")).read().strip() | |
def get_event(prompt): | |
"""Use GPT-3 to generate a calendar event based on natural language input.""" | |
template = """I am an assistant for creating calendar events. | |
I will convert a plaintext prompt into specific calendar event details with: | |
start_time, end_time, tz_info, title, description, location. | |
tz_info is the timezone of the event in a format like "America/New_York". | |
If I cannot fill a certain field, I will write "unknown". | |
Times will be formatted like this: "2022-12-25 13:00:00". | |
CURRENT DATE: | |
%s | |
USER PROMPT: | |
%s | |
OUTPUT: | |
* start_time:""" | |
prompt = template % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S%z"), prompt) | |
response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=prompt, | |
max_tokens=200, | |
temperature=0) | |
output = response.choices[0].text | |
output = "* start_time:" + output | |
return output | |
def split_result(event): | |
"""Split the result into key-value pairs.""" | |
key_value_pairs = {} | |
for line in event.splitlines(): | |
if line.startswith("*"): | |
key, value = line[2:].split(":", 1) | |
key_value_pairs[key.strip()] = value.strip() | |
return key_value_pairs | |
def format_start_end_time(start_time, end_time, tz_info): | |
""" | |
Format the start and end time into a Google Calendar format. | |
Google's funky date format is 'Ymd\\THi00\\Z'. | |
Times we are parsing are in the format '2020-09-10 12:00:00' | |
""" | |
start_time = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S") | |
if end_time == "unknown": | |
end_time = start_time + datetime.timedelta(hours=1) | |
else: | |
end_time = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S") | |
start_time = pytz.timezone(tz_info).localize(start_time) | |
end_time = pytz.timezone(tz_info).localize(end_time) | |
start_time = pytz.timezone("UTC").normalize(start_time).strftime("%Y%m%dT%H%M00Z") | |
end_time = pytz.timezone("UTC").normalize(end_time).strftime("%Y%m%dT%H%M00Z") | |
return start_time, end_time | |
def main(): | |
if len(sys.argv) == 1: | |
print("Please provide a prompt.") | |
sys.exit(1) | |
event = get_event(" ".join(sys.argv[1:])) | |
print(event) | |
event = split_result(event) | |
start_time, end_time = format_start_end_time(event["start_time"], event["end_time"], event["tz_info"]) | |
url_params = { | |
"dates": "%s/%s" % (start_time, end_time), | |
"text": event["title"], | |
"details": event["description"], | |
"location": event["location"], | |
} | |
base_url = "http://www.google.com/calendar/render?action=TEMPLATE" | |
url = base_url + "&".join(["&%s=%s" % (k, v) for k, v in url_params.items()]) | |
print(url) | |
os.system("open '%s'" % url) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment