UNPKG

youtube-video-exists

Version:

Checks if a YouTube video exists under the given ID. No API key is required.

69 lines (59 loc) 1.99 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var axios = _interopDefault(require('axios')); /** * Source: https://webapps.stackexchange.com/a/101153 */ var idRegex = /*#__PURE__*/new RegExp('^([0-9A-Za-z_-]{10}[048AEIMQUYcgkosw])$'); /** * Checks if a YouTube video exists under the given ID * * When a video is found the return object also includes the title and author of the video * * @param id - YouTube video id * @returns {@link VideoInfoResponse} when {@link Promise} is resolved * @throws {@link AxiosError} when a network issue occurred */ function getVideoInfo(id) { var videoInfoResponse = { existing: false, validId: false, "private": undefined, info: undefined }; if (idRegex.test(id)) { videoInfoResponse.validId = true; return axios.request({ url: 'oembed', baseURL: 'https://www.youtube.com/', params: { url: "https://youtu.be/" + id, format: 'json' }, validateStatus: function validateStatus(status) { return status === 200 || status === 404 || status === 401; } }).then(function (response) { if (response.status === 200) { videoInfoResponse.existing = true; videoInfoResponse["private"] = false; videoInfoResponse.info = { title: response.data.title, author: { name: response.data.author_name, url: response.data.author_url } }; } else if (response.status === 401) { videoInfoResponse.existing = true; videoInfoResponse["private"] = true; } return videoInfoResponse; }); } else return new Promise(function (resolve) { return resolve(videoInfoResponse); }); } exports.getVideoInfo = getVideoInfo; //# sourceMappingURL=youtube-video-exists.cjs.development.js.map