@sugarcube/plugin-youtube
Version:
A SugarCube plugin to fetch videos from Youtube.
97 lines (82 loc) • 2.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _fp = require("lodash/fp");
var _dashp = require("dashp");
var _core = require("@sugarcube/core");
var _utils = require("@sugarcube/utils");
var _api = require("../api");
var _utils2 = require("../utils");
const checkAndFilterVideos = async (envelope, {
cfg,
log,
stats
}) => {
const key = (0, _fp.get)("youtube.api_key", cfg);
const logCounter = (0, _utils.counter)(envelope.data.length, ({
cnt,
total,
percent
}) => log.debug(`Progress: ${cnt}/${total} units (${percent}%).`), {
threshold: 50,
steps: 25
});
log.info(`Checking ${envelope.data.length} videos.`);
const videos = await (0, _dashp.flatmapP)((0, _dashp.flowP)([async units => {
stats.count("total", units.length);
const ids = units.reduce((memo, unit) => memo.concat(unit._sc_media.filter(({
type
}) => type === "video").map(({
term
}) => (0, _utils2.parseYoutubeVideo)(term))), []);
const missing = [];
const errors = [];
let results = [];
let isError = false;
let failMsg = "Doesn't exist.";
try {
results = await (0, _api.videosListCheck)(key, ids);
} catch (e) {
isError = true;
failMsg = e.message;
}
if (results.length !== units.length) {
units.forEach(unit => {
unit._sc_media.filter(({
type
}) => type === "video").forEach(({
term
}) => {
const i = (0, _utils2.parseYoutubeVideo)(term);
if (results.find(({
id
}) => id === i) == null) {
if (isError) {
errors.push(unit);
} else {
missing.push(unit);
}
stats.fail({
type: "youtube_video",
reason: failMsg,
term
});
}
});
});
units.forEach(logCounter);
}
stats.count("existing", units.length - missing.length - errors.length);
stats.count("missing", missing.length);
log.info(`Fetch details for ${units.length} videos: ${missing.length} failed.`);
return missing;
}, (0, _dashp.delayP)(500)]), (0, _fp.chunk)(50, envelope.data));
return _core.envelope.envelopeData(videos);
};
const plugin = _core.plugin.liftManyA2([_utils2.assertCredentials, checkAndFilterVideos]);
plugin.desc = "Check failing videos and remove existing videos from the pipeline.";
plugin.argv = {};
var _default = plugin;
exports.default = _default;