calendar-booking/Test.ipynb

248 lines
6.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"id": "cc56de59",
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"from google.auth.transport.requests import Request\n",
"from google.oauth2.credentials import Credentials\n",
"from google_auth_oauthlib.flow import InstalledAppFlow\n",
"from googleapiclient.discovery import build\n",
"#from googleapiclient.errors import HttpError\n",
"import os"
]
},
{
"cell_type": "markdown",
"id": "ec6f4b30",
"metadata": {},
"source": [
"# Authenticate"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "afc203d8",
"metadata": {},
"outputs": [],
"source": [
"SCOPES = ['https://www.googleapis.com/auth/calendar']"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "f24d1ed8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading credentials from file\n"
]
}
],
"source": [
"if os.path.exists('creds.json'):\n",
" print(\"Loading credentials from file\")\n",
" creds = Credentials.from_authorized_user_file('creds.json', SCOPES)\n",
"\n",
" if not creds.valid:\n",
" creds.refresh(Request())\n",
"else:\n",
" print(\"Getting credentials\")\n",
" flow = InstalledAppFlow.from_client_secrets_file(\n",
" 'app.json',\n",
" SCOPES,\n",
" )\n",
" creds = flow.run_local_server(port=0)\n",
" with open('creds.json', 'w') as fp:\n",
" fp.write(creds.to_json())"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "169209cd",
"metadata": {},
"outputs": [],
"source": [
"service = build('calendar', 'v3', credentials=creds)"
]
},
{
"cell_type": "markdown",
"id": "24280d27",
"metadata": {},
"source": [
"# Get events"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "32249c11",
"metadata": {},
"outputs": [],
"source": [
"now = datetime.utcnow().isoformat() + 'Z'\n",
"events_result = service.events().list(\n",
" calendarId='primary',\n",
" timeMin=now,\n",
" maxResults=10,\n",
" singleEvents=True,\n",
" orderBy='startTime',\n",
").execute()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "243551bd",
"metadata": {},
"outputs": [],
"source": [
"for event in events_result.get('items', []):\n",
" print(event['start'].get('dateTime') or event['start'].get('date'))\n",
" print(event['summary'])"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "5c3996cd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'kind': 'calendar#event',\n",
" 'etag': '\"3304780330438000\"',\n",
" 'id': '3mco45cklnem88pbcgs7k37em0',\n",
" 'status': 'confirmed',\n",
" 'htmlLink': 'https://www.google.com/calendar/event?eid=M21jbzQ1Y2tsbmVtODhwYmNnczdrMzdlbTAgcnIyMzY5QG55dS5lZHU',\n",
" 'created': '2022-05-12T21:16:05.000Z',\n",
" 'updated': '2022-05-12T21:16:05.219Z',\n",
" 'summary': 'Test event from API',\n",
" 'creator': {'email': 'rr2369@nyu.edu', 'self': True},\n",
" 'organizer': {'email': 'rr2369@nyu.edu', 'self': True},\n",
" 'start': {'dateTime': '2022-05-12T19:30:00-04:00',\n",
" 'timeZone': 'America/New_York'},\n",
" 'end': {'dateTime': '2022-05-12T19:45:00-04:00',\n",
" 'timeZone': 'America/New_York'},\n",
" 'iCalUID': '3mco45cklnem88pbcgs7k37em0@google.com',\n",
" 'sequence': 0,\n",
" 'reminders': {'useDefault': True},\n",
" 'eventType': 'default'}"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"event"
]
},
{
"cell_type": "markdown",
"id": "1a110255",
"metadata": {},
"source": [
"# Create an event"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "33021064",
"metadata": {},
"outputs": [],
"source": [
"result = service.events().insert(\n",
" calendarId='primary',\n",
" body=dict(\n",
" start={'dateTime': '2022-05-12T23:30:00Z'},\n",
" end={'dateTime': '2022-05-12T23:45:00Z'},\n",
" summary=\"Test event from API\",\n",
" ),\n",
").execute()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "1f9ba98a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'kind': 'calendar#event',\n",
" 'etag': '\"3304780330438000\"',\n",
" 'id': '3mco45cklnem88pbcgs7k37em0',\n",
" 'status': 'confirmed',\n",
" 'htmlLink': 'https://www.google.com/calendar/event?eid=M21jbzQ1Y2tsbmVtODhwYmNnczdrMzdlbTAgcnIyMzY5QG55dS5lZHU',\n",
" 'created': '2022-05-12T21:16:05.000Z',\n",
" 'updated': '2022-05-12T21:16:05.219Z',\n",
" 'summary': 'Test event from API',\n",
" 'creator': {'email': 'rr2369@nyu.edu', 'self': True},\n",
" 'organizer': {'email': 'rr2369@nyu.edu', 'self': True},\n",
" 'start': {'dateTime': '2022-05-12T19:30:00-04:00',\n",
" 'timeZone': 'America/New_York'},\n",
" 'end': {'dateTime': '2022-05-12T19:45:00-04:00',\n",
" 'timeZone': 'America/New_York'},\n",
" 'iCalUID': '3mco45cklnem88pbcgs7k37em0@google.com',\n",
" 'sequence': 0,\n",
" 'reminders': {'useDefault': True},\n",
" 'eventType': 'default'}"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f78dd2a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "_rr_calendar-booking",
"language": "python",
"name": "_rr_calendar-booking"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}