UNPKG

ts-open-graph-scraper

Version:

Node.js scraper module for Open Graph and Twitter Card info, based on https://github.com/jshemas/openGraphScraper

163 lines (162 loc) 7.29 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var lodash_1 = __importDefault(require("lodash")); var fields_1 = __importDefault(require("./fields")); function mediaSetup(ogObjectRaw, options) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v; if (ogObjectRaw.ogImage || ogObjectRaw.ogImageWidth || ogObjectRaw.twitterImageHeight || ogObjectRaw.ogImageType) { ogObjectRaw.ogImage = (_a = ogObjectRaw.ogImage, (_a !== null && _a !== void 0 ? _a : [])); ogObjectRaw.ogImageWidth = (_b = ogObjectRaw.ogImageWidth, (_b !== null && _b !== void 0 ? _b : [])); ogObjectRaw.ogImageHeight = (_c = ogObjectRaw.ogImageHeight, (_c !== null && _c !== void 0 ? _c : [])); ogObjectRaw.ogImageType = (_d = ogObjectRaw.ogImageType, (_d !== null && _d !== void 0 ? _d : [])); } var ogImages = lodash_1.default.zip(ogObjectRaw.ogImage, ogObjectRaw.ogImageWidth, ogObjectRaw.ogImageHeight, ogObjectRaw.ogImageType) .map(mediaMapper).sort(mediaSorter); if (ogObjectRaw.ogVideo || ogObjectRaw.ogVideoWidth || ogObjectRaw.ogVideoHeight || ogObjectRaw.ogVideoType) { ogObjectRaw.ogVideo = (_e = ogObjectRaw.ogVideo, (_e !== null && _e !== void 0 ? _e : [])); ogObjectRaw.ogVideoWidth = (_f = ogObjectRaw.ogVideoWidth, (_f !== null && _f !== void 0 ? _f : [])); ogObjectRaw.ogVideoHeight = (_g = ogObjectRaw.ogVideoHeight, (_g !== null && _g !== void 0 ? _g : [])); ogObjectRaw.ogVideoType = (_h = ogObjectRaw.ogVideoType, (_h !== null && _h !== void 0 ? _h : [])); } var ogVideos = lodash_1.default.zip(ogObjectRaw.ogVideo, ogObjectRaw.ogVideoWidth, ogObjectRaw.ogVideoHeight, ogObjectRaw.ogVideoType) .map(mediaMapper).sort(mediaSorter); if (ogObjectRaw.twitterImageSrc || ogObjectRaw.twitterImage || ogObjectRaw.twitterImageWidth || ogObjectRaw.twitterImageHeight || ogObjectRaw.twitterImageAlt) { ogObjectRaw.twitterImage = (_k = (_j = ogObjectRaw.twitterImage, (_j !== null && _j !== void 0 ? _j : ogObjectRaw.twitterImageSrc)), (_k !== null && _k !== void 0 ? _k : [])); ogObjectRaw.twitterImageWidth = (_l = ogObjectRaw.twitterImageWidth, (_l !== null && _l !== void 0 ? _l : [])); ogObjectRaw.twitterImageHeight = (_m = ogObjectRaw.twitterImageHeight, (_m !== null && _m !== void 0 ? _m : [])); ogObjectRaw.twitterImageAlt = (_o = ogObjectRaw.twitterImageAlt, (_o !== null && _o !== void 0 ? _o : [])); } var twitterImages = lodash_1.default.zip(ogObjectRaw.twitterImage, ogObjectRaw.twitterImageWidth, ogObjectRaw.twitterImageHeight, ogObjectRaw.twitterImageAlt) .map(mediaMapperTwitterImage).sort(mediaSorter); if (ogObjectRaw.twitterPlayer || ogObjectRaw.twitterPlayerWidth || ogObjectRaw.twitterPlayerHeight || ogObjectRaw.twitterPlayerStream) { ogObjectRaw.twitterPlayer = (_p = ogObjectRaw.twitterPlayer, (_p !== null && _p !== void 0 ? _p : [])); ogObjectRaw.twitterPlayerWidth = (_q = ogObjectRaw.twitterPlayerWidth, (_q !== null && _q !== void 0 ? _q : [])); ogObjectRaw.twitterPlayerHeight = (_r = ogObjectRaw.twitterPlayerHeight, (_r !== null && _r !== void 0 ? _r : [])); ogObjectRaw.twitterPlayerStream = (_s = ogObjectRaw.twitterPlayerStream, (_s !== null && _s !== void 0 ? _s : [])); } var twitterPlayers = lodash_1.default.zip(ogObjectRaw.twitterPlayer, ogObjectRaw.twitterPlayerWidth, ogObjectRaw.twitterPlayerHeight, ogObjectRaw.twitterPlayerStream) .map(mediaMapperTwitterPlayer).sort(mediaSorter); if (ogObjectRaw.musicSong || ogObjectRaw.musicSongTrack || ogObjectRaw.musicSongDisc) { ogObjectRaw.musicSong = (_t = ogObjectRaw.musicSong, (_t !== null && _t !== void 0 ? _t : [])); ogObjectRaw.musicSongTrack = (_u = ogObjectRaw.musicSongTrack, (_u !== null && _u !== void 0 ? _u : [])); ogObjectRaw.musicSongDisc = (_v = ogObjectRaw.musicSongDisc, (_v !== null && _v !== void 0 ? _v : [])); } var musicSongs = lodash_1.default.zip(ogObjectRaw.musicSong, ogObjectRaw.musicSongTrack, ogObjectRaw.musicSongDisc) .map(mediaMapperMusicSong).sort(mediaSorterMusicSong); var ogObject = fields_1.default.reduce(function (acc, item) { var _a; if ((item.multiple && (item.fieldName.startsWith('ogImage') || item.fieldName.startsWith('ogVideo') || item.fieldName.startsWith('twitter') || item.fieldName.startsWith('musicSong')))) { return acc; } else { return __assign(__assign({}, acc), (_a = {}, _a[item.fieldName] = ogObjectRaw[item.fieldName], _a)); } }, {}); if (ogImages.length) { ogObject.ogImage = ogImages; } if (ogVideos.length) { ogObject.ogVideo = ogVideos; } if (twitterImages.length) { ogObject.twitterImage = twitterImages; } if (twitterPlayers.length) { ogObject.twitterPlayer = twitterPlayers; } if (musicSongs.length) { ogObject.musicSong = musicSongs; } return ogObject; } exports.mediaSetup = mediaSetup; function mediaMapperTwitterImage(item) { return { url: item[0], width: Number(item[1]), height: Number(item[2]), alt: item[3], }; } function mediaMapperTwitterPlayer(item) { return { url: item[0], width: Number(item[1]), height: Number(item[2]), stream: item[3], }; } function mediaMapperMusicSong(item) { return { url: item[0], track: Number(item[1]), disc: Number(item[2]), }; } function mediaMapper(item) { var out = { url: item[0], width: Number(item[1]), height: Number(item[2]), type: item[3], }; if (out.width === null || isNaN(out.width)) { delete out.width; } if (out.height === null || isNaN(out.height)) { delete out.height; } return out; } function mediaSorter(a, b) { if (!(a.url && b.url)) { return 0; } var aRes = a.url.match(/\.(\w{2,5})$/); var aExt = (aRes && aRes[1].toLowerCase()) || null; var bRes = b.url.match(/\.(\w{2,5})$/); var bExt = (bRes && bRes[1].toLowerCase()) || null; if (aExt === 'gif' && bExt !== 'gif') { return -1; } else if (aExt !== 'gif' && bExt === 'gif') { return 1; } return Math.max(b.width, b.height) - Math.max(a.width, a.height); } function mediaSorterMusicSong(a, b) { if (!(a.track && b.track)) { return 0; } else if (a.disc > b.disc) { return 1; } else if (a.disc < b.disc) { return -1; } return a.track - b.track; }