Automate your workflow with YouTube Data API

Achanandhi M
3 min readJul 11, 2024

--

Hello All, In the Last blog I mentioned about my small side projects in Go. Actually what I was planning is, see YouTube as my go-to platform for learning new thinking. I’ve been following many YouTubers and their channels. But the real problem is turning off the notifications on my phone, I hate notifications, I didn’t get any notifications on my phone. I turned off everything.

Sometimes I couldn’t watch the videos from the YouTubers, I was missing something, I wanted to check every YouTube channel manually, and whether any videos were uploaded or not, these are the things that I have been doing till now. So what I am trying to do is, I have to build something that tells me if there are any videos or shorts uploaded from the particular channel. It is more like a pub sub-model. I didn’t think about anything. Just started yesterday.

Yesterday, I used the first time Youtube API, for fetching the video from the channel which I mentioned, programmatically.

Yes, you can play with YouTube through its APIs.

To get the APIs, Go to https://console.cloud.google.com

After that Click API and Services, then search for Youtube Data API V3

After enabling it will look like this, try to explore the API by yourself, and click generate API

Once you are ready with the API key, we need to implement this functionality through code, for this demo, I am using Go.

package main

import (
"fmt"
"log"
"net/http"
"os"
"strings"
"bufio"
"google.golang.org/api/googleapi/transport"
"google.golang.org/api/youtube/v3"
)

func clientChoice(){

reader := bufio.NewReader(os.Stdin)

fmt.Println("Enter the Youtube Channel name: ")

userchoice, _ := reader.ReadString('\n')

userchoice = strings.TrimSpace(userchoice)

fmt.Println("Enter your Youtube API key: ")

apiKey, _ := reader.ReadString('\n')

apiKey = strings.TrimSpace(apiKey)


client := &http.Client{
Transport: &transport.APIKey{Key: apiKey},
}
service, err := youtube.New(client)
if err != nil {
log.Fatalf("Error creating YouTube client: %v", err)
}
searchResponse, err := service.Search.List([]string{"snippet"}).
Q(userchoice).
Type("video").
Order("date").
MaxResults(1).
Do()
if err != nil {
log.Fatalf("Error searching for videos: %v", err)
}

// Process search response


for _, item := range searchResponse.Items {
videoTitle := item.Snippet.Title
videoId := item.Id.VideoId

// Print the latest video information
fmt.Printf("Latest Video Title: %s\n", videoTitle)
fmt.Printf("Video ID: %s\n", videoId)
fmt.Printf("Watch it here: https://www.youtube.com/watch?v=%s\n", videoId)

}
//time.Sleep(24 * time.Hour)
}

func main() {
clientChoice()

}

Basically, the above code gets the YouTube channel from the user, and based on the user's choice, the code will print the latest video from that channel. This is how the code will function.

Note: You can try the above approach in any language, but you need API, that’s it.

Output:

How cool right? When I first ran this code, I was so happy that I interacted with YouTube programmatically. But it does not give a 100 percent correct answer, I tried with different channels it fails to give the correct answer, I don’t why, If you are interested just try this by yourself.

In the future I had a plan to build something using this approach, to solve my problem. I think we have seen something interesting and a new topic today, see you tomorrow with another tech blog. Happy Learning

--

--

Achanandhi M

CNCF Enthusiast 🙋🏻 | Cloud Native Build packs| Cloud | DevOps♾️ | Docker🐬 | 🏗️Microservice| Python🐍 | Go | K8s☸️| Linux🐧