UNPKG

flibusta

Version:

Unofficial Flibusta API based on website search engine. If you like to read books - buy

1 lines 14.9 kB
(()=>{"use strict";var e={195:function(e,t,s){var r=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const o=s(517),a=r(s(782));class n extends a.default{static getAuthorTranslationsRegExp=/\d+ (перевода|перевод)/g;axiosInstance;constructor(e){super(e),this.axiosInstance=e}static generateGetBooksByNameURL(e,t){return`booksearch?ask=${encodeURIComponent(e)}&page=${t}&cha=on`}async fetchAuthorsFromFlibusta(e,t=0){const s=n.generateGetBooksByNameURL(e,t);return this.getFlibustaHTMLPage(s)}generateAuthorsListResponse(e){return e.map((e=>{const[t,...s]=e.childNodes,r=t;return{...this.getInformationOfBookOrAuthor(r),books:this.getBooksOrTranslations(s,this.getAuthorBooksRegExp),translations:this.getBooksOrTranslations(s,n.getAuthorTranslationsRegExp)}}))}async getAuthors(e){const t=await this.fetchAuthorsFromFlibusta(e);if((0,o.isNil)(t))return;const s=this.removePagerElements(t).querySelectorAll("ul li");return this.generateAuthorsListResponse(s)}async getAuthorsPaginated(e,t=0,s=50){const r=await this.fetchAuthorsFromFlibusta(e,t);if((0,o.isNil)(r))return;const a=this.getCurrentPageInformation(r),n=this.removePagerElements(r),i=n.querySelectorAll("ul li").slice(0,s),u=n;return{items:this.generateAuthorsListResponse(i),currentPage:t,totalCountItems:this.getTotalItemsCount(u),...a}}}t.default=n},469:function(e,t,s){var r=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const o=s(517),a=r(s(782));class n extends a.default{axiosInstance;constructor(e){super(e),this.axiosInstance=e}static generateGetAuthorsFromOpdsURL(e,t){return`opds/author/${e}/alphabet/${t}`}async fetchAuthorsFromOpds(e,t=0){const s=n.generateGetAuthorsFromOpdsURL(e,t),r=await this.getFlibustaOpdsEntry(s);if((0,o.isNil)(r))return;const{feed:a}=r;return a}async getAuthorsFromOpds(e){const t=await this.fetchAuthorsFromOpds(e);if((0,o.isNil)(t))return;const{entry:s}=t;return(0,o.isNil)(s)?void 0:this.prepareResponseFromOpdsEntry(s)}async getAuthorsFromOpdsPaginated(e,t=0,s=20){const r=await this.fetchAuthorsFromOpds(e,t);if((0,o.isNil)(r))return;const{entry:a}=r;if((0,o.isNil)(a))return;let n=a;return Array.isArray(n)&&(n=n.slice(0,s)),{items:this.prepareResponseFromOpdsEntry(n),currentPage:t,...this.getCurrentOpdsPageInformation(r,t)}}}t.default=n},751:function(e,t,s){var r=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const o=s(517),a=r(s(782)),n=r(s(789));class i extends a.default{axiosInstance;constructor(e){super(e),this.axiosInstance=e}static generateGetBooksByNameURL(e,t){return`booksearch?ask=${encodeURIComponent(e)}&page=${t}&chb=on`}async fetchBooksByNameFromFlibusta(e,t=0){const s=i.generateGetBooksByNameURL(e,t);return this.getFlibustaHTMLPage(s)}getBookAuthors(e){const t=[];return e.forEach((e=>{const s=e;if(n.default.containsComma(s.text))return;const r=n.default.getNumbersFromString(s.attrs.href),o=Number.parseInt(r,10);t.push({id:o,name:s.text})})),t}generateBooksListResponse(e){return e.map((e=>{const[t,,...s]=e.childNodes,r=t;return{book:this.getInformationOfBookOrAuthor(r),authors:this.getBookAuthors(s)}}))}async getBooksByName(e){const t=await this.fetchBooksByNameFromFlibusta(e);if((0,o.isNil)(t))return;const s=this.removePagerElements(t).querySelectorAll("ul li");return this.generateBooksListResponse(s)}async getBooksByNamePaginated(e,t=0,s=50){const r=await this.fetchBooksByNameFromFlibusta(e,t);if((0,o.isNil)(r))return;const a=this.getCurrentPageInformation(r),n=this.removePagerElements(r).querySelectorAll("ul li").slice(0,s),i=r;return{items:this.generateBooksListResponse(n),currentPage:t,totalCountItems:this.getTotalItemsCount(i),...a}}}t.default=i},825:function(e,t,s){var r=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const o=s(517),a=r(s(782));class n extends a.default{static ITEMS_PER_PAGE=20;axiosInstance;constructor(e){super(e),this.axiosInstance=e}static generateGetBooksByNameFromOpdsURL(e,t){return`opds/opensearch?searchTerm=${encodeURIComponent(e)}&searchType=books&pageNumber=${t}`}static hasSearchOpdsNextPage(e,t){return t+n.ITEMS_PER_PAGE<e}async fetchBooksByNameFromOpds(e,t=0){const s=n.generateGetBooksByNameFromOpdsURL(e,t),r=await this.getFlibustaOpdsEntry(s);if((0,o.isNil)(r))return;const{feed:a}=r;return a}getCurrentSearchOpdsPageInformation(e){const t=e["os:totalResults"],s=e["os:itemsPerPage"],r=e["os:startIndex"];return{hasNextPage:n.hasSearchOpdsNextPage(t,r),hasPreviousPage:this.hasPreviousPage(r),totalPages:this.getTotalPagesCount(t,s)}}async getBooksByNameFromOpds(e){const t=await this.fetchBooksByNameFromOpds(e);if((0,o.isNil)(t))return;const{entry:s}=t;return(0,o.isNil)(s)?void 0:this.prepareResponseFromOpdsEntry(s)}async getBooksByNameFromOpdsPaginated(e,t=0,s=20){const r=await this.fetchBooksByNameFromOpds(e,t);if((0,o.isNil)(r))return;const{entry:a}=r;if((0,o.isNil)(a))return;let n=a;Array.isArray(n)&&(n=n.slice(0,s));const i=this.prepareResponseFromOpdsEntry(n),u=this.getCurrentSearchOpdsPageInformation(r);return{items:i,currentPage:t,totalCountItems:r["os:totalResults"],...u}}}t.default=n},865:function(e,t,s){var r=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const o=s(517),a=r(s(782));class n extends a.default{axiosInstance;constructor(e){super(e),this.axiosInstance=e}static generateGetBooksByNameURL(e,t){return`booksearch?ask=${encodeURIComponent(e)}&page=${t}&chs=on`}async fetchBooksBySeriesFromFlibusta(e,t=0){const s=n.generateGetBooksByNameURL(e,t);return this.getFlibustaHTMLPage(s)}generateBooksSeriesListResponse(e){return e.map((e=>{const[t,...s]=e.childNodes,r=this.getBooksOrTranslations(s,this.getAuthorBooksRegExp),o=t;return{...this.getInformationOfBookOrAuthor(o),books:r}}))}async getBooksBySeries(e){const t=await this.fetchBooksBySeriesFromFlibusta(e);if((0,o.isNil)(t))return;const s=this.removePagerElements(t).querySelectorAll("ul li");return this.generateBooksSeriesListResponse(s)}async getBooksBySeriesPaginated(e,t=0,s=50){const r=await this.fetchBooksBySeriesFromFlibusta(e,t);if((0,o.isNil)(r))return;const a=this.getCurrentPageInformation(r),n=this.removePagerElements(r).querySelectorAll("ul li").slice(0,s),i=r;return{items:this.generateBooksSeriesListResponse(n),currentPage:t,totalCountItems:this.getTotalItemsCount(i),...a}}}t.default=n},875:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0});const r=s(517);class o{axiosInstance;constructor(e){this.axiosInstance=e}static generateCoverUrl(e,t,s){return`i/${t}/${e}/cover.${s}`}async fetchImageByTypeUrl(e){return this.axiosInstance.get(e,{}).then((e=>e.data)).catch((e=>{throw e}))}async fetchCoverByUrl(e){const t=e.toString().slice(4),s=Number.parseInt(t,10),a=o.generateCoverUrl(e,s,"jpg"),n=await this.fetchImageByTypeUrl(a).catch((()=>{}));if(!(0,r.isNil)(n))return n;const i=o.generateCoverUrl(e,s,"png"),u=await this.fetchImageByTypeUrl(i).catch((()=>{}));return(0,r.isNil)(u)?void 0:u}async getCoverByBookId(e){return this.fetchCoverByUrl(e)}}t.default=o},574:function(e,t,s){var r=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const o=s(517),a=r(s(782)),n=r(s(789));class i extends a.default{axiosInstance;constructor(e){super(e),this.axiosInstance=e}static generateGetGenresURL(e,t){return`booksearch?ask=${encodeURIComponent(e)}&page=${t}&chg=on`}async fetchGenresFromFlibusta(e,t=0){const s=i.generateGetGenresURL(e,t);return this.getFlibustaHTMLPage(s)}static getInformationOfGenres(e){const t=e.attrs.href.replace("/g/",""),s=e.childNodes.map((e=>e.text));return{id:t,name:n.default.concatenateString(s)}}generateGenresListResponse(e){return e.map((e=>{const[t]=e.childNodes,s=t;return i.getInformationOfGenres(s)}))}async getGenres(e){const t=await this.fetchGenresFromFlibusta(e);if((0,o.isNil)(t))return;const s=this.removePagerElements(t).querySelectorAll("ul li");return this.generateGenresListResponse(s)}async getGenresPaginated(e,t=0,s=50){const r=await this.fetchGenresFromFlibusta(e,t);if((0,o.isNil)(r))return;const a=this.getCurrentPageInformation(r),n=this.removePagerElements(r).querySelectorAll("ul li").slice(0,s),i=r;return{items:this.generateGenresListResponse(n),currentPage:t,totalCountItems:this.getTotalItemsCount(i),...a}}}t.default=i},782:function(e,t,s){var r=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const o=s(955),a=s(517),n=r(s(780)),i=r(s(789));class u extends n.default{axiosInstance;getAuthorBooksRegExp=/\d+ книг/g;matchOnlyNumbersRegExp=/\d+/g;constructor(e){super(e),this.axiosInstance=e}getCurrentPageInformation(e){const t=e.querySelectorAll("div.item-list .pager");if((0,a.isEmpty)(t))return{totalPages:1,hasNextPage:!1,hasPreviousPage:!1};t[1].remove();const s=t[0].childNodes,r=s.filter((e=>{const t=e;return t.attrs?.class.includes("pager-current")||t.attrs?.class.includes("pager-item")})),o=s.some((e=>{const t=e;return"pager-next"===t.attrs?.class})),n=s.some((e=>{const t=e;return"pager-previous"===t.attrs?.class}));return{totalPages:r.length,hasNextPage:o,hasPreviousPage:n}}async getFlibustaHTMLPage(e){return this.axiosInstance.get(e).then((e=>(0,o.parse)(e.data).querySelector("#main"))).catch((e=>{throw e}))}getInformationOfBookOrAuthor(e){const t=i.default.getNumbersFromString(e.attrs.href),s=Number.parseInt(t,10),r=e.childNodes.map((e=>e.text));return{id:s,name:i.default.concatenateString(r)}}getBooksOrTranslations(e,t){const s=e.toString(),r=i.default.getStringMatches(s,t);if((0,a.isNil)(r))return;const[o]=r,n=i.default.getNumbersFromString(o);return Number.parseInt(n,10)}getTotalItemsCount(e){const t=e.querySelector("h3"),s=t?.text;if((0,a.isNil)(s))return;const[,r]=s.split("из"),o=r.match(this.matchOnlyNumbersRegExp);if((0,a.isNil)(o))return;const[n]=o;return Number.parseInt(n,10)}removePagerElements(e){const t=e.querySelectorAll("div.item-list .pager");return(0,a.isEmpty)(t)||t.forEach((e=>{e.remove()})),e}}t.default=u},780:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0});const r=s(861),o=s(517);class a{static FlibustaFileMIMETypesToDownload=new Set(["application/epub","application/fb2+zip","application/html+zip","application/pdf+rar","application/rtf+zip","application/txt+zip","application/x-mobipocket-ebook","application/pdf+zip","application/djvu","application/msword","application/x-rar-compressed","application/pdf"]);static getAuthorsFromOpdsEntry(e){return(0,o.isArray)(e)?e.map((e=>({name:e.name,uri:e.uri}))):[e]}static getCategoriesFromOpdsEntry(e){return(0,o.isArray)(e)?e.map((e=>e["@_label"])):[e["@_label"]]}static getCoverFromLink(e){if((0,o.isNil)(e))return;const t=e.find((e=>"image/jpeg"===e["@_type"]||"image/png"===e["@_type"]));return(0,o.isNil)(t)?void 0:t["@_href"]}static getDownloadsItemList(e){const{FlibustaFileMIMETypesToDownload:t}=a;return e.filter((e=>t.has(e["@_type"]))).map((e=>({link:e["@_href"],type:e["@_type"]})))}axiosInstance;constructor(e){this.axiosInstance=e}prepareResponseFromOpdsObjectEntry(e){const{author:t,link:s,title:r,updated:o,content:n,category:i}=e;return{author:a.getAuthorsFromOpdsEntry(t),title:r,updated:o,categories:a.getCategoriesFromOpdsEntry(i),cover:a.getCoverFromLink(s),downloads:a.getDownloadsItemList(s),description:n["#text"]}}prepareResponseFromOpdsEntry(e){return Array.isArray(e)?e.map((e=>this.prepareResponseFromOpdsObjectEntry(e))):[this.prepareResponseFromOpdsObjectEntry(e)]}getTotalPagesCount(e,t){return(0,o.round)(e/t,0)}hasNextPage(e){return e.some((e=>"next"===e["@_rel"]))}hasPreviousPage(e){return!(e<=0)}getCurrentOpdsPageInformation(e,t){const s=e.link;return{hasNextPage:this.hasNextPage(s),hasPreviousPage:this.hasPreviousPage(t)}}async getFlibustaOpdsEntry(e){return this.axiosInstance.get(e).then((e=>new r.XMLParser({ignoreAttributes:!1,unpairedTags:["hr","br","link","meta"],stopNodes:["*.pre","*.script"],processEntities:!0,htmlEntities:!0}).parse(e.data))).catch((e=>{throw e}))}}t.default=a},465:function(e,t,s){var r=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const o=r(s(167)),a=r(s(195)),n=r(s(469)),i=r(s(751)),u=r(s(825)),c=r(s(865)),l=r(s(875)),g=r(s(574));t.default=class{getAuthorsApi;getBooksByAuthorOpdsApi;getBooksByNameApi;getBooksByNameOpdsApi;getBooksBySeriesApi;getCoverByBookIdApi;getGenresApi;constructor(e="http://flibusta.is/",t){const s=o.default.create({baseURL:e,...t});this.getAuthorsApi=new a.default(s),this.getBooksByAuthorOpdsApi=new n.default(s),this.getBooksByNameApi=new i.default(s),this.getBooksByNameOpdsApi=new u.default(s),this.getBooksBySeriesApi=new c.default(s),this.getCoverByBookIdApi=new l.default(s),this.getGenresApi=new g.default(s)}async getAuthors(e){return this.getAuthorsApi.getAuthors(e)}async getBooksByAuthorOpds(e){return this.getBooksByAuthorOpdsApi.getAuthorsFromOpds(e)}async getBooksByAuthorOpdsPaginated(e,t=0,s=50){return this.getBooksByAuthorOpdsApi.getAuthorsFromOpdsPaginated(e,t,s)}async getAuthorsPaginated(e,t=0,s=50){return this.getAuthorsApi.getAuthorsPaginated(e,t,s)}async getBooksByName(e){return this.getBooksByNameApi.getBooksByName(e)}async getBooksByNameFromOpds(e){return this.getBooksByNameOpdsApi.getBooksByNameFromOpds(e)}async getBooksByNameFromOpdsPaginated(e,t=0,s=20){return this.getBooksByNameOpdsApi.getBooksByNameFromOpdsPaginated(e,t,s)}async getBooksByNamePaginated(e,t=0,s=50){return this.getBooksByNameApi.getBooksByNamePaginated(e,t,s)}async getBooksBySeries(e){return this.getBooksBySeriesApi.getBooksBySeries(e)}async getBooksBySeriesPaginated(e,t=0,s=50){return this.getBooksBySeriesApi.getBooksBySeriesPaginated(e,t,s)}async getCoverByBookId(e){return this.getCoverByBookIdApi.getCoverByBookId(e)}async getGenres(e){return this.getGenresApi.getGenres(e)}async getGenresPaginated(e,t=0,s=50){return this.getGenresApi.getGenresPaginated(e,t,s)}}},789:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=class{static getNumbersFromString(e){return e.replace(/\D/g,"")}static containsComma(e){return/,/.test(e)}static getStringMatches(e,t){return e.match(t)}static concatenateString(e){return e.join("")}}},167:e=>{e.exports=require("axios")},861:e=>{e.exports=require("fast-xml-parser")},517:e=>{e.exports=require("lodash")},955:e=>{e.exports=require("node-html-parser")}},t={},s=function s(r){var o=t[r];if(void 0!==o)return o.exports;var a=t[r]={exports:{}};return e[r].call(a.exports,a,a.exports,s),a.exports}(465),r=exports;for(var o in s)r[o]=s[o];s.__esModule&&Object.defineProperty(r,"__esModule",{value:!0})})();