UNPKG

@sugarcube/plugin-twitter

Version:
178 lines (162 loc) 6.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.friendsTransform = exports.followersTransform = exports.searchTransform = exports.tweetNcube = exports.tweetLegacy = void 0; var _fp = require("lodash/fp"); var _core = require("@sugarcube/core"); var _utils = require("./utils"); const tweetFields = ["retweet_count", "favorite_count", "lang", "favorited", "retweeted", "tweet_id", "tweet", "tweet_time", "geo", "coordinates", "place", "href"]; const userFields = ["name", "screen_name", "location", "description", "url", "followers_count", "friends_count", "list_count", "favourites_count", "utc_offset", "timezone", "geo_enabled", "verified", "statuses_count", "lang", "profile_image_url_https", "user_id", "user_created_at"]; const mediaEntities = (0, _fp.map)(media => (0, _fp.merge)({}, { id: media.id_str, type: media.type === "photo" ? "image" : media.type, term: media.type === "photo" ? media.media_url_https : media.expanded_url })); const urlEntities = (0, _fp.curry)((type, es) => (0, _fp.map)(url => (0, _fp.merge)({}, { type, term: url.expanded_url }), es)); const hashtagEntities = (0, _fp.map)(tag => (0, _fp.merge)({}, { tag: `#${(0, _fp.toLower)(tag.text)}`, original_tag: tag.text })); const mentionEntities = (0, _fp.map)(mention => (0, _fp.merge)({}, { mention: mention.screen_name, name: mention.name, id: mention.id_str })); const coordinatesEntities = ({ coordinates }) => { if (coordinates == null) return []; return [{ location: { lon: coordinates[0], lat: coordinates[1] }, type: "tweet_location", term: coordinates }]; }; const pubDates = unit => { const createdAt = (0, _utils.twitterDate)(unit.created_at); return createdAt.isValid() ? { source: createdAt.toDate() } : {}; }; const userEntity = (0, _fp.flow)([u => { const createdAt = (0, _utils.twitterDate)(u.created_at); return (0, _fp.merge)(u, { user_id: u.id_str, user_created_at: createdAt.isValid() ? createdAt.toDate() : null }); }, (0, _fp.pick)(userFields)]); const tweetEntity = (0, _fp.flow)([t => { const createdAt = (0, _utils.twitterDate)(t.created_at); return (0, _fp.merge)(t, { tweet_id: t.id_str, tweet: t.text, tweet_time: createdAt.isValid() ? createdAt.toDate() : null, href: `https://twitter.com/statuses/${t.id_str}` }); }, (0, _fp.pick)(tweetFields)]); const mentionsToRelations = (0, _fp.map)(m => (0, _fp.merge)({}, { type: "twitter_mention", term: m.mention })); const hashtagsToRelations = (0, _fp.map)(h => (0, _fp.merge)({}, { type: "hashtag", term: h.tag })); const linksToRelations = (0, _fp.map)(l => (0, _fp.merge)({}, { type: "url", term: l.term })); const tweetLegacy = t => { const lfUrls = (0, _fp.flow)([(0, _fp.getOr)([], "entities.urls"), urlEntities("url")])(t); const lfMedia = (0, _fp.flow)([(0, _fp.getOr)([], "extended_entities.media"), mediaEntities])(t); const lfHashtags = (0, _fp.flow)([(0, _fp.getOr)([], "entities.hashtags"), hashtagEntities])(t); const lfMentions = (0, _fp.flow)([(0, _fp.getOr)([], "entities.user_mentions"), mentionEntities])(t); const lfLocations = coordinatesEntities(t.coordinates || {}); const language = t.lang != null ? t.lang : null; return (0, _fp.merge)({ _sc_id_fields: ["tweet_id"], _sc_content_fields: ["tweet"], _sc_pubdates: pubDates(t), _sc_relations: (0, _fp.flatten)([mentionsToRelations(lfMentions), hashtagsToRelations(lfHashtags), lfUrls, lfMedia]), _sc_locations: lfLocations, _sc_media: (0, _fp.flatten)([lfMedia, lfUrls]), _sc_language: language, user: userEntity(t.user), urls: (0, _fp.getOr)([], "entities.url", t), medias: (0, _fp.getOr)([], "extended_entities.media", t), hashtags: lfHashtags, mentions: lfMentions }, tweetEntity(t)); }; exports.tweetLegacy = tweetLegacy; const tweetNcube = t => { const lfUrls = (0, _fp.flow)([(0, _fp.getOr)([], "entities.urls"), urlEntities("url")])(t); const lfMedia = (0, _fp.flow)([(0, _fp.getOr)([], "extended_entities.media"), mediaEntities])(t); const authorUrl = `https://twitter.com/${t.user.screen_name}`; const lfLocations = coordinatesEntities(t.coordinates || {}); const language = t.lang != null ? t.lang : null; return { _sc_id_fields: ["_sc_id"], _sc_content_fields: ["tweet"], _sc_id: t.id_str, _sc_href: `${authorUrl}/status/${t.id_str}`, _sc_title: t.text, _sc_author: t.user.screen_name, _sc_author_id: t.user.id_str, _sc_author_url: authorUrl, _sc_pubdates: pubDates(t), _sc_locations: lfLocations, _sc_media: (0, _fp.flatten)([lfMedia, lfUrls]), _sc_language: language, _sc_data: t, _sc_queries: [], _sc_tags: [] }; }; exports.tweetNcube = tweetNcube; const tweet = t => { const decisions = (0, _core.createFeatureDecisions)(); if (decisions.canNcube()) return tweetNcube(t); return tweetLegacy(t); }; const user = (0, _fp.curry)((source, u) => { const urls = (0, _fp.flatten)([(0, _fp.getOr)([], "entities.url.urls", u), (0, _fp.getOr)([], "entities.description.urls", u), (0, _fp.getOr)([], "status.entities.urls", u)]); const lfUrls = (0, _fp.map)(l => ({ type: "url", term: l.expanded_url }), urls); const lfImages = (0, _fp.flow)([(0, _fp.getOr)([], "extended_entities.media"), mediaEntities, (0, _fp.concat)([{ type: "image", term: u.profile_image_url_https }])])(u); const lfHashtags = (0, _fp.flow)([(0, _fp.getOr)([], "status.entities.hashtags"), hashtagEntities])(u); const lfMentions = (0, _fp.flow)([(0, _fp.getOr)([], "status.entities.user_mentions"), mentionEntities])(u); return (0, _fp.merge)({ _sc_id_fields: ["user_id"], _sc_pubdates: pubDates(u), _sc_relations: (0, _fp.flatten)([hashtagsToRelations(lfHashtags), mentionsToRelations(lfMentions), linksToRelations(lfUrls), linksToRelations(lfImages)]), _sc_media: (0, _fp.flatten)([lfImages, lfUrls]), // TODO: This is broken, where does _sc_graph* from from? // _sc_graph: {from: u._sc_graph_from, depth: u._sc_graph_depth}, urls, medias: lfImages, hashtags: lfHashtags, mentions: lfMentions }, userEntity(u)); }); const searchResult = t => (0, _fp.merge)(tweet(t), { _sc_source: "twitter_search" }); const searchTransform = (0, _fp.map)(searchResult); exports.searchTransform = searchTransform; const followersTransform = (0, _fp.map)(user("followers")); exports.followersTransform = followersTransform; const friendsTransform = (0, _fp.map)(user("friends")); exports.friendsTransform = friendsTransform;