UNPKG

@sugarcube/plugin-twitter

Version:
131 lines (110 loc) 3.48 kB
"use strict"; 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 _twitter = require("../twitter"); var _utils2 = require("../utils"); var _assertions = require("../assertions"); const getAll = (fields, obj) => { let result; for (const field of fields) { if (result == null) result = (0, _fp.get)(field, obj); } return result; }; const plugin = async (envelope, { log, cfg, stats }) => { const logCounter = (0, _utils.counter)(envelope.data.length, ({ cnt, total, percent }) => log.debug(`Progress: ${cnt}/${total} units (${percent}%).`), { threshold: 500, steps: 500 }); log.info(`Checking ${envelope.data.length} tweets.`); const twitterTweets = await (0, _dashp.flatmapP)(async units => { stats.count("total", units.length); const ids = (0, _fp.flow)([(0, _fp.reduce)((memo, unit) => { const id = getAll(["_sc_id", "tweet_id"], unit); if (id == null) { stats.fail({ type: "twitter_tweet", term: unit._sc_id_hash, reason: `Twitter id is invalid (_sc_id: ${(0, _fp.get)("_sc_id", unit)}, tweet_id: ${(0, _fp.get)("tweet_id", unit)})` }); return memo; } try { return memo.concat((0, _utils2.parseTweetId)(id)); } catch (e) { log.error(`Failed to parse twitter id of unit ${unit._sc_id_hash}.`); stats.fail({ type: "twitter_tweet", term: unit._sc_id_hash, reason: e.message }); return memo; } }, []), _fp.compact])(units); const missing = []; try { const { id: response } = await (0, _twitter.tweets)(cfg, ids); if (response == null) { ids.forEach(id => { stats.fail({ type: "twitter_tweet", term: id, reason: "No tweets fetched." }); }); return []; } Object.keys(response).forEach(id => { logCounter(); if (response[id] == null) { const unit = units.find(({ tweet_id: tweetId }) => tweetId === id); stats.fail({ type: "twitter_tweet", term: id, reason: "Tweet does not exist." }); missing.push(unit); } else { stats.count("success"); } }); } catch (e) { const reason = (0, _twitter.parseApiErrors)(e); ids.forEach(id => stats.fail({ type: "twitter_tweet", term: id, reason })); } return missing; }, (0, _fp.chunk)(50, envelope.data.filter(({ _sc_source: source }) => source === "twitter_tweets" || source === "twitter_feed"))); stats.count("existing", envelope.data.length - twitterTweets.length); stats.count("missing", twitterTweets.length); log.info(`Fetch details for ${envelope.data.length} Tweets: ${twitterTweets.length} failed.`); return _core.envelope.envelopeData(twitterTweets); }; const twitterFilterFailingPlugin = _core.plugin.liftManyA2([_assertions.assertCredentials, plugin]); twitterFilterFailingPlugin.desc = "Check failing tweets and remove existing tweets from the pipeline."; twitterFilterFailingPlugin.argv = {}; var _default = twitterFilterFailingPlugin; exports.default = _default;