UNPKG

@sugarcube/plugin-youtube

Version:
157 lines (137 loc) 4.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.videoChannelPlaylist = exports.videoChannel = exports.videosListCheck = exports.videosList = exports.channelExists = exports.channelSearch = void 0; var _fp = require("lodash/fp"); var _pify = _interopRequireDefault(require("pify")); var _request = _interopRequireDefault(require("request")); var _url = require("url"); var _dashp = require("dashp"); var _entities = require("./entities"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const getAsync = (0, _pify.default)(_request.default.get); const urlify = (0, _fp.curry)((resource, params) => { const endpoint = "https://www.googleapis.com/youtube/v3"; const u = (0, _url.parse)(`${endpoint}/${resource}`); u.query = params; return (0, _url.format)(u); }); const getJson = url => getAsync(url).then(r => JSON.parse(r.body)); const handleError = async (action, params) => { const { error, ...resp } = await action(params); if (error != null) { throw new Error(error.message); } return resp; }; const page = async (action, params, results = []) => { const { items, nextPageToken } = await handleError(action, params); if (nextPageToken) { return page(action, (0, _fp.merge)(params, { pageToken: nextPageToken }), (0, _fp.concat)(results, items)); } return (0, _fp.concat)(results, items); }; const search = (0, _fp.flow)([urlify("search"), getJson]); const videos = (0, _fp.flow)([urlify("videos"), getJson]); const channels = (0, _fp.flow)([urlify("channels"), getJson]); const playlistItems = (0, _fp.flow)([urlify("playlistItems"), getJson]); const getplaylistid = async (action, params) => { const { items } = await handleError(action, params); if (items.length > 0) return items[0].contentDetails.relatedPlaylists.uploads; return null; }; const channelSearch = (0, _fp.curry)((key, range, channelId) => { const parts = ["id", "snippet"]; const params = { type: "video", part: (0, _fp.join)(",", parts), maxResults: 50, channelId, key }; return page(search, (0, _fp.merge)(params, range)); }); exports.channelSearch = channelSearch; const channelExists = (0, _fp.curry)(async (key, id) => { const params = { part: "id", id, key }; const { pageInfo } = await handleError(channels, params); return pageInfo.totalResults > 0; }); exports.channelExists = channelExists; const channelToPlaylist = (0, _fp.curry)((key, id) => { const params = { part: "contentDetails", id, key }; return getplaylistid(channels, params); }); const playlistVideos = (0, _fp.curry)((key, playlistId) => { const parts = ["id", "snippet", "status", "contentDetails"]; const ps = { part: (0, _fp.join)(",", parts), playlistId, maxResults: 50, key }; return page(playlistItems, ps).then((0, _fp.map)(_entities.playlistVideo)); }); const videosList = (0, _fp.curry)((key, ids) => { const parts = ["id", "snippet", "contentDetails", "statistics", "status", "recordingDetails", "topicDetails"]; const params = { part: (0, _fp.join)(",", parts), id: (0, _fp.join)(",", Array.isArray(ids) ? ids : [ids]), maxResults: 50, key }; return page(videos, params).then((0, _fp.map)(_entities.video)); }); exports.videosList = videosList; const videosListCheck = (0, _fp.curry)((key, ids) => { const parts = ["id"]; const params = { part: (0, _fp.join)(",", parts), id: (0, _fp.join)(",", Array.isArray(ids) ? ids : [ids]), maxResults: 50, key }; return page(videos, params); }); exports.videosListCheck = videosListCheck; const videoChannel = (0, _fp.curry)(async (key, range, id) => { const resp = await channelSearch(key, range, id); const ids = (0, _fp.map)((0, _fp.property)("id.videoId"), resp); // There is a limit on how many video ids can be queried at once. return (0, _dashp.flatmapP)(videosList(key), (0, _fp.chunk)(50, ids)); }); exports.videoChannel = videoChannel; const videoChannelPlaylist = (0, _fp.curry)(async (key, id) => { const playlistId = await channelToPlaylist(key, id); if (playlistId != null) return (0, _fp.flatten)(await playlistVideos(key, playlistId)); return []; }); exports.videoChannelPlaylist = videoChannelPlaylist; var _default = { channelSearch, videosList, videosListCheck, videoChannel, videoChannelPlaylist }; exports.default = _default;