urban-scraper
Version:
A package for scraping terms and their definitions from the [Urban Dictionary](https://www.urbandictionary.com/).
3 lines (2 loc) • 13.7 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var t=require("node-fetch");function r(t){var r=Object.create(null);return t&&Object.keys(t).forEach((function(e){if("default"!==e){var n=Object.getOwnPropertyDescriptor(t,e);Object.defineProperty(r,e,n.get?n:{enumerable:!0,get:function(){return t[e]}})}})),r.default=t,r}var e=r(require("cheerio"));function n(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=Array(r);e<r;e++)n[e]=t[e];return n}function o(t,r,e,n,o,a,i){try{var u=t[a](i),c=u.value}catch(t){return void e(t)}u.done?r(c):Promise.resolve(c).then(n,o)}function a(t){return function(){var r=this,e=arguments;return new Promise((function(n,a){var i=t.apply(r,e);function u(t){o(i,n,a,u,c,"next",t)}function c(t){o(i,n,a,u,c,"throw",t)}u(void 0)}))}}function i(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(e)return(e=e.call(t)).next.bind(e);if(Array.isArray(t)||(e=function(t,r){if(t){if("string"==typeof t)return n(t,r);var e={}.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?n(t,r):void 0}}(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var o=0;return function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function u(){return u=Object.assign?Object.assign.bind():function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)({}).hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t},u.apply(null,arguments)}function c(){c=function(){return r};var t,r={},e=Object.prototype,n=e.hasOwnProperty,o=Object.defineProperty||function(t,r,e){t[r]=e.value},a="function"==typeof Symbol?Symbol:{},i=a.iterator||"@@iterator",u=a.asyncIterator||"@@asyncIterator",s=a.toStringTag||"@@toStringTag";function f(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{f({},"")}catch(t){f=function(t,r,e){return t[r]=e}}function l(t,r,e,n){var a=Object.create((r&&r.prototype instanceof w?r:w).prototype),i=new M(n||[]);return o(a,"_invoke",{value:_(t,e,i)}),a}function p(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}r.wrap=l;var h="suspendedStart",d="suspendedYield",v="executing",y="completed",m={};function w(){}function b(){}function g(){}var x={};f(x,i,(function(){return this}));var k=Object.getPrototypeOf,E=k&&k(k(D([])));E&&E!==e&&n.call(E,i)&&(x=E);var O=g.prototype=w.prototype=Object.create(x);function j(t){["next","throw","return"].forEach((function(r){f(t,r,(function(t){return this._invoke(r,t)}))}))}function L(t,r){function e(o,a,i,u){var c=p(t[o],t,a);if("throw"!==c.type){var s=c.arg,f=s.value;return f&&"object"==typeof f&&n.call(f,"__await")?r.resolve(f.__await).then((function(t){e("next",t,i,u)}),(function(t){e("throw",t,i,u)})):r.resolve(f).then((function(t){s.value=t,i(s)}),(function(t){return e("throw",t,i,u)}))}u(c.arg)}var a;o(this,"_invoke",{value:function(t,n){function o(){return new r((function(r,o){e(t,n,r,o)}))}return a=a?a.then(o,o):o()}})}function _(r,e,n){var o=h;return function(a,i){if(o===v)throw Error("Generator is already running");if(o===y){if("throw"===a)throw i;return{value:t,done:!0}}for(n.method=a,n.arg=i;;){var u=n.delegate;if(u){var c=S(u,n);if(c){if(c===m)continue;return c}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===h)throw o=y,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=v;var s=p(r,e,n);if("normal"===s.type){if(o=n.done?y:d,s.arg===m)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(o=y,n.method="throw",n.arg=s.arg)}}}function S(r,e){var n=e.method,o=r.iterator[n];if(o===t)return e.delegate=null,"throw"===n&&r.iterator.return&&(e.method="return",e.arg=t,S(r,e),"throw"===e.method)||"return"!==n&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+n+"' method")),m;var a=p(o,r.iterator,e.arg);if("throw"===a.type)return e.method="throw",e.arg=a.arg,e.delegate=null,m;var i=a.arg;return i?i.done?(e[r.resultName]=i.value,e.next=r.nextLoc,"return"!==e.method&&(e.method="next",e.arg=t),e.delegate=null,m):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,m)}function P(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function A(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function M(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(P,this),this.reset(!0)}function D(r){if(r||""===r){var e=r[i];if(e)return e.call(r);if("function"==typeof r.next)return r;if(!isNaN(r.length)){var o=-1,a=function e(){for(;++o<r.length;)if(n.call(r,o))return e.value=r[o],e.done=!1,e;return e.value=t,e.done=!0,e};return a.next=a}}throw new TypeError(typeof r+" is not iterable")}return b.prototype=g,o(O,"constructor",{value:g,configurable:!0}),o(g,"constructor",{value:b,configurable:!0}),b.displayName=f(g,s,"GeneratorFunction"),r.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===b||"GeneratorFunction"===(r.displayName||r.name))},r.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,g):(t.__proto__=g,f(t,s,"GeneratorFunction")),t.prototype=Object.create(O),t},r.awrap=function(t){return{__await:t}},j(L.prototype),f(L.prototype,u,(function(){return this})),r.AsyncIterator=L,r.async=function(t,e,n,o,a){void 0===a&&(a=Promise);var i=new L(l(t,e,n,o),a);return r.isGeneratorFunction(e)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},j(O),f(O,s,"Generator"),f(O,i,(function(){return this})),f(O,"toString",(function(){return"[object Generator]"})),r.keys=function(t){var r=Object(t),e=[];for(var n in r)e.push(n);return e.reverse(),function t(){for(;e.length;){var n=e.pop();if(n in r)return t.value=n,t.done=!1,t}return t.done=!0,t}},r.values=D,M.prototype={constructor:M,reset:function(r){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(A),!r)for(var e in this)"t"===e.charAt(0)&&n.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(r){if(this.done)throw r;var e=this;function o(n,o){return u.type="throw",u.arg=r,e.next=n,o&&(e.method="next",e.arg=t),!!o}for(var a=this.tryEntries.length-1;a>=0;--a){var i=this.tryEntries[a],u=i.completion;if("root"===i.tryLoc)return o("end");if(i.tryLoc<=this.prev){var c=n.call(i,"catchLoc"),s=n.call(i,"finallyLoc");if(c&&s){if(this.prev<i.catchLoc)return o(i.catchLoc,!0);if(this.prev<i.finallyLoc)return o(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return o(i.catchLoc,!0)}else{if(!s)throw Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return o(i.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var o=this.tryEntries[e];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=r&&r<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=t,i.arg=r,a?(this.method="next",this.next=a.finallyLoc,m):this.complete(i)},complete:function(t,r){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&&r&&(this.next=r),m},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),A(e),m}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;A(e)}return o}}throw Error("illegal catch attempt")},delegateYield:function(r,e,n){return this.delegate={iterator:D(r),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=t),m}},r}function s(t,r){return s=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,r){return t.__proto__=r,t},s(t,r)}function f(){f=function(t,r){return new e(t,void 0,r)};var t=RegExp.prototype,r=new WeakMap;function e(t,n,o){var a=RegExp(t,n);return r.set(a,o||r.get(t)),s(a,e.prototype)}function n(t,e){var n=r.get(e);return Object.keys(n).reduce((function(r,e){var o=n[e];if("number"==typeof o)r[e]=t[o];else{for(var a=0;void 0===t[o[a]]&&a+1<o.length;)a++;r[e]=t[o[a]]}return r}),Object.create(null))}return function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),r&&s(t,r)}(e,RegExp),e.prototype.exec=function(r){var e=t.exec.call(this,r);if(e){e.groups=n(e,this);var o=e.indices;o&&(o.groups=n(o,this))}return e},e.prototype[Symbol.replace]=function(e,o){if("string"==typeof o){var a=r.get(this);return t[Symbol.replace].call(this,e,o.replace(/\$<([^>]+)>/g,(function(t,r){var e=a[r];return"$"+(Array.isArray(e)?e.join("$"):e)})))}if("function"==typeof o){var i=this;return t[Symbol.replace].call(this,e,(function(){var t=arguments;return"object"!=typeof t[t.length-1]&&(t=[].slice.call(t)).push(n(t,i)),o.apply(this,t)}))}return t[Symbol.replace].call(this,e,o)},f.apply(this,arguments)}var l="#ud-root > div > main > div > div > section > div:nth-child(1)",p=l+" > div > div > h1 > a",h=l+" > div > div.break-words.meaning.mb-4",d=l+" > div > div.break-words.example.italic.mb-4",v=l+" > div > div.contributor.font-bold > a",y=l+" > div > div.contributor.font-bold",m=l+" > a",w=f(/<a class="autolink" href="(\/define\.php\?term=[\w%\/.-]+)">([\w. '-]+)/gm,{url:1,text:2}),b="[$<text>](https://www.urbandictionary.com$<url>)",g=/<\/a>|\*/gm,x=/<br>/gm,k=f(/[\w:\/.?]+defid=([\d]+)/,{termId:1});function E(t,r){void 0===r&&(r=!1);var e=t.description,n=t.example;return t.found&&e&&n?u({},t,{description:e.replace(x,"\n").replace(w,r?b:"$<text>").replace(g,""),example:n.replace(x,"\n").replace(w,r?b:"$<text>").replace(g,"")}):t}function O(t,r){return j.apply(this,arguments)}function j(){return j=a(c().mark((function r(n,o){var i,u,s,f,l,w,b,g,x,E,O,j,L,_;return c().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return void 0===o&&(o=!1),i="https://www.urbandictionary.com/"+(o?"random.php":"define.php?term="+n),r.next=4,t(i,{redirect:"follow"}).then(function(){var t=a(c().mark((function t(r){return c().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r.text();case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}());case 4:if(u=e.load(r.sent),null==(s=u("#ud-root > div > main > div > div > section > div > div.font-bold.text-2xl.my-8").html())||!s.startsWith("Sorry, we couldn't find:")){r.next=9;break}return r.abrupt("return",{found:!1,term:n});case 9:return f=u(p).html(),l=u(h).html(),w=u(d).html(),b=u(v),g=b.html(),x="https://www.urbandictionary.com"+b[0].attribs.href,E=u(y),O=E.children()[0].next.data.trim(),j=new Date(Date.UTC(new Date(O).getFullYear(),new Date(O).getMonth(),new Date(O).getDate(),new Date(O).getHours(),new Date(O).getMinutes(),new Date(O).getSeconds(),new Date(O).getMilliseconds())),L=u(m),_=Number(L[0].attribs.href.replace(k,"$<termId>")),r.next=22,t("https://api.urbandictionary.com/v0/uncacheable?ids="+_).then(function(){var t=a(c().mark((function t(r){return c().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r.json();case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}()).then((function(t){return{up:t.thumbs[0].up,down:t.thumbs[0].down}}));case 22:return r.abrupt("return",{found:!0,term:f,id:_,url:"https://www.urbandictionary.com/define.php?term="+f+"&defid="+_,description:l,example:w,createdAt:j,author:{name:g,url:x},thumbs:r.sent});case 25:case"end":return r.stop()}}),r)}))),j.apply(this,arguments)}function L(){return(L=a(c().mark((function t(r,e){var n,o,a,u;return c().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(void 0===e&&(e={formatMarkdown:!1}),r&&0!==r.length){t.next=3;break}throw new Error("Provide a term to search for");case 3:if("boolean"==typeof e.formatMarkdown){t.next=5;break}throw new Error("Format markdown option needs to be a boolean");case 5:if(!Array.isArray(r)){t.next=17;break}n=[],o=i(r);case 8:if((a=o()).done){t.next=16;break}return u=a.value,t.next=12,O(u);case 12:n.push(E(t.sent,e.formatMarkdown));case 14:t.next=8;break;case 16:return t.abrupt("return",n);case 17:return t.next=19,O(r);case 19:return t.abrupt("return",E(t.sent,e.formatMarkdown));case 21:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _(){return(_=a(c().mark((function t(r){return c().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(void 0===r&&(r={formatMarkdown:!1}),"boolean"==typeof r.formatMarkdown){t.next=3;break}throw new Error("Format markdown option needs to be a boolean");case 3:return t.next=5,O("",!0);case 5:return t.abrupt("return",E(t.sent,r.formatMarkdown));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.getRandom=function(t){return _.apply(this,arguments)},exports.getTerm=function(t,r){return L.apply(this,arguments)};
//# sourceMappingURL=urban-scraper.cjs.production.min.js.map