~/tutorials/how-to-fetch-json-all-videos-youtube-channel.md

How to Fetch JSON of all Videos of a Youtube Channel

// TL;DR

Use the youtube-api-es6 Node.js module to paginate through a YouTube channel's uploads playlist and retrieve JSON details for every video.

How to Fetch JSON of all Videos of a Youtube Channel

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: GyanByte 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.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Youtube API in NodeJs, Usage with Example

Youtube API in NodeJs, Usage with Example

This library is ES6, promise compatible. Or, in your package.json file, include…

How to Download multiple Youtube Videos using Nodejs and Show a Progress Bar

How to Download multiple Youtube Videos using Nodejs and Show a Progress Bar

Introduction I was trying to download some youtube videos for my kids. As I have…

How to get Youtube Video Thumbnail Images

How to get Youtube Video Thumbnail Images

If your youtube video looks like:https://www.youtube.com/watch?v=g0kFl7sBdDQ…

Claude Code Skills — Build a Better Engineering Workflow

Claude Code Skills — Build a Better Engineering Workflow

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…