0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Auto add Youtube Live streaming schedule to your Google Calendar

Posted at

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

Create a new project
image.png

These pre written codes will be shown.
image.png

Write over all the pre-written codes with the script above.
image.png

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.
image.png

click "Create new calendar"
image.png

Change the name as you prefer and click "create Calendar"
Screenshot 2022-02-03 223528.png

below this, steps are the same for both new Calendar and existing Calendar.

Back to top menu and click gear icon.
Then click Settings.
image.png

From the list in the left column, chooose the calendar you made for the script.
2.png

Find and copy the Calendar ID
Screenshot 2022-02-03 224644.png

Back to GAS, and change the YOUR Calendar ID in the script.
(Do not erase ' ' quotation marks)
image.png

Get YouTube Channel ID

Access to the channel page of your favorite Youtuber
https://www.youtube.com/channel/UCFVkfdFmaOh7BCtrqw8YvaA

image.png

The last letters of URL(~channel/*******) is the channel ID.
Copy it!
(delete "?sub_confirmation=1" etc following channel ID)
image.png

Back to GAS and change the Channel ID
image.png

Prepare API

Click the + next to "Service"
image.png

Add "Google calendar API" and "Youtube Data API"
image.png

Authorization

Save the script
image.png

Then Run!
image.png

This message will be shown if the script is run for the first time.
Click "Review permissions".
image.png

Choose your account
5.png

This warning will be shown.
Click "Advanced"
image.png

Developper is you.
If you can trust Google, yourself and me (the author of this script), click "Go to ******* project".
image.png

Click "Allow"
image.png

Back to GAS, then run again.
image.png

Check your Google Calendar to confirm the script properly worked and the live streaming schedules are registered.
image.png

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.

手順

Click "Triggers" icon
image.png

Click "Add trigger"
image.png

Confirm that "Time-driven" is selected in "Select event source" menu.
image.png

Change "time based trigger" and "interval" to how often you want to refresh the calendar.
(As written above, 15mins is maybe the most frequent)
image.png

Click "Save"
image.png

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

Thank you!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?