Summary
This script add a live stream reservation of any Youtuber you choose to your Google calendar.
You can automatically get the streaming schedule of your favorite Youtuber.
Features
This script can
- automatically register the time, URL and description of plan live streaming reserved by Youtube Channel.
- put URL into location slot, streaming description into Calendar description.
- If the detail of reserved live stream is updated, Calendar event gets also updated.
- If stream is cancelled, event will be cancelled.
Unables
This scrips does not work for
- premiere video.
- member-only stream.
Privacy
Of course, this script does not deliver any information of you to me.
Script
let scheduleUpcomingLive=()=> {
let results = YouTube.Search.list(
'snippet', {
channelId: 'CHANNEL ID', //change CHANNEL ID to your favorite Youtuber's Channel ID (You can get it from URL of their channel top page)
safeSearch: 'none',
eventType:'upcoming',
type:'video'
}
);
let calendarId = 'YOUR CALENDAR ID';// change YOUR CALENDAR ID to your Google calendar's ID
let now = new Date()
let optionalArgs = {
timeMax: (new Date(now.getTime() + (1095 * 24 * 60 * 60 * 1000))).toISOString(),
timeMin: (new Date(now.getTime() - ( 7 * 24 * 60 * 60 * 1000))).toISOString(),
showDeleted: false,
singleEvents: true
};
let events = Calendar.Events.list(calendarId, optionalArgs).items;
let arrayOfLocation = []
for (let n in events) {
arrayOfLocation.splice(n,0,events[n].getLocation())
}
let arrayOfDescription =[]
let arrayOfURL =[]
let VIdIs = "videoID is "
for(let i in results.items){
let resultsVideo = YouTube.Videos.list(
'snippet,liveStreamingDetails',{
id : results.items[i].id.videoId
}
)
let VideoId = results.items[i].id.videoId
let URLofStream = 'https://www.youtube.com/watch?v=' + VideoId;
let descriptionV = resultsVideo.items[0].snippet.description+"\n\n"+"videoID is "+VideoId
let title = resultsVideo.items[0].snippet.title;
let startTime = new Date(resultsVideo.items[0].liveStreamingDetails.scheduledStartTime);
let endTime = new Date(resultsVideo.items[0].liveStreamingDetails.scheduledStartTime);
endTime.setHours(endTime.getHours() + 1)
let option = {
description: descriptionV,
location: URLofStream
}
if (arrayOfLocation.indexOf(URLofStream) < 0 ) {
CalendarApp.getCalendarById(calendarId).createEvent(title, startTime, endTime, option);
} else {
let eventsToUpdate = Calendar.Events.list(
calendarId, {
timeMax: (new Date(now.getTime() + (1095 * 24 * 60 * 60 * 1000))).toISOString(),
timeMin: (new Date(now.getTime() - ( 7 * 24 * 60 * 60 * 1000))).toISOString(),
maxResults:1,
singleEvents: true,
q:VideoId
}
);
let eventId = eventsToUpdate.items[0].id;
let startTimeVideoISO = new Date(startTime).toISOString()
let startTimeEventISO = new Date(eventsToUpdate.items[0].start.dateTime).toISOString()
if (startTimeVideoISO !== startTimeEventISO) {
CalendarApp.getCalendarById(calendarId).getEventById(eventId).setTime(startTime, endTime).setTitle(title).setDescription(descriptionV)
}
if (title !== eventsToUpdate.items[0].summary) {
CalendarApp.getCalendarById(calendarId).getEventById(eventId).setTitle(title).setDescription(descriptionV)
}
if (descriptionV !== eventsToUpdate.items[0].description) {
CalendarApp.getCalendarById(calendarId).getEventById(eventId).setDescription(descriptionV)
}
}
arrayOfDescription.splice(i,0,descriptionV)
arrayOfURL.splice(i,0,URLofStream)
}
optionalArgs = {
timeMax: (new Date(now.getTime() + (1095 * 24 * 60 * 60 * 1000))).toISOString(),
timeMin: (new Date(now.getTime() + (60 * 60 * 1000))).toISOString(),
showDeleted: false,
singleEvents: true
};
let eventsRemove = Calendar.Events.list(calendarId, optionalArgs).items;
for (let o in eventsRemove) {
let descriptionE = eventsRemove[o].getDescription() + "to avoid undef"
let locationE = eventsRemove[o].getLocation()
if(descriptionE.indexOf(VIdIs) >= 0){
if (arrayOfURL.indexOf(locationE) < 0 ) {
Calendar.Events.remove(calendarId,eventsRemove[o].id)
}
}
}
}
Installation
Before all, please finish following steps.
- Get a Google account
- Start using Google Calendar
- Start using Youtube
(If you share your Calendar openly with others, your gmail address will be known.
Do not use your private address if you post your sharing link in twitter etc)
I use the channel of Kurono Kureha san for example.
Prepare Google Apps Script
Access to Google Apps Script (GAS)
https://script.google.com/home
These pre written codes will be shown.
Write over all the pre-written codes with the script above.
You have to change the Calendar ID and Channel ID.
How?
Keep reading! :)
Get Google calendar ID
(If you want to use a calendar already existing, please skip the next step)
Open Google Calendar
https://calendar.google.com/
Create new Calendar
Click + icon to add other calendars.
Change the name as you prefer and click "create Calendar"
below this, steps are the same for both new Calendar and existing Calendar.
Back to top menu and click gear icon.
Then click Settings.
From the list in the left column, chooose the calendar you made for the script.
Back to GAS, and change the YOUR Calendar ID in the script.
(Do not erase ' ' quotation marks)
Get YouTube Channel ID
Access to the channel page of your favorite Youtuber
https://www.youtube.com/channel/UCFVkfdFmaOh7BCtrqw8YvaA
The last letters of URL(~channel/*******) is the channel ID.
Copy it!
(delete "?sub_confirmation=1" etc following channel ID)
Back to GAS and change the Channel ID
Prepare API
Add "Google calendar API" and "Youtube Data API"
Authorization
This message will be shown if the script is run for the first time.
Click "Review permissions".
This warning will be shown.
Click "Advanced"
Developper is you.
If you can trust Google, yourself and me (the author of this script), click "Go to ******* project".
Check your Google Calendar to confirm the script properly worked and the live streaming schedules are registered.
Trigger
In order to run the script periodically and check if there is new live streaming reservation, prepare trigger.
There is a limit for the time you can let the script access to Youtube.
From my experience, checking every 15mins is the most frequent available trigger.
If you made two apps, double the time, 30mins.
手順
Confirm that "Time-driven" is selected in "Select event source" menu.
Change "time based trigger" and "interval" to how often you want to refresh the calendar.
(As written above, 15mins is maybe the most frequent)
All done!
Share your effort
If you kindly want to help others, you can share your calendar to others and save others' time and Youtube API quota.
But be careful.
Your email address of the Google calendar account will be shown to others.
Do not use private email address.
Here is how.
https://support.google.com/calendar/answer/37083?hl=en