@sugarcube/plugin-youtube
Version:
A SugarCube plugin to fetch videos from Youtube.
157 lines (132 loc) • 4.73 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 _parse = _interopRequireDefault(require("date-fns/parse"));
var _format = _interopRequireDefault(require("date-fns/format"));
var _sub_days = _interopRequireDefault(require("date-fns/sub_days"));
var _utils2 = require("../utils");
var _api = require("../api");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const querySource = "youtube_channel";
const listChannel = async (envelope, {
cfg,
log,
stats
}) => {
const key = (0, _fp.get)("youtube.api_key", cfg);
const publishedBefore = (0, _fp.get)("youtube.published_before", cfg);
const publishedAfter = (0, _fp.get)("youtube.published_after", cfg);
const pastDays = (0, _fp.get)("youtube.past_days", cfg);
const queries = _core.envelope.queriesByType(querySource, envelope).map(term => (0, _utils2.parseYoutubeChannel)(term));
let range;
if (publishedBefore != null || publishedAfter != null || pastDays != null) {
const till = publishedBefore == null ? new Date() : (0, _parse.default)(publishedBefore);
let from; // pastDays takes precedence over publishedAfter
if (pastDays != null) {
from = (0, _sub_days.default)(till, pastDays);
} else if (publishedAfter != null) {
from = (0, _parse.default)(publishedAfter);
}
if (from >= till) {
log.warn(`published_before is before published_after. Did you mean to switch them around?`);
} else {
range = {
publishedBefore: (0, _format.default)(till),
...(from ? {
publishedAfter: (0, _format.default)(from)
} : {})
};
}
}
if (range) log.info(`Limiting queries from ${range.publishedAfter} till ${range.publishedBefore}`);
const logCounter = (0, _utils.counter)(envelope.data.length, ({
cnt,
total,
percent
}) => log.debug(`Progress: ${cnt}/${total} units (${percent}%).`), {
threshold: 50,
steps: 25
});
const op = range ? (0, _api.videoChannel)(key, (0, _fp.pickBy)(_fp.identity, range)) : (0, _api.videoChannelPlaylist)(key);
const retrieveChannel = query => (0, _dashp.flowP)([(0, _dashp.tapP)(() => stats.count("total")), _utils2.parseYoutubeChannel, async q => {
let exists;
try {
exists = await (0, _api.channelExists)(key, q);
} catch (e) {
stats.fail({
type: querySource,
term: q,
reason: e.message
});
return [];
}
if (!exists) stats.fail({
type: querySource,
term: q,
reason: "Doesn't exist."
});
return exists ? (0, _dashp.flowP)([op, results => {
const sourceQuery = envelope.queries.find(({
type,
term
}) => // The deprecated video data format uses r.id, the new Ncube
// based data format uses r._sc_id.
type === querySource && (0, _utils2.parseYoutubeChannel)(term) === q);
if (sourceQuery == null) return results;
const {
tags,
...rest
} = sourceQuery;
return results.map(r => Object.assign(r, {
_sc_queries: Array.isArray(r._sc_queries) ? r._sc_queries.concat(rest) : [rest]
}, Array.isArray(tags) && tags.length > 0 ? {
_sc_tags: Array.isArray(r._sc_tags) ? r._sc_tags.concat(tags) : tags
} : undefined));
}, (0, _dashp.caughtP)(e => {
stats.fail({
type: querySource,
term: q,
reason: e.message
});
return [];
}), (0, _dashp.tapP)(ds => {
const total = (0, _fp.size)(ds);
log.info(`Received ${total} videos for ${query}.`);
stats.count("success");
stats.count("fetched", total);
})])(q) : [];
}, (0, _dashp.tapP)(() => logCounter())], query);
const videos = await (0, _dashp.flatmapP)(retrieveChannel, queries);
return _core.envelope.concatData(videos, envelope);
};
const plugin = _core.plugin.liftManyA2([_utils2.assertCredentials, listChannel]);
plugin.desc = "List all videos in a youtube channel.";
plugin.source = {
name: querySource,
desc: "A Youtube channel ID."
};
plugin.argv = {
"youtube.published_after": {
type: "string",
nargs: 1,
desc: "only fetch videos published after a certain date. yyyy-mm-dd"
},
"youtube.published_before": {
type: "string",
nargs: 1,
desc: "only fetch videos published before a certain date. yyyy-mm-ss"
},
"youtube.past_days": {
type: "string",
nargs: 1,
desc: "only fetch videos published in the past x days"
}
};
var _default = plugin;
exports.default = _default;