Giter Site home page Giter Site logo

youtube-api's Introduction

YouTube API

This is a small library with no dependencies* to work with YouTube API. Can work either on client side or with nodejs.

*node-fetch is required in nodejs environment

Usage

Search for videos

import { YoutubeSearch } from 'youtube-api';

const options = {
  key: 'YOUTUBE_API_KEY',
  part: 'snippet',
  maxResults: 10
};

async function search(searcher, query) {
  try {
    return await searcher.search(query);
  } catch (e) {
    // Handle error
  }
}

const youtubeSearcher = new YoutubeSearch(options);

// .search() method returns an array of videos
// it's size less or equals maxResults
// or 0 if there are no more results or request was aborted with .abort()
const first10Videos = search(youtubeSearcher, 'search query');

// calling .search() again with the same query string returns the next resulting page
const next10Videos = search(youtubeSearcher, 'search query');

You can call .search() method with the same search query string in order to get the next page of found videos.

Video details

Retrieve a list of videos with details.

import { YoutubeVideos } from 'youtube-api';

const options = {
  key: 'YOUTUBE_API_KEY'
};

// Array of video ids
const videoId = ['videoId'];

const videos = new YouTubeVideos({ ...options, part: 'id' });
videos.list(videoId).then(res => {
    console.log(res);
});

Captions

You can retrieve a list of video captions or download a particular caption. Please note that downloading captions requires authorization.

import { YoutubeCaptions } from 'youtube-api';

const options = {
  access_token: 'YOUTUBE_ACCESS_TOKEN'
};

// Video ID
const videoId = 'videoId';

const captions = new YoutubeCaptions(options);

// The first parameter is string with video id
// The second parameter is part. May be 'id' or 'snippet'.
// https://developers.google.com/youtube/v3/docs/captions/list
captions.list(videoId, 'snippet').then(res => {
    if (res.items.length) {
        // Retrieve the first caption track
        captions.download(res.items[0].id).then(blob => {
            // returns Blob
        });
    }
});

Options

All class constructors have options parameter. It should have either key or access_token key. See next sections for details.

Options object may contain any of supported YouTube API parameters. See particular YouTube API reference page for more details.

Google API Key

You need to create an API key in order to perform YouTube API requests. Some requests require authorization. In that case you need to create OAuth 2 credentials and use YouTubeAuth class to obtain access token.

Authorization

Some API requests require authorization. In order to obtain access token to perform such requests you can use YouTubeAuth class. There is YouTubeAuth.createAuthUrl(params) static method that creates an URL to Google authorization server. You need to pass your client_id and redirect_uri along with other optional parameters to it and then redirect user to the returned URL. After authentication the server redirects back to the redirect_uri with the response in hash or query string (depends on access type). For access_type=offline the client_secret is required as well. Then use .fetchAccessTokenWithCallbackUrl(callbackUrl) method in order to obtain access token. If server returns an error an exception will be thrown. You can store the token and use it for requests instead of API key. For access_type=offline a refresh token will be also provided. Store it in a secure place and use for refreshing access token once it's expired. See Google guide and an example for details.

Revoke token

Server side application can revoke access token with .revokeToken(token) method of YouTubeAuth instance. It accepts token obtained with one of .fetchAccessToken* methods. In order to revoke access token of client side application follow documentation.

Timeout

Request timeout is 5000ms by default. You can change it with options.timeout.

const options = {
  key: 'YOUTUBE_API_KEY',
  // Number of milliseconds
  timeout: 3000
};

Aborting request

You can abort a request by calling .abort() method.

const searcher = new YoutubeSearch(options);
searcher.search(query).then(results => { ... });
// Abort the previous request
searcher.abort();
searcher.search(query).then(results => { ... });

youtube-api's People

Contributors

trypton avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.