UNPKG

page-meta-scraper

Version:
3 lines (2 loc) 16 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=(t=require("cheerio"))&&"object"==typeof t&&"default"in t?t.default:t;function r(t,e,r,n,i,a,o){try{var u=t[a](o),c=u.value}catch(t){return void r(t)}u.done?e(c):Promise.resolve(c).then(n,i)}function n(t){return function(){var e=this,n=arguments;return new Promise((function(i,a){var o=t.apply(e,n);function u(t){r(o,i,a,u,c,"next",t)}function c(t){r(o,i,a,u,c,"throw",t)}u(void 0)}))}}function i(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function a(t,e){var r;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return i(t,void 0);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?i(t,void 0):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(r=t[Symbol.iterator]()).next.bind(r)}var o,u=(function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},i=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",o=n.toStringTag||"@@toStringTag";function u(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,r){return t[e]=r}}function c(t,e,r,n){var i=Object.create((e&&e.prototype instanceof f?e:f).prototype),a=new L(n||[]);return i._invoke=function(t,e,r){var n="suspendedStart";return function(i,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===i)throw a;return{value:void 0,done:!0}}for(r.method=i,r.arg=a;;){var o=r.delegate;if(o){var u=x(o,r);if(u){if(u===p)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=s(t,e,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===p)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}(t,r,a),i}function s(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var p={};function f(){}function h(){}function l(){}var g={};g[i]=function(){return this};var d=Object.getPrototypeOf,v=d&&d(d(E([])));v&&v!==e&&r.call(v,i)&&(g=v);var m=l.prototype=f.prototype=Object.create(g);function y(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){var n;this._invoke=function(i,a){function o(){return new e((function(n,o){!function n(i,a,o,u){var c=s(t[i],t,a);if("throw"!==c.type){var p=c.arg,f=p.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,o,u)}),(function(t){n("throw",t,o,u)})):e.resolve(f).then((function(t){p.value=t,o(p)}),(function(t){return n("throw",t,o,u)}))}u(c.arg)}(i,a,n,o)}))}return n=n?n.then(o,o):o()}}function x(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method))return p;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return p}var n=s(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,p;var i=n.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,p):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,p)}function b(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function k(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function E(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,a=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return a.next=a}}return{next:S}}function S(){return{value:void 0,done:!0}}return h.prototype=m.constructor=l,l.constructor=h,h.displayName=u(l,o,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,l):(t.__proto__=l,u(t,o,"GeneratorFunction")),t.prototype=Object.create(m),t},t.awrap=function(t){return{__await:t}},y(w.prototype),w.prototype[a]=function(){return this},t.AsyncIterator=w,t.async=function(e,r,n,i,a){void 0===a&&(a=Promise);var o=new w(c(e,r,n,i),a);return t.isGeneratorFunction(r)?o:o.next().then((function(t){return t.done?t.value:o.next()}))},y(m),u(m,o,"Generator"),m[i]=function(){return this},m.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=E,L.prototype={constructor:L,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(k),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return o.type="throw",o.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],o=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=r.call(a,"catchLoc"),c=r.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var i=this.tryEntries[n];if(i.tryLoc<=this.prev&&r.call(i,"finallyLoc")&&this.prev<i.finallyLoc){var a=i;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=e&&e<=a.finallyLoc&&(a=null);var o=a?a.completion:{};return o.type=t,o.arg=e,a?(this.method="next",this.next=a.finallyLoc,p):this.complete(o)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),p},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),k(r),p}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var i=n.arg;k(r)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:E(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),p}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}}(o={exports:{}}),o.exports),c={useFallbackImages:!1,imageFallbackStrategies:[],imageSearchBaseUrl:"https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/ImageSearchAPI",unsplashBaseUrl:"",unsplashClientId:"",xRapidapiHost:"",xRapidapiKey:"",debugMode:!1},s=function(){var t=n(u.mark((function t(e){var r,n,i,a,o;return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,r={pageNumber:1,pageSize:10,q:e.join(" "),autoCorrect:!0},n={"x-rapidapi-host":c.xRapidapiHost,"x-rapidapi-key":c.xRapidapiKey},i=new URL(c.imageSearchBaseUrl),Object.keys(r).forEach((function(t){return i.searchParams.append(t,r[t])})),t.next=7,fetch(i.toString(),{headers:n});case 7:return a=t.sent,t.next=10,a.json();case 10:return o=t.sent.value.map((function(t){return t.url})),t.abrupt("return",o);case 15:return t.prev=15,t.t0=t.catch(0),t.abrupt("return",[]);case 18:case"end":return t.stop()}}),t,null,[[0,15]])})));return function(e){return t.apply(this,arguments)}}(),p=function(){var t=n(u.mark((function t(e){var r,n,i,a,o;return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r={query:e.join(" ")},n={Authorization:"Client-ID "+c.unsplashClientId},i=new URL(c.unsplashBaseUrl),Object.keys(r).forEach((function(t){return i.searchParams.append(t,r[t])})),t.next=6,fetch(i.toString(),{headers:n});case 6:return a=t.sent,t.next=9,a.json();case 9:return o=t.sent.results.map((function(t){return t.urls.small})),t.abrupt("return",o);case 12:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),f=["ask","show","hn","the","a","that","this","as","and","or","for","in","out","new","i"],h=function(){function t(t,e,r){void 0===e&&(e=s),void 0===r&&(r=p),this.keywords=[],this.strategies={},this.MAX_IMAGES=10,this.keywords=this.stripKeywords(t),this.strategies={contextualweb:e,unsplash:r}}var e=t.prototype;return e.images=function(){var t=n(u.mark((function t(){var e,r,n,i;return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!c.debugMode){t.next=2;break}return t.abrupt("return",["https://www.tagesspiegel.de/images/grosser-panda-meng-meng_zoo-berlin/24195486/2-format43.jpg"]);case 2:e=[],r=a(c.imageFallbackStrategies);case 4:if((n=r()).done){t.next=19;break}if(i=this.strategies[n.value]){t.next=9;break}return t.abrupt("continue",17);case 9:return t.t0=e.push,t.t1=e,t.next=13,i(this.keywords);case 13:if(t.t2=t.sent,t.t0.apply.call(t.t0,t.t1,t.t2),!(e.length>=1)){t.next=17;break}return t.abrupt("return",e.slice(0,this.MAX_IMAGES));case 17:t.next=4;break;case 19:return t.abrupt("return",e.slice(0,this.MAX_IMAGES));case 20:case"end":return t.stop()}}),t,this)})));return function(){return t.apply(this,arguments)}}(),e.stripKeywords=function(t){return t.map((function(t){return t.replace(/[^A-Za-z\s]/g,"").replace(/\s{2,}/g," ")})).filter((function(t){return t.length>=4})).filter((function(t){return!f.includes(t.toLowerCase())}))},t}(),l=function(t,e){return t(e).attr("content")},g=function(t,e){var r=[];return t(e).each((function(e,n){return r.push(t(n).attr("src"))})),r},d=function(t,e){return t(e).text()},v=function(t,e){return t(e).attr("href")},m=function(t,e){var r=new RegExp("^(?:[a-z]+:)?//","i");if(e){var n=r.test(e),i=/^\//i.test(e);return n?e:i?""+new URL(t).origin+e:function(t){return t.replace(/[a-zA-Z]*\.html?$/g,"")}(t)+"/"+e}},y=function(){function t(t,e){this.MAX_IMAGES=10,this.strategies=[['meta[property="og:image:secure_url"]',l],['meta[property="og:image:url"]',l],['meta[property="og:image"]',l],['meta[name="twitter:image:src"]',l],['meta[name="twitter:image"]',l],['meta[itemprop="image"]',l],["article img[src]",g],["#content img[src]",g],['img[alt*="author" i]',g],['img[src]:not([aria-hidden="true"])',g]],this.page=t,this.url=e}return t.prototype.images=function(){var t=n(u.mark((function t(){var e,r=this;return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,e=this.strategies.map((function(t){return(0,t[1])(r.page,t[0])})).flat().map((function(t){return m(r.url,t)})).filter((function(t){return"string"==typeof t})).map((function(t){return String(t)})).slice(0,this.MAX_IMAGES),t.abrupt("return",e);case 5:t.prev=5,t.t0=t.catch(0);case 7:return t.abrupt("return",[]);case 8:case"end":return t.stop()}}),t,this,[[0,5]])})));return function(){return t.apply(this,arguments)}}(),t}(),w=function(){function t(t,e,r){this.MAX_LENGTH=5e3,this.strategies=[["meta[property='og:description']",l],["meta[property='twitter:description']",l],["meta[name='description']",l],["meta[itemprop='description']",l],["meta[property='og:description']",l],["p",d],["pre",d]],this.page=t,this.url=e,this.truncateLength=r||this.MAX_LENGTH}var e=t.prototype;return e.description=function(){var t=this;if(this.isAPdf())return"";var e=this.strategies.map((function(e){return(0,e[1])(t.page,e[0])})).map((function(t){return t&&t.replace(/(<([^>]+)>)/gi," ").replace(/&.*;/g," ")})).filter((function(t){return t})).map((function(t){return String(t)}));return e[0]&&e[0].slice(0,this.truncateLength)},e.isAPdf=function(){return this.url.endsWith(".pdf")},t}(),x=function(){function t(t){this.strategies=[["meta[property='og:title']",l],["title",d]],this.page=t}return t.prototype.title=function(){var t=this;return this.strategies.map((function(e){return(0,e[1])(t.page,e[0])})).filter((function(t){return t})).map((function(t){return String(t)}))[0]},t}(),b=function(){function t(t){this.strategies=[["link[rel='icon']",v]],this.page=t}return t.prototype.favicon=function(){var t=this;return this.strategies.map((function(e){return(0,e[1])(t.page,e[0])})).filter((function(t){return t})).map((function(t){return String(t)}))[0]},t}(),k=function(){function t(t,e,r){this.url=t,this.keywords=e,this.fetcher=r}var r=t.prototype;return r.loadPage=function(){var t=n(u.mark((function t(){var r;return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,this.fetcher(this.url);case 3:return r=t.sent,t.next=6,r.text();case 6:this.page=e.load(t.sent,{xmlMode:!0,normalizeWhitespace:!0,decodeEntities:!1}),t.next=12;break;case 10:t.prev=10,t.t0=t.catch(0);case 12:case"end":return t.stop()}}),t,this,[[0,10]])})));return function(){return t.apply(this,arguments)}}(),r.scrape=function(){var t=n(u.mark((function t(){var e;return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:e={};try{e.title=this.title(),e.description=this.description(),e.favicon=this.favicon()}catch(t){}return t.next=4,this.images();case 4:return e.imageUrls=t.sent,t.abrupt("return",e);case 6:case"end":return t.stop()}}),t,this)})));return function(){return t.apply(this,arguments)}}(),r.images=function(){var t=n(u.mark((function t(){var e;return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,new y(this.page,this.url).images();case 2:if(!((e=t.sent).length>0)){t.next=5;break}return t.abrupt("return",e);case 5:if(!c.useFallbackImages){t.next=9;break}return t.next=8,new h(this.keywords).images();case 8:return t.abrupt("return",t.sent);case 9:return t.abrupt("return",[]);case 10:case"end":return t.stop()}}),t,this)})));return function(){return t.apply(this,arguments)}}(),r.title=function(){return new x(this.page).title()},r.favicon=function(){return new b(this.page).favicon()},r.description=function(){return new w(this.page,this.url,300).description()},t}(),L=function(t){Object.assign(c,t)},E=function(){var t=n(u.mark((function t(e,r,n){var i;return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===r&&(r=[]),void 0===n&&(n=fetch),i=new k(e,r,n),t.next=5,i.loadPage();case 5:return t.next=7,i.scrape();case 7:return t.abrupt("return",t.sent);case 8:case"end":return t.stop()}}),t)})));return function(e,r,n){return t.apply(this,arguments)}}(),S=function(){var t=n(u.mark((function t(e){return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,new h(e).images();case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),A={configure:L,scrape:E,fallbackImages:S};exports.configure=L,exports.default=A,exports.fallbackImages=S,exports.scrape=E; //# sourceMappingURL=page-meta-scraper.cjs.production.min.js.map