@transloadit/enrich-tweet
Advanced tools
Comparing version 0.4.3 to 0.4.4
@@ -1,3 +0,3 @@ | ||
type Tweet = { | ||
full_text: string; | ||
interface Tweet { | ||
full_text?: string; | ||
entities?: { | ||
@@ -19,4 +19,4 @@ urls: { | ||
}; | ||
}; | ||
export default function enrichTweet(tweet: Tweet, unshorten?: boolean): Promise<string | undefined>; | ||
export {}; | ||
} | ||
declare const _default: (tweet: Tweet, unshorten?: boolean) => Promise<string | undefined>; | ||
export = _default; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const twitter_text_1 = __importDefault(require("twitter-text")); | ||
const tall_1 = require("tall"); | ||
const get_urls_1 = __importDefault(require("get-urls")); | ||
function tryUnshorten(url, unshorten) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!unshorten) | ||
return url; | ||
try { | ||
return yield (0, tall_1.tall)(url); | ||
} | ||
catch (err) { | ||
return url; | ||
} | ||
}); | ||
const twttr = require("twitter-text"); | ||
const tall = require("tall"); | ||
async function tryUnshorten(url, unshorten) { | ||
if (!unshorten) | ||
return url; | ||
try { | ||
return await tall.tall(url); | ||
} | ||
catch (err) { | ||
return url; | ||
} | ||
} | ||
function enrichTweet(tweet, unshorten = true) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!tweet) | ||
return; | ||
let text = tweet.full_text; | ||
if (tweet.entities && tweet.entities.urls.length) { | ||
const subUrls = tweet.entities.urls; | ||
for (const subUrl1 of subUrls) { | ||
const unshortened = yield tryUnshorten(subUrl1.expanded_url, unshorten); | ||
const friends1 = [subUrl1.display_url, subUrl1.url, subUrl1.expanded_url]; | ||
for (const friend1 of friends1) { | ||
text = text.replace(`http://www.${friend1}`, unshortened); | ||
text = text.replace(`https://www.${friend1}`, unshortened); | ||
text = text.replace(`http://${friend1}`, unshortened); | ||
text = text.replace(`https://${friend1}`, unshortened); | ||
text = text.replace(`${friend1}`, unshortened); | ||
} | ||
module.exports = async function enrichTweet(tweet, unshorten = true) { | ||
if (!tweet) | ||
return; | ||
let text = tweet.full_text ?? ''; | ||
if (tweet.entities && tweet.entities.urls.length) { | ||
const subUrls = tweet.entities.urls; | ||
for (const subUrl1 of subUrls) { | ||
const unshortened = await tryUnshorten(subUrl1.expanded_url, unshorten); | ||
const friends1 = [subUrl1.display_url, subUrl1.url, subUrl1.expanded_url]; | ||
for (const friend1 of friends1) { | ||
text = text.replace(`http://www.${friend1}`, unshortened); | ||
text = text.replace(`https://www.${friend1}`, unshortened); | ||
text = text.replace(`http://${friend1}`, unshortened); | ||
text = text.replace(`https://${friend1}`, unshortened); | ||
text = text.replace(`${friend1}`, unshortened); | ||
} | ||
} | ||
if (tweet.extended_entities && tweet.extended_entities.media.length) { | ||
for (const subUrl2 of tweet.extended_entities.media) { | ||
const friends2 = [subUrl2.display_url, subUrl2.url, subUrl2.media_url, subUrl2.expanded_url]; | ||
for (const friend2 of friends2) { | ||
text = text.replace(`http://${friend2}`, `${subUrl2.media_url_https}`); | ||
text = text.replace(`https://${friend2}`, `${subUrl2.media_url_https}`); | ||
text = text.replace(`${friend2}`, `${subUrl2.media_url_https}`); | ||
} | ||
} | ||
if (tweet.extended_entities && tweet.extended_entities.media.length) { | ||
for (const subUrl2 of tweet.extended_entities.media) { | ||
const friends2 = [subUrl2.display_url, subUrl2.url, subUrl2.media_url, subUrl2.expanded_url]; | ||
for (const friend2 of friends2) { | ||
text = text.replace(`http://${friend2}`, `${subUrl2.media_url_https}`); | ||
text = text.replace(`https://${friend2}`, `${subUrl2.media_url_https}`); | ||
text = text.replace(`${friend2}`, `${subUrl2.media_url_https}`); | ||
} | ||
} | ||
const urls = (0, get_urls_1.default)(text); | ||
for (const subUrl3 of urls) { | ||
if (!subUrl3.match(/^https?:\/\/bit\.ly/)) { | ||
continue; | ||
} | ||
const unshortened3 = yield tryUnshorten(subUrl3, unshorten); | ||
text = text.replace(`${subUrl3}`, `${unshortened3}`); | ||
} | ||
const getUrls = (await import('get-urls')).default; | ||
const urls = getUrls(text); | ||
for (const subUrl3 of urls) { | ||
if (!subUrl3.match(/^https?:\/\/bit\.ly/)) { | ||
continue; | ||
} | ||
text = twitter_text_1.default.autoLink(text); | ||
text = text.replace(/>(https:\/\/pbs\.twimg\.com\/[^<]+)</g, `><img class="tweet-media" src="$1" /><`); | ||
text = text.replace(`<a href="https://pbs.twimg.com/`, `\n\n<a href="https://pbs.twimg.com/`); | ||
text = text.replace(/@<a\s+class="tweet-url username"\s+href="https:\/\/twitter.com\/([^"]+)"\s+data-screen-name="([^"]+)"\s+rel="nofollow">([^<]+)<\/a>/g, '<a class="tweet-url username" href="https://twitter.com/$1" data-screen-name="$2" rel="nofollow">@$3</a>'); | ||
return text; | ||
}); | ||
} | ||
exports.default = enrichTweet; | ||
const unshortened3 = await tryUnshorten(subUrl3, unshorten); | ||
text = text.replace(`${subUrl3}`, `${unshortened3}`); | ||
} | ||
text = twttr.autoLink(text); | ||
text = text.replace(/>(https:\/\/pbs\.twimg\.com\/[^<]+)</g, `><img class="tweet-media" src="$1" /><`); | ||
text = text.replace(`<a href="https://pbs.twimg.com/`, `\n\n<a href="https://pbs.twimg.com/`); | ||
text = text.replace(/@<a\s+class="tweet-url username"\s+href="https:\/\/twitter.com\/([^"]+)"\s+data-screen-name="([^"]+)"\s+rel="nofollow">([^<]+)<\/a>/g, '<a class="tweet-url username" href="https://twitter.com/$1" data-screen-name="$2" rel="nofollow">@$3</a>'); | ||
return text; | ||
}; | ||
//# sourceMappingURL=enrichTweet.js.map |
{ | ||
"name": "@transloadit/enrich-tweet", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"repository": { | ||
@@ -32,3 +32,3 @@ "type": "git", | ||
}, | ||
"gitHead": "15e11163ea5c1fd4886d0175d9bfdfb79db573f3" | ||
"gitHead": "2d38d089c0665440abd2af7a5f779deb1389b191" | ||
} |
import twttr = require('twitter-text') | ||
import tall = require('tall') | ||
import getUrls = require('get-urls') | ||
async function tryUnshorten(url: string, unshorten: boolean): Promise<string> { | ||
@@ -67,2 +65,3 @@ if (!unshorten) return url | ||
const getUrls = (await import('get-urls')).default | ||
const urls = getUrls(text) | ||
@@ -69,0 +68,0 @@ for (const subUrl3 of urls) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
131098
22
1601