Skip to content

Instantly share code, notes, and snippets.

@picscels
Last active April 20, 2022 11:24
Show Gist options
  • Save picscels/dcd1688bbf978a691855f8c0ec05b41c to your computer and use it in GitHub Desktop.
Save picscels/dcd1688bbf978a691855f8c0ec05b41c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"import os.path\n",
"import pickle\n",
"import pathlib\n",
"import datetime\n",
"\n",
"from googleapiclient.discovery import build\n",
"from google_auth_oauthlib.flow import InstalledAppFlow\n",
"from google.auth.transport.requests import Request\n",
"from googleapiclient.errors import HttpError\n"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
"class GoogleCalendar:\n",
" SCOPES = [\"https://www.googleapis.com/auth/calendar\"]\n",
" API_SERVICE_NAME = \"calendar\"\n",
" API_VERSION = \"v3\"\n",
"\n",
" directory = \"credentials\"\n",
" credentials = pathlib.Path(directory, \"credentials.json\")\n",
" tokenpickle = pathlib.Path(directory, f\"{API_SERVICE_NAME}_token.pickle\")\n",
"\n",
" def __init__(self):\n",
"\n",
" creds = None\n",
" if os.path.exists(self.tokenpickle):\n",
" with open(self.tokenpickle, \"rb\") as token:\n",
" creds = pickle.load(token)\n",
"\n",
" if not creds or not creds.valid:\n",
" if creds and creds.expired and creds.refresh_token:\n",
" creds.refresh(Request())\n",
" else:\n",
" flow = InstalledAppFlow.from_client_secrets_file(\n",
" self.credentials, self.SCOPES\n",
" )\n",
" creds = flow.run_local_server(port=0)\n",
" with open(self.tokenpickle, \"wb\") as token:\n",
" pickle.dump(creds, token)\n",
"\n",
" self.service = build(self.API_SERVICE_NAME, self.API_VERSION, credentials=creds)\n",
"\n",
" def print_todayevents(self):\n",
"\n",
" try:\n",
" today = datetime.datetime.now().strftime(\"%Y-%m-%d\")\n",
" events_result = (\n",
" self.service.events()\n",
" .list(\n",
" calendarId=\"primary\",\n",
" timeMin=today + \"T00:00:00+0900\",\n",
" timeMax=today + \"T23:59:59+0900\",\n",
" singleEvents=True,\n",
" orderBy=\"startTime\",\n",
" )\n",
" .execute()\n",
" )\n",
" events = events_result.get(\"items\", [])\n",
"\n",
" if not events:\n",
" print(\"No upcoming events found.\")\n",
" return\n",
"\n",
" for event in events:\n",
" print(event[\"summary\"])\n",
"\n",
" except HttpError as error:\n",
" print(f\"An error occurred: {error}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"class GoogleTasks:\n",
" SCOPES = [\"https://www.googleapis.com/auth/tasks\"]\n",
" API_SERVICE_NAME = \"tasks\"\n",
" API_VERSION = \"v1\"\n",
"\n",
" directory = \"credentials\"\n",
" credentials = pathlib.Path(directory, \"credentials.json\")\n",
" tokenpickle = pathlib.Path(directory, f\"{API_SERVICE_NAME}_token.pickle\")\n",
"\n",
" def __init__(self):\n",
"\n",
" creds = None\n",
" if os.path.exists(self.tokenpickle):\n",
" with open(self.tokenpickle, \"rb\") as token:\n",
" creds = pickle.load(token)\n",
"\n",
" if not creds or not creds.valid:\n",
" if creds and creds.expired and creds.refresh_token:\n",
" creds.refresh(Request())\n",
" else:\n",
" flow = InstalledAppFlow.from_client_secrets_file(\n",
" self.credentials, self.SCOPES\n",
" )\n",
" creds = flow.run_local_server(port=0)\n",
" with open(self.tokenpickle, \"wb\") as token:\n",
" pickle.dump(creds, token)\n",
"\n",
" self.service = build(self.API_SERVICE_NAME, self.API_VERSION, credentials=creds)\n",
"\n",
" def print_todaytasks(self):\n",
"\n",
" tasklists = self.service.tasklists().list().execute()\n",
" for tasklist in tasklists[\"items\"]:\n",
" if tasklist[\"title\"] == \"Main\":\n",
" tasklistid = tasklist[\"id\"]\n",
"\n",
" try:\n",
" tasks = []\n",
" nextpagetoken = None\n",
" now = datetime.datetime.now()\n",
" today = now.strftime(\"%Y-%m-%d\")\n",
" nextday = (now + datetime.timedelta(days=+1)).strftime(\"%Y-%m-%d\")\n",
" while True:\n",
" response = (\n",
" self.service.tasks()\n",
" .list(\n",
" tasklist=tasklistid,\n",
" dueMin=today + \"T00:00:00Z\",\n",
" dueMax=nextday + \"T00:00:00Z\",\n",
" showCompleted=False,\n",
" showDeleted=False,\n",
" showHidden=False,\n",
" maxResults=100,\n",
" pageToken=nextpagetoken,\n",
" )\n",
" .execute()\n",
" )\n",
" tasks.extend(response.get(\"items\"))\n",
" nextpagetoken = response.get(\"nextPageToken\")\n",
" if not nextpagetoken:\n",
" break\n",
"\n",
" for task in tasks:\n",
" print(task[\"title\"])\n",
"\n",
" except HttpError as err:\n",
" print(f\"An error occurred: {error}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"テスト前日23-テスト当日2\n",
"テスト当日0-1\n",
"テスト終日\n",
"テスト当日4-5\n",
"テスト当日17-20\n",
"テスト当日23-テスト翌日2\n",
"テスト当日23-24\n"
]
}
],
"source": [
"gcalendar = GoogleCalendar()\n",
"gcalendar.print_todayevents()\n"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"当日タスクテスト2\n",
"当日タスクテスト1\n"
]
}
],
"source": [
"gtasks = GoogleTasks()\n",
"gtasks.print_todaytasks()\n"
]
}
],
"metadata": {
"interpreter": {
"hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment