archive-search
Version:
Small, zero dependency, Promise based library to search through archive.org's api for the browser and node.
1 lines • 5.19 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("archiveSearch",[],t):"object"==typeof exports?exports.archiveSearch=t():e.archiveSearch=t()}("undefined"!=typeof self?self:this,(()=>{return e={328:e=>{e.exports={isString:function(e){return"[object String]"===Object.prototype.toString.call(e)},isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)},isObject:function(e){return"[object Object]"===Object.prototype.toString.call(e)}}},371:e=>{e.exports="&fl%5B%5D=year&fl%5B%5D=title&fl%5B%5D=mediatype&fl%5B%5D=language&fl%5B%5D=date&fl%5B%5D=downloads&fl%5B%5D=creator&&fl%5B%5D=identifier&sort%5B%5D=year+asc&sort%5B%5D=downloads+asc&rows=150&page=1&output=json"},972:(e,t,r)=>{function o(e){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},o(e)}function n(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,o)}return r}function c(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?n(Object(r),!0).forEach((function(t){a(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):n(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function a(e,t,r){return(t=s(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function i(e,t){for(var r=0;r<t.length;r++){var o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,s(o.key),o)}}function s(e){var t=function(e,t){if("object"!=o(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=o(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==o(t)?t:t+""}var u=r(328),f=r(371),l=function(){return e=function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.searchBaseUrl="https://archive.org/advancedsearch.php",this.searchDefaults=f,this.searchBy="creator",this.metaBaseUrl="https://archive.org/metadata/"},t=[{key:"setOptions",value:function(e){var t=this;if(!u.isObject(e))throw new TypeError("setOptions() expected an Object");var r=["fields","max","sortBy","searchBy"];if(Object.keys(e).forEach((function(e){if(-1===r.indexOf(e))throw new Error("Invalid syntax for options check field: ".concat(e))})),!u.isArray(e.fields))throw new Error("Invalid syntax for options");this.searchDefaults=encodeURI("&fl[]=identifier");var o=e.fields,n=e.sortBy,c=e.max,a=e.searchBy;o.forEach((function(e){t.searchDefaults+=encodeURI("&fl[]=".concat(e))})),n&&(this.searchDefaults+=this.constructor.setSortBy(n)),c&&(this.searchDefaults+="&rows=".concat(c,"&page=1")),a&&(this.searchBy=a),this.searchDefaults+="&output=json"}},{key:"constructSearchUrl",value:function(e,t){return"".concat(this.searchBaseUrl,"?q=").concat(e,"%3A").concat(t).concat(this.searchDefaults)}},{key:"search",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if(!u.isString(e))throw new TypeError("search() expected a string");0!==t.length&&this.setOptions(t);var r=e.replace(/\s+/g,"+").toLowerCase(),o=this.constructSearchUrl(this.searchBy,r);return console.log("constructUrlFromParams",o),this.constructor.makeSearch(o)}},{key:"metaSearch",value:function(e){var t=this,r="".concat(this.metaBaseUrl).concat(e);return new Promise((function(e){t.constructor.jsonp(r,(function(t){var r=t.files,o=t.server,n=t.dir,a={metadata:t.metadata,reviews:t.reviews,files:[]};Object.keys(r).forEach((function(e){var t=r[e],i=t.format,s=t.name;"Metadata"!==i&&"JSON"!==i&&a.files.push(c(c({},r[e]),{},{name:s,format:i,link:"https://".concat(o).concat(n,"/").concat(s.replace(/ /g,"%20"))}))})),e(a)}))}))}}],r=[{key:"setSortBy",value:function(e){var t="";return Object.keys(e).forEach((function(r){t+="&sort[]=".concat(r,"+").concat(e[r])})),encodeURI(t)}},{key:"jsonp",value:function(e,t){var r="jsonp_callback_".concat(Math.round(15e4*Math.random()));window[r]=function(e){t(e),delete window[r],document.body.removeChild(o)};var o=document.createElement("script");o.src="".concat(e+(e.indexOf("?")>=0?"&":"?"),"callback=").concat(r),o.onerror=function(){throw new Error("Bad request, check query params")},document.body.appendChild(o)}},{key:"makeSearch",value:function(e){var t=this;return new Promise((function(r){t.jsonp(e,(function(e){var t=e.response,o=t.numFound,n=t.docs;if(0===o)throw new Error("no results, please update query params");r(n)}))}))}}],t&&i(e.prototype,t),r&&i(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,t,r}(),p=new l;e.exports={archiveSearch:p,Search:l}}},t={},function r(o){var n=t[o];if(void 0!==n)return n.exports;var c=t[o]={exports:{}};return e[o](c,c.exports,r),c.exports}(972);var e,t}));