This article is a continuation of the article below.
I made a calendar that automatically updates the distribution schedule of Vtuber
Last time, we got video information from YouTube, so this time we will register the schedule in Google Calendar based on the obtained video information.
--YouTube video information --Google Calendar Calendar ID
This time, we will register the schedule in Google Calendar, so naturally we need the calendar of the registration destination. Therefore, check the calendar ID in advance from the calendar settings.
After that, I was able to easily register to the calendar just by following the Official sample.
Just press the button on the sample page. At the same time, save credentials.json in the directory you are working on.
Execute the command exactly as it is written.
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Register the appointment in the calendar based on the sample code.
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/calendar']
def insert_event(event, calendar_id):
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
event = {
'summary': event.summary,
'description': event.description,
'start': {
'dateTime': event.start_datetime,
'timeZone': 'Japan',
},
'end': {
'dateTime': event.end_datetime,
'timeZone': 'Japan',
}
}
event = service.events().insert(calendarId=calendar_id, body=event).execute()
return event['id']
After that, pass the information to be registered in the calendar from the video information acquired last time and the calendar ID confirmed first to this function, and you're done. I decided to pass only the minimum information to be registered in the calendar as an event.
--summary: Video title --description: Video URL --https://www.youtube.com/watch?v= + Video ID --start_datetime: Delivery start time --Basically, actualStartTime is used. The video before the start of distribution is replaced with scheduledStartTime. --end_datetime: Delivery end time --Basically, actualEndTime is used. Set before the start of distribution or 1 hour after scheduledStartTime if it is being distributed.
If you write down the ID of the return value, you can edit the registered schedule, so you can respond to changes in the delivery time and video title.
Recommended Posts