how to get videoCategoryId from videoID youtubube api

376 views Asked by At

Website link if you want to know how it works - https://nerdtube.000webhostapp.com/home.html

i am building a site where only education category videos would be fetched and iframed. Like if someone inputs "cat" in my searchbar, i should only get cat videos/playlist which have category "education". I have the videoID, but i dont seem to find the category ID in the json returned.

function search(id,tit) {
  //console.log(id);

    var key = 'MY API KEY';
     //playlistId = 'PLUl4u3cNGP6317WaSNfmCvGym2ucw3oGp';
     var playlistId=id;
    var URL = 'https://www.googleapis.com/youtube/v3/playlistItems';
    //playlistId=document.getElementById('searchTerm').value;
    //console.log(playlistId);
    $('#plname').html(
      `<h1>Playlist : ${tit}</h1>`
    )


    var options = {
        part: 'snippet',
        key: key,
        maxResults: 60,
        playlistId: playlistId,

    }
    //console.log(playlistId);

    loadVids();

    function loadVids() {
        $.getJSON(URL, options, function (data) {
            var id = data.items[0].snippet.resourceId.videoId;
            console.log(data);
            resultsLoop(data);
            mainVid(id);

        });
    }

    function mainVid(id) {

        document.getElementById('plname').style.display="block";
        $('#video').html(`
                    <iframe src="https://www.youtube.com/embed/${id}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
                `);
    }


    function resultsLoop(data) {
        document.getElementById('dec').style.display="block";

        $.each(data.items, function (i, item) {

            var thumb = item.snippet.thumbnails.medium.url;
            var title = item.snippet.title;
            var desc = item.snippet.description.substring(0, 100);
            var vid = item.snippet.resourceId.videoId;


            $('main').append(`

            <a>
                            <article class="item" data-key="${vid}">

                                <img src="${thumb}" alt="" class="thumb">
                                <div class="details">

                                    <h4>${title}</h4>
                                    <p>${desc}</p>

                                </div>

                            </article>
              </a>
                        `);
        });
    }

        // CLICK EVENT
    $('main').on('click', 'article', function () {
        var id = $(this).attr('data-key');
        mainVid(id);
    });


}

so what i am doing is (code not shown here) i first get objects that are playlist type from searchbar, show the playlists and when someone clicks on a playlist, this search(id, tit) function starts which gradually fetches all the videos in that playlist. Now i want to filter only the education category videos but json doesnt have that part i guess.

1

There are 1 answers

9
pedde07 On

Edit:

I probably misunderstood your question and your usecase. Sorry for the confusion. But I think the answer and especially the comments are useful so I decided not to delete the answer.

Thanks @stvar for your comments

My recommendation

For me your usecase sounds like it would be better to use the API endpoint "serach/list" or "videos/list" instead of the API endpoint "playlistItems".

By using the endpoint "serach/list" or "videos/list" you can already restrict the results to the video category by specifiying the "videoCategoryId" in the initial request.

For example:

  • https://www.googleapis.com/youtube/v3/search?videoCategoryId=[YOUR_VIDEO_CATEGORY_ID]&...
  • https://www.googleapis.com/youtube/v3/videos?videoCategoryId=[YOUR_VIDEO_CATEGORY_ID]&...

I would recommend to specify as much parameters as possible in order to have the best possible results for your site/application. But note that not all combinations are allowed.

Use the "Try this API" in the API documentation in order to narrow your request:

Keep in mind that the endpoint "search/list" has a significantly increased cost-factor for the API quotas. It provides more flexibility so you can include or exlude live streams and specify much more options in comparison to "videos/list".

Alternative method (not verified by testing it myself)

This is not working as explained in the comments because "id" and "videoCategoryId" are exclusive parameters.

If this is not an option for you then you can make an additional API call for each video in the result of your "playlistItems" API call. Specify the "Video ID"" and "Video Category ID". If you get no result skip the video and do not show it on your site, e.g.:

https://www.googleapis.com/youtube/v3/videos?id=[YOUR_VIDEO_ID]&videoCategoryId=[YOUR_VIDEO_CATEGORY_ID]&...