UNPKG

@sugarcube/plugin-youtube

Version:
150 lines (132 loc) 4.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.playlistVideo = exports.video = exports.ncubeVideo = exports.deprecatedVideo = void 0; var _fp = require("lodash/fp"); var _parse = _interopRequireDefault(require("date-fns/parse")); var _core = require("@sugarcube/core"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // Use deprecated Sugarcube data format. This is still the default but will be // replaced with the data formate defined in ncubeVideo below. const deprecatedVideo = item => { let locations = []; if (item.recordingDetails != null && item.recordingDetails.location != null && item.recordingDetails.location.longitude != null && item.recordingDetails.location.latitude != null) { const { longitude, latitude } = item.recordingDetails.location; const location = { location: { lon: longitude, lat: latitude }, type: "youtube_recording", term: [longitude, latitude], description: item.recordingDetails.locationDescription }; locations = [location]; } const language = item.snippet != null && item.snippet.defaultLanguage != null ? item.snippet.defaultLanguage : null; return (0, _fp.merge)(item, { _sc_id_fields: ["id"], _sc_content_fields: ["snippet.title", "snippet.description"], _sc_pubdates: { source: (0, _parse.default)(item.snippet.publishedAt) }, _sc_locations: locations, _sc_language: language, _sc_media: [{ type: "image", term: item.snippet.thumbnails.high.url, width: item.snippet.thumbnails.high.width, height: item.snippet.thumbnails.high.height }, { type: "video", term: `https://www.youtube.com/watch?v=${item.id}` }, { type: "url", term: `https://www.youtube.com/watch?v=${item.id}` }] }); }; // This formats a youtube video in the format that is compatible with the SQL plugin and Ncube. exports.deprecatedVideo = deprecatedVideo; const ncubeVideo = item => { const id = (0, _fp.get)("id", item); const href = `https://youtube.com/watch?v=${id}`; const title = (0, _fp.get)("snippet.title", item); const description = (0, _fp.get)("snippet.description", item); const channelId = (0, _fp.get)("snippet.channelId", item); const channel = (0, _fp.get)("snippet.channelTitle", item); const stats = (0, _fp.getOr)({}, "snippt.statistics", item); const createdAt = (0, _fp.get)("snippet.publishedAt", item); let locations = []; if (item.recordingDetails != null && item.recordingDetails.location != null && item.recordingDetails.location.longitude != null && item.recordingDetails.location.latitude != null) { const { longitude, latitude } = item.recordingDetails.location; const location = { location: { lon: longitude, lat: latitude }, type: "youtube_recording", term: [longitude, latitude], description: item.recordingDetails.locationDescription }; locations = [location]; } const language = item.snippet != null && item.snippet.defaultLanguage != null ? item.snippet.defaultLanguage : null; return (0, _fp.mergeAll)([{ _sc_id: id, _sc_href: href, _sc_title: title, _sc_description: description, _sc_id_fields: ["_sc_id"], _sc_content_fields: ["_sc_title", "_sc_description"], _sc_pubdates: { source: (0, _parse.default)(createdAt) }, _sc_entity: "youtube_video", _sc_locations: locations, _sc_language: language, _sc_stats: stats, _sc_media: [{ type: "image", term: item.snippet.thumbnails.high.url, width: item.snippet.thumbnails.high.width, height: item.snippet.thumbnails.high.height }, { type: "video", term: `https://www.youtube.com/watch?v=${item.id}` }, { type: "url", term: `https://www.youtube.com/watch?v=${item.id}` }], _sc_data: item, _sc_tags: [], _sc_queries: [] }, channelId != null ? { _sc_channel_href: `https://youtube.com/channel/${channelId}`, _sc_channel_id: channelId, _sc_channel: channel } : {}]); }; exports.ncubeVideo = ncubeVideo; const video = item => { const decisions = (0, _core.createFeatureDecisions)(); if (decisions.canNcube()) return ncubeVideo(item); return deprecatedVideo(item); }; exports.video = video; const playlistVideo = item => video((0, _fp.merge)(item, { id: item.contentDetails.videoId, playlist_id: item.id })); exports.playlistVideo = playlistVideo; var _default = { video, playlistVideo }; exports.default = _default;