Accessing YouTube data programmatically using Scala

Few days back, I wanted to save the list of vidoes in few youtube playlists for future references. But on a quick google search, I was not able to find any simple examples of accessing youtube data apis using scala (or even in Java).

Here is a quick notes on how I used Google Java data apis to access youtube data in scala.

Setup 0 - build.sbt

First, let's start by creating a simple build.sbt and include google youtube api dependency.

// build.sbt
name := """youtube-test"""
version := "1.0-SNAPSHOT"
scalaVersion := "2.11.7"
libraryDependencies += "com.google.apis" % "google-api-services-youtube" % "v3-rev174-1.22.0"

Once we have that, we can experiment using sbt console or write normal scala code.

Setup 1- API key for accessing google data apis

Google data apis provide access through two main mechanisms:

  1. API Key -- simple api key based approach

  2. OAuth 2.0 -- useful if you want to login on behalf of some user. Required for say uploading a new video on behalf of a user etc.

For our use case, we will use API key -- more especially a server api key. You can find more details about how to create a server api key at following google developers page. Once you have created a server api key, you are all set to write some scala code.

Use Case 1: Fetching single video details in scala

import com.google.api.services.youtube.{YouTube, YouTubeRequestInitializer}
import com.google.api.services.youtube.model.Video
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.client.http.javanet.NetHttpTransport
import com.google.api.client.http.HttpRequest
import com.google.api.client.http.HttpRequestInitializer



\\ init google api access
val transport = new NetHttpTransport()
val factory =  new JacksonFactory()	
val httpRequestInit = new HttpRequestInitializer {
  override def initialize(re: HttpRequest ) =  { }
}

val service = new YouTube.Builder(transport, factory, httpRequestInit).setApplicationName("test").setYouTubeRequestInitializer(new YouTubeRequestInitializer(<API KEY>)).build()

\\ get details of a video with id ecekSCX3B4Q
val videoResponse = service.videos().list("snippet").setId("ecekSCX3B4Q").execute()

\\ videoResponse contains list of items. We can access first result using
val video =  videoResponse.getItems()(0)

\\ print video title
println(video.getSnippet.getTitle)

\\ print description
println(video.getSnippet.getDescription)

You can figure out more details of the apis from youtube api documentation

Use Case 2: Fetching list of videos in a playlist in scala

val list = service.playlistItems().list("snippet,contentDetails")
list.setMaxResults(50L)
list.setPlaylistId("PLuAEyXjRhil-MnC_4m-Eat6F644y096HC");
val videos = list.execute().getItems();

\\ videos type is java.util.List[com.google.api.services.youtube.model.PlaylistItem]

\\ priting youtube id of every video in the list
import scala.collection.JavaConversions._
videos.foreach( v => println(v.getContentDetails.getVideoId  ))

That's it.

Category: scala