How to Fetch JSON of all Videos of a Youtube Channel

September 07, 2018

Youtube APIs are great way to fetch details about video or channels. I’ve written a nodejs module for youtube api, and its documentation is available at: Gyanblog Youtube Nodejs Module.

The documentation page describe how you can use the module, fetch details about a video like:

  • thumbnails of video
  • duration
  • dimension
  • view count
  • like count
  • comment count
  • dislike count
  • all comments etc

There is an API available in that module: fetchAllVideosFromChannel, which will give a brief detail about each video from a channel. But, to get detailed attributes for a video, you need to call another API: getVideoDetail and getAllCommentsForVideo.

Code to Fetch all videos in detail and write in file

'use strict';
const fs = require('fs');
const async = require('async');

const youtubeService = require('youtube-api-es6').youtubeService;
const youtubeConfig = {
    key: 'Your key'
};

return youtubeService.init(youtubeConfig)
    .then(function() {
        const channelId = 'UCNNxPxH_zIPxvWy5QMFkruA'; //channel ID
        return youtubeService.fetchAllVideosFromChannel(channelId)
    })
    .then(function(allVideos) {
        //get details of all videos
        return new Promise(function (resolve, reject) {
				    //get details about each single video in details
            async.eachLimit(allVideos, 1,
                function (video, callback) {
                    return youtubeService.getVideoDetail(video.id.videoId)
                        .then(function (videoDetail) {
                            return youtubeService.getAllCommentsForVideo(video.id.videoId)
                                .then(function(comments) {
                                    videoDetail.comments = comments;
                                    return videoDetail;
                                });
                        }.bind(this))
                        .then(function (videoDetail) {
                            //write in file
                            const video_filepath = 'result/' + videoDetail.items[0].id + '.json';

                            console.log('Write in file: ', video_filepath);
                            
                            fs.writeFileSync(video_filepath, JSON.stringify(videoDetail));
                            callback();
                        })
                        .catch(function (err) {
                            logger.log('error', err);
                            callback(err);
                        });
                }.bind(this), function (err) {
                    if (err) {
                        reject(err);
                    } else {
                        resolve();
                    }
                }
            );
        }.bind(this));
    }.bind(this));

Output of the code

Note: Result folder must exist in aboev example. The code will write filenames in format:

<video_id>.json
Example: RvBJ6XdFfj4.json 

Sample output for a video:

{
"comments": [
  {
    "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/cHRQb_eanfimPvW3gfVrnIx112I\"",
    "id": "UgxTDLaAnG5ffFyyioR4AaABAg",
    "kind": "youtube#commentThread",
    "snippet": {
      "canReply": true,
      "isPublic": true,
      "topLevelComment": {
        "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/b_WVOqHakED1c8XBbafBKnCHDbs\"",
        "id": "UgxTDLaAnG5ffFyyioR4AaABAg",
        "kind": "youtube#comment",
        "snippet": {
          "authorChannelId": {
            "value": "UCkORvzRSAzfCdoqjix2bKKQ"
          },
          "authorChannelUrl": "http://www.youtube.com/channel/UCkORvzRSAzfCdoqjix2bKKQ",
          "authorDisplayName": "robert Intros",
          "authorProfileImageUrl": "https://yt3.ggpht.com/-q_saRhxhAnI/AAAAAAAAAAI/AAAAAAAAAAA/KUIqhCm4FsI/s28-c-k-no-mo-rj-c0xffffff/photo.jpg",
          "canRate": true,
          "likeCount": 1,
          "publishedAt": "2018-08-03T11:36:41.000Z",
          "textDisplay": "this is for freign countries not in india homes",
          "textOriginal": "this is for freign countries not in india homes",
          "updatedAt": "2018-08-03T11:36:41.000Z",
          "videoId": "RvBJ6XdFfj4",
          "viewerRating": "none"
        }
      },
      "totalReplyCount": 0,
      "videoId": "RvBJ6XdFfj4"
    }
  },
  {
    "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/FIpplXlb9GTpd0sN5QPp75yEg6Y\"",
    "id": "UgxuDsgy174uzrg9H6t4AaABAg",
    "kind": "youtube#commentThread",
    "snippet": {
      "canReply": true,
      "isPublic": true,
      "topLevelComment": {
        "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/Puxdgvh9B6ttAa5KGrVvbKYZ8jI\"",
        "id": "UgxuDsgy174uzrg9H6t4AaABAg",
        "kind": "youtube#comment",
        "snippet": {
          "authorChannelId": {
            "value": "UCygc3sRNG_EmaWTXHufnW3A"
          },
          "authorChannelUrl": "http://www.youtube.com/channel/UCygc3sRNG_EmaWTXHufnW3A",
          "authorDisplayName": "Laila Lakshmi Sripathi",
          "authorProfileImageUrl": "https://yt3.ggpht.com/-1XWathYsq34/AAAAAAAAAAI/AAAAAAAAAAA/cNTOFQ22VPE/s28-c-k-no-mo-rj-c0xffffff/photo.jpg",
          "canRate": true,
          "likeCount": 1,
          "publishedAt": "2017-11-24T10:04:21.000Z",
          "textDisplay": "nice",
          "textOriginal": "nice",
          "updatedAt": "2017-11-24T10:04:21.000Z",
          "videoId": "RvBJ6XdFfj4",
          "viewerRating": "none"
        }
      },
      "totalReplyCount": 0,
      "videoId": "RvBJ6XdFfj4"
    }
  },
  {
    "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/4EocfPom6yqVOEqNyruW6T-ezrk\"",
    "id": "UghVDICYWi-MMXgCoAEC",
    "kind": "youtube#commentThread",
    "snippet": {
      "canReply": true,
      "isPublic": true,
      "topLevelComment": {
        "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/LTbO3uy2JjeqTyX1rGu686gDXEw\"",
        "id": "UghVDICYWi-MMXgCoAEC",
        "kind": "youtube#comment",
        "snippet": {
          "authorChannelId": {
            "value": "UCUluqA6JeuARlse8Pd9FuYg"
          },
          "authorChannelUrl": "http://www.youtube.com/channel/UCUluqA6JeuARlse8Pd9FuYg",
          "authorDisplayName": "ventende",
          "authorProfileImageUrl": "https://yt3.ggpht.com/-szU1YVE5rwM/AAAAAAAAAAI/AAAAAAAAAAA/g9Na6gX-Ncg/s28-c-k-no-mo-rj-c0xffffff/photo.jpg",
          "canRate": true,
          "likeCount": 1,
          "publishedAt": "2017-07-26T00:25:40.000Z",
          "textDisplay": "Would be easier to maintain a 3D screen system projecting an aquarium. With all sorts of scenarios.",
          "textOriginal": "Would be easier to maintain a 3D screen system projecting an aquarium. With all sorts of scenarios.",
          "updatedAt": "2017-07-26T00:26:06.000Z",
          "videoId": "RvBJ6XdFfj4",
          "viewerRating": "none"
        }
      },
      "totalReplyCount": 0,
      "videoId": "RvBJ6XdFfj4"
    }
  },
  {
    "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/Ml-tMq3ATMVar3Edtu7KzcDkQyo\"",
    "id": "Ughzq0vxKMHb-HgCoAEC",
    "kind": "youtube#commentThread",
    "replies": {
      "comments": [
        {
          "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/C5jXW3EzE1170v__DwRQ32h-Qlc\"",
          "id": "Ughzq0vxKMHb-HgCoAEC.8LT1ebzH4-w8L_h9qdCMwc",
          "kind": "youtube#comment",
          "snippet": {
            "authorChannelId": {
              "value": "UCNNxPxH_zIPxvWy5QMFkruA"
            },
            "authorChannelUrl": "http://www.youtube.com/channel/UCNNxPxH_zIPxvWy5QMFkruA",
            "authorDisplayName": "Plan n Design",
            "authorProfileImageUrl": "https://yt3.ggpht.com/-jOyekwseTi0/AAAAAAAAAAI/AAAAAAAAAAA/Kx-yMDRdmcc/s28-c-k-no-mo-rj-c0xffffff/photo.jpg",
            "canRate": true,
            "likeCount": 0,
            "parentId": "Ughzq0vxKMHb-HgCoAEC",
            "publishedAt": "2016-11-22T12:26:29.000Z",
            "textDisplay": "thanks",
            "textOriginal": "thanks",
            "updatedAt": "2016-11-22T12:26:29.000Z",
            "videoId": "RvBJ6XdFfj4",
            "viewerRating": "none"
          }
        }
      ]
    },
    "snippet": {
      "canReply": true,
      "isPublic": true,
      "topLevelComment": {
        "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/kI82qIiZQQ7Zkzz8N4Q4Hk8eoPQ\"",
        "id": "Ughzq0vxKMHb-HgCoAEC",
        "kind": "youtube#comment",
        "snippet": {
          "authorChannelId": {
            "value": "UCw21Eo0hzkQwVecUQqp9STA"
          },
          "authorChannelUrl": "http://www.youtube.com/channel/UCw21Eo0hzkQwVecUQqp9STA",
          "authorDisplayName": "Life's Student",
          "authorProfileImageUrl": "https://yt3.ggpht.com/-eUO9G6Ar2-M/AAAAAAAAAAI/AAAAAAAAAAA/U4ZzatHJWZI/s28-c-k-no-mo-rj-c0xffffff/photo.jpg",
          "canRate": true,
          "likeCount": 0,
          "publishedAt": "2016-11-19T13:00:24.000Z",
          "textDisplay": "you forgot built in fish aquarium (in the tags)",
          "textOriginal": "you forgot built in fish aquarium (in the tags)",
          "updatedAt": "2016-11-19T13:00:51.000Z",
          "videoId": "RvBJ6XdFfj4",
          "viewerRating": "none"
        }
      },
      "totalReplyCount": 1,
      "videoId": "RvBJ6XdFfj4"
    }
  }
],
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/ehDMB8rcAP7BzzM0wfQiBiSWwTM\"",
"items": [
  {
    "contentDetails": {
      "caption": "false",
      "definition": "hd",
      "dimension": "2d",
      "duration": "PT1M10S",
      "licensedContent": true,
      "projection": "rectangular"
    },
    "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/gE1XJtoHGLSXTpJW_7B9tquY72Q\"",
    "id": "RvBJ6XdFfj4",
    "kind": "youtube#video",
    "snippet": {
      "categoryId": "26",
      "channelId": "UCNNxPxH_zIPxvWy5QMFkruA",
      "channelTitle": "Plan n Design",
      "defaultAudioLanguage": "en-GB",
      "defaultLanguage": "en-GB",
      "description": "Description of the video",
        "title": "22 Extremely Interesting Ideas to put Aquarium in Interior Spaces- Plan n Design"
      },
      "publishedAt": "2016-08-22T06:15:01.000Z",
      "tags": [
        "Plan n Design"
      ],
      "thumbnails": {
        "default": {
          "height": 90,
          "url": "https://i.ytimg.com/vi/RvBJ6XdFfj4/default.jpg",
          "width": 120
        },
        "high": {
          "height": 360,
          "url": "https://i.ytimg.com/vi/RvBJ6XdFfj4/hqdefault.jpg",
          "width": 480
        },
        "maxres": {
          "height": 720,
          "url": "https://i.ytimg.com/vi/RvBJ6XdFfj4/maxresdefault.jpg",
          "width": 1280
        },
        "medium": {
          "height": 180,
          "url": "https://i.ytimg.com/vi/RvBJ6XdFfj4/mqdefault.jpg",
          "width": 320
        },
        "standard": {
          "height": 480,
          "url": "https://i.ytimg.com/vi/RvBJ6XdFfj4/sddefault.jpg",
          "width": 640
        }
      },
      "title": "22 Extremely Interesting Ideas to put Aquarium in Interior Spaces- Plan n Design"
    },
    "statistics": {
      "commentCount": "5",
      "dislikeCount": "45",
      "favoriteCount": "0",
      "likeCount": "426",
      "viewCount": "131645"
    },
    "topicDetails": {
      "relevantTopicIds": [
        "/m/019_rr",
        "/m/03glg",
        "/m/019_rr",
        "/m/03glg"
      ],
      "topicCategories": [
        "https://en.wikipedia.org/wiki/Hobby",
        "https://en.wikipedia.org/wiki/Lifestyle_(sociology)"
      ]
    }
  }
],
"kind": "youtube#videoListResponse",
"pageInfo": {
  "resultsPerPage": 1,
  "totalResults": 1
}
}

Number of files in Example

In my case, I tried to fetch planndesign youtube channel, and it fetched me 109 videos currently as of 14 December 2018.

Hope you enjoyed reading this.


Similar Posts

Latest Posts