UNPKG

nyaa.si-client

Version:
2 lines (1 loc) 22.1 kB
"use strict";var Re=Object.create;var Z=Object.defineProperty,ve=Object.defineProperties,Le=Object.getOwnPropertyDescriptor,Oe=Object.getOwnPropertyDescriptors,ke=Object.getOwnPropertyNames,pe=Object.getOwnPropertySymbols,Se=Object.getPrototypeOf,he=Object.prototype.hasOwnProperty,Ie=Object.prototype.propertyIsEnumerable;var fe=(s,t,e)=>t in s?Z(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,ae=(s,t)=>{for(var e in t||(t={}))he.call(t,e)&&fe(s,e,t[e]);if(pe)for(var e of pe(t))Ie.call(t,e)&&fe(s,e,t[e]);return s},se=(s,t)=>ve(s,Oe(t));var Ce=(s,t)=>{for(var e in t)Z(s,e,{get:t[e],enumerable:!0})},ye=(s,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of ke(t))!he.call(s,r)&&r!==e&&Z(s,r,{get:()=>t[r],enumerable:!(i=Le(t,r))||i.enumerable});return s};var oe=(s,t,e)=>(e=s!=null?Re(Se(s)):{},ye(t||!s||!s.__esModule?Z(e,"default",{value:s,enumerable:!0}):e,s)),Ee=s=>ye(Z({},"__esModule",{value:!0}),s);var y=(s,t,e)=>new Promise((i,r)=>{var n=l=>{try{o(e.next(l))}catch(m){r(m)}},c=l=>{try{o(e.throw(l))}catch(m){r(m)}},o=l=>l.done?i(l.value):Promise.resolve(l.value).then(n,c);o((e=e.apply(s,t)).next())});var Be={};Ce(Be,{Cheerio:()=>Q,Client:()=>ee,DataTypes:()=>H,ElementPropertyTypes:()=>ue,Exporter:()=>ie,FileEntityType:()=>ne,FilterObject:()=>ce,Filters:()=>we,Job:()=>_,JobStatus:()=>le,Method:()=>G,Puppeteer:()=>W,RunTimes:()=>X,Scraper:()=>me,SessionManager:()=>te,TypedEventEmitter:()=>Y});module.exports=Ee(Be);var xe=oe(require("axios"),1),Te=require("nanoid"),De=oe(require("puppeteer"),1);var X=(e=>(e.Puppeteer="puppeteer",e.Cheerio="cheerio",e))(X||{});var le=(i=>(i.Reserved="reserved",i.Listening="listening",i.Dead="dead",i))(le||{});var ge=require("events"),Y=class{constructor(){this.emitter=new ge.EventEmitter}on(t,e){return this.emitter.on(t,e),this}emit(t,...e){return this.emitter.emit(t,...e)}off(t,e){return this.emitter.off(t,e),this}};var $e=require("puppeteer");var _=class{constructor({runTime:t,timeout:e,client:i,api:r,id:n}){this.status="dead";this.runTime=t,this.timeout=e,this.client=i,this.id=n,this.api=r,this.status="listening",this.client.emit("started",this),this.client.emit("listening",this)}finishedTask(){return y(this,null,function*(){this.runTime==="puppeteer"&&this.api.goto("about:blank"),yield new Promise(t=>setTimeout(()=>t(),this.timeout)),this.status="listening",this.client.emit("listening",this)})}stop(){ee.session.clear(this.id),this.runTime==="puppeteer"&&(this.api.isClosed()||this.api.close()),this.client.emit("died",this),this.client.jobs.delete(this.id)}};var be=oe(require("user-agents"),1),te=class{constructor(t){this.options=t;this.sessions=new Map}create(t,e){var r,n,c,o;let i=(o=(r=this.options)==null?void 0:r.defaultUserAgent)!=null?o:new be.default((c=(n=this.options)==null?void 0:n.userAgentOptions)!=null?c:e).toString();return this.sessions.set(t,i),i}get(t){return this.sessions.get(t)}createOrGet(t,e){let i=this.get(t);return i||this.create(t,e)}clear(t){return this.sessions.delete(t)}};var V=class V{constructor(t){this.runTime="cheerio";this.jobs=new Map;this.timeout=3e3;this.showNavigator=!1;this.concurrentJobs=1;this.requestQueue=[];this.host="https://nyaa.si";this.emitter=new Y;t!=null&&t.session&&(V.session=t.session),t!=null&&t.showNavigator&&(this.showNavigator=t.showNavigator),t!=null&&t.concurrentJobs&&(this.concurrentJobs=t.concurrentJobs),t!=null&&t.runTime&&(this.runTime=t.runTime),this.setupQueueListener()}on(t,e){this.emitter.on(t,e)}emit(t,...e){this.emitter.emit(t,...e)}initialize(){return y(this,null,function*(){switch(this.runTime){case"cheerio":{this.setupCheerio();break}case"puppeteer":yield this.setupPuppeteer()}})}setupPuppeteer(){return y(this,null,function*(){return this.browser=yield De.default.launch({waitForInitialPage:!0,acceptInsecureCerts:!0,headless:!this.showNavigator}),yield Promise.all(Array.from({length:this.concurrentJobs},()=>y(this,null,function*(){let{page:t,pageId:e}=yield this.newPage();new _({id:e,api:t,runTime:this.runTime,timeout:this.timeout,client:this})}))),this})}setupCheerio(){for(let t=0;t<this.concurrentJobs;t++){let{instance:e,instanceId:i}=this.newAxiosInstance(),r=new _({id:i,api:e,runTime:this.runTime,timeout:this.timeout,client:this});this.jobs.set(r.id,r)}return this}set setURL(t){this.host=t}getURL(){return this.host}newAxiosInstance(){let t=(0,Te.nanoid)(),e=V.session.create(t);return{instance:xe.default.create({baseURL:this.host,headers:{"User-Agent":e},method:"GET"}),instanceId:t}}newPage(){return y(this,null,function*(){let e=yield(yield this.browser.createBrowserContext()).newPage(),i=e.mainFrame()._id,r=V.session.create(i);return yield e.setUserAgent(r),yield e.setViewport({width:1920,height:1080}),{page:e,pageId:i}})}requestJob(){return new Promise(t=>{let e=Array.from(this.jobs.values()).find(i=>i.status==="listening");if(e)return e.status="reserved",this.emit("reserved",e),t(e);this.requestQueue.push(t)})}setupQueueListener(){this.on("listening",t=>{let e=this.requestQueue.shift();e&&(t.status="reserved",this.emit("reserved",t),e(t))})}};V.session=new te;var ee=V;var ie=class{constructor(t){this.scraper=t.scraper,this.data=t.data,this.search=t.search}getData(){return this.data}setData(t){this.data=t}addNextPage(t=1){return y(this,null,function*(){if(!this.data.metadata.hasNextPage)return this;let e=yield this.scraper.search(this.search.content,{page:this.data.metadata.current+1,loadOnlyPage:!(t>1),filter:this.search.filter},this);return this.setData(e.data),this})}getNextPage(t=1){return y(this,null,function*(){if(!this.data.metadata.hasNextPage)throw new Error("There is no next page for fetching information");return yield this.scraper.search(this.search.content,{page:this.data.metadata.current+1,loadOnlyPage:!(t>1),filter:this.search.filter})})}};var de=oe(require("cheerio"),1);var H=(e=>(e.List="list",e.Details="details",e))(H||{}),ne=(e=>(e.File="file",e.Folder="folder",e))(ne||{});var G=class{constructor({job:t,url:e,loadAdditionalInfo:i}){this.job=t,this.url=e,this.loadAdditionalInfo=i!=null?i:!1}};var Q=class s extends G{extract(t){return y(this,null,function*(){var l,m,u,p,T,D,S,P,R,E,B,U,M,K,j,q,$,d,h,w,v,a,x,L,I,A;let e=yield this.job.api(`${this.url}?${t}`),i=de.load(e.data),r=i(".torrent-list tbody tr"),n=[];for(let f of r){let g=(l=i(f).find("td:nth-child(1) a").first().attr())==null?void 0:l.title,b=(m=i(f).find("td:nth-child(2) a:not(.comments)").first().attr())==null?void 0:m.title,O=(u=i(f).find("td:nth-child(2) a:not(.comments)").first().attr())==null?void 0:u.href,F=(p=i(f).find('td:nth-child(3) a[href$=".torrent"]').first().attr())==null?void 0:p.href,k=(T=i(f).find('td:nth-child(3) a[href^="magnet:"]').first().attr())==null?void 0:T.href,z=i(f).find("td:nth-child(4)").first().text().trim(),N=parseInt((S=(D=i(f).find("td:nth-child(5)").first().attr())==null?void 0:D["data-timestamp"])!=null?S:""),J=parseInt(i(f).find("td:nth-child(6)").first().text().trim()),re=parseInt(i(f).find("td:nth-child(7)").first().text().trim()),Pe=parseInt(i(f).find("td:nth-child(8)").first().text().trim());g&&b&&k&&O&&F&&n.push({id:parseInt((R=(P=O.match(/\/view\/(\d+)/))==null?void 0:P[1])!=null?R:"0"),hash:(B=(E=k==null?void 0:k.match(/magnet:\?xt=urn:btih:([a-fA-F0-9]{40}|[a-zA-Z0-9]{32})/))==null?void 0:E[1])!=null?B:"",category:g,name:b,links:{magnet:k,page:O,torrent:F},size:z,timestamp:N,stats:{seeders:J,leechers:re,downloaded:Pe}})}let c=i("ul.pagination li"),o={hasPreviousPage:!0,hasNextPage:!0,current:0,total:0,timestamp:Date.now(),timeTaken:0};for(let f of c)switch(!0){case((U=f.attribs.class)==null?void 0:U.includes("active")):{let b=(j=(K=i(f).find("a").first().text().trim())!=null?K:(M=this.url.match(/[?&]p=(\d+)/))==null?void 0:M[1])!=null?j:"0";o.current=parseInt(b);break}case((q=f.attribs.class)==null?void 0:q.includes("previous")):{($=f.attribs.class)!=null&&$.includes("disabled")&&(o.hasPreviousPage=!1);break}case((d=f.attribs.class)==null?void 0:d.includes("next")):{(h=f.attribs.class)!=null&&h.includes("disabled")&&(o.hasNextPage=!1);let g=new URL((v=(w=i(f).find("a").attr())==null?void 0:w.href)!=null?v:"",this.url).toString();o.nextPageLink=g,o.nextPage=(a=s.extractPageNumber(new URL(g,this.url).toString()))!=null?a:0;break}case((x=f.attribs.class)==null?void 0:x.includes("prev")):{let g=new URL((I=(L=i(f).find("a").attr())==null?void 0:L.href)!=null?I:"",this.url).toString();o.previousPageLink=g,o.previousPage=s.extractPageNumber(new URL(g,this.url).toString());break}default:{let g=o.total,b=parseFloat((A=i(f).find("a").text())!=null?A:"0");g===void 0&&(o.total=b),Number(g)<b&&(o.total=b)}}return yield this.job.finishedTask(),{type:"list",count:n.length,metadata:o,torrents:n}})}details(t,e){return y(this,null,function*(){var c,o,l;let i=yield this.job.api(t),r=de.load(i.data),n={type:"details",description:void 0,submitter:void 0,information:void 0,files:void 0,comments:void 0};if(e===void 0||e===!0||e!==!1&&(e!=null&&e.submitter)){let m=r('[title="User"]'),u=m.text(),p=(c=m.attr())==null?void 0:c.href;u&&p&&(n.submitter={name:u,url:new URL(p,this.url).toString()})}if((e===void 0||e===!0||e!==!1&&(e!=null&&e.information))&&(n.information=r("body > div > div:nth-child(1) > div.panel-body > div:nth-child(3) > div:nth-child(2)").text().trim()),e===!0||e!==!1&&(e!=null&&e.description)){let m=r("torrent-description");switch(e){case!0:{let u=m.html();if(u){n.description=u;break}break}default:switch(e.description){case"text":{n.description=m.text().trim();break}default:{n.description=m.html();break}}}}if(e===void 0||e===!0||e!==!1&&(e!=null&&e.files)){let m=r(".torrent-file-list");if(m){let u=s.parseTorrentComponents(m.html());n.files=u}}if(e===void 0||e===!0||e!==!1&&(e!=null&&e.comments)){let m=r(".comment-panel"),u=[];for(let p of m){let T=(o=r(p).find("img").first().attr("src"))!=null?o:"",D=r(p).find('[title="User"]').first().text().trim(),S=(l=r(p).find('[title="User"]').first().parent().text().trim().includes("uploader"))!=null?l:!1,P=r(p).find("[markdown-text]").first().text().trim(),R=r(p).find("[data-timestamp-swap]").first(),E=R.data("timestamp"),B=R.text().replaceAll("UTC","").trim();!T||!D||!P||!E||!B||u.push({avatarURL:T,userName:D,message:P,publishDate:B,timestamp:parseInt(E),isUploader:S})}u.length>0&&(n.comments=u)}return yield this.job.finishedTask(),n})}static parseTorrentComponents(t){if(!t)return[];let e=de.load(t),i=[],r=e(t).children("ul li");for(let n of r){let c=e(n).children("a.folder").first();if(c.length>0){let o=c.text().trim();if(!o)return i;let l=e(n).html();if(!l)return i;let m=this.parseTorrentComponents(l);i.push({type:"folder",name:o,files:m})}else{let o=e(n).children("span.file-size").text().trim();if(!o)return i;let l=e(n).text().replaceAll(o,"");if(!l)return i;let m=o.replace("(","").replace(")","");i.push({type:"file",fileName:l,readableSize:m,sizeInBytes:this.parseSizeToBytes(m)})}}return i}static parseSizeToBytes(t){let e={B:1,KiB:1024,MiB:1048576,GiB:1073741824,TiB:1099511627776,PiB:0x4000000000000},i=/([\d.]+)\s*(B|KiB|MiB|GiB|TiB|PiB)/i,r=t.match(i);if(!r)throw new Error(`Invalid size format: ${t}`);let[,n,c]=r,o=parseFloat(n);if(!Object.prototype.hasOwnProperty.call(e,c))throw new Error(`Unknown unit: ${c}`);let l=e[c];return o*l}static extractPageNumber(t){let i=new URL(t).searchParams.get("p");return i?parseInt(i,10):void 0}};var Ae=require("node-html-markdown");var ce={anime:["anime_music_video","english_translated","non_english_translated","raw"],audio:["lossless","lossy"],literature:["english_translated","non_english_translated","raw"],live_action:["english_translated","idol_promotional_video","non_english_translated","raw"],pictures:["graphics","photos"],software:["applications","games"]},ue=(i=>(i.TextContent="textContent",i.Href="href",i.GetAttribute="getAttribute",i))(ue||{}),we=(i=>(i[i.NoFilter=0]="NoFilter",i[i.NoRemakes=1]="NoRemakes",i[i.TrustedOnly=2]="TrustedOnly",i))(we||{});var Fe=new Ae.NodeHtmlMarkdown,W=class extends G{extract(t){return y(this,null,function*(){return yield this.waitForConnect(`${this.url}?${t}`),yield this.job.api.evaluate((e,i)=>{var D,S,P,R,E,B,U,M,K,j,q,$;function r({selector:d,subSelector:h,row:w}){var a;let v=h?`${d} ${h}`:d;return(a=w.querySelector(v))!=null?a:void 0}function n(d){var v,a;if(!d.element)return;let{element:h,type:w}=d;switch(w){case e.GetAttribute:return(v=h.getAttribute(d.attributeName))!=null?v:void 0;case e.TextContent:return(a=h.textContent)==null?void 0:a.trim();case e.Href:return h.href}}let c=document.querySelector(".torrent-list");if(!c)throw new Error("Torrent list not available!");let l=Array.from(c.querySelectorAll("tbody tr")).map(d=>{var b,O,F,k,z,N,J;let h=n({type:e.GetAttribute,element:r({row:d,selector:"td:nth-child(1)",subSelector:"a"}),attributeName:"title"}),w=n({type:e.GetAttribute,element:r({row:d,selector:"td:nth-child(2)",subSelector:"a:not(.comments)"}),attributeName:"title"}),v=n({type:e.Href,element:r({row:d,selector:"td:nth-child(2)",subSelector:"a:not(.comments)"})}),a=n({type:e.Href,element:r({row:d,selector:"td:nth-child(3)",subSelector:'a[href$=".torrent"]'})}),x=n({type:e.Href,element:r({row:d,selector:"td:nth-child(3)",subSelector:'a[href^="magnet:"]'})}),L=n({type:e.TextContent,element:r({row:d,selector:"td:nth-child(4)"})}),I=parseInt((b=n({type:e.GetAttribute,element:r({row:d,selector:"td:nth-child(5)"}),attributeName:"data-timestamp"}))!=null?b:"undefined"),A=parseInt((O=n({type:e.TextContent,element:r({row:d,selector:"td:nth-child(6)"})}))!=null?O:""),f=parseInt((F=n({type:e.TextContent,element:r({row:d,selector:"td:nth-child(7)"})}))!=null?F:""),g=parseInt((k=n({type:e.TextContent,element:r({row:d,selector:"td:nth-child(8)"})}))!=null?k:"");return{id:parseInt((N=(z=v==null?void 0:v.match(/\/view\/(\d+)/))==null?void 0:z[1])!=null?N:""),hash:(J=x==null?void 0:x.match(/magnet:\?xt=urn:btih:([a-fA-F0-9]{40}|[a-zA-Z0-9]{32})/))==null?void 0:J[1],name:w,timestamp:I,size:L,category:h,links:v&&x&&a?{page:v,magnet:x,torrent:a}:void 0,stats:{seeders:A,leechers:f,downloaded:g}}}),m=document.querySelectorAll("ul.pagination"),u={hasPreviousPage:!0,hasNextPage:!0,current:0,total:0,timestamp:Date.now(),timeTaken:0};function p(d){let w=new URL(d).searchParams.get("p");return w?parseInt(w,10):null}for(let d of Array.from(m[0].querySelectorAll("li")))switch(!0){case d.classList.contains("active"):{let w=(R=(P=(D=d.querySelector("a").childNodes[0].textContent)==null?void 0:D.trim())!=null?P:(S=window.location.href.match(/[?&]p=(\d+)/))==null?void 0:S[1])!=null?R:"0";u.current=parseInt(w);break}case d.classList.contains("previous"):{d.classList.contains("disabled")&&(u.hasPreviousPage=!1);break}case d.classList.contains("next"):{d.classList.contains("disabled")&&(u.hasNextPage=!1);let h=(B=(E=d.querySelector("a"))==null?void 0:E.href)!=null?B:"";u.nextPageLink=h,u.nextPage=(U=p(h))!=null?U:0;break}case d.classList.contains("prev"):{let h=(K=(M=d.querySelector("a"))==null?void 0:M.href)!=null?K:"";u.previousPageLink=h,u.previousPage=(j=p(h))!=null?j:void 0;break}default:{let h=u.total,w=parseFloat(($=(q=d.querySelector("a"))==null?void 0:q.textContent)!=null?$:"0");h===void 0&&(u.total=w),Number(h)<w&&(u.total=w)}}return{type:i.List,metadata:u,count:l.length,torrents:l.filter(d=>Object.values(d).every(h=>h!==void 0))}},ue,H).finally(()=>y(this,null,function*(){return yield this.job.finishedTask()}))})}details(t,e){return y(this,null,function*(){let i=new URL(`/view/${t}`,this.url).toString();return yield this.waitForConnect(i),yield this.job.api.evaluate((r,n,c,o)=>{var p,T,D,S,P,R,E,B,U,M,K,j,q,$,d,h,w,v;let l={type:c.Details,description:void 0,submitter:void 0,information:void 0,files:void 0,comments:void 0};if(n===!0||n!==!1&&(n!=null&&n.submitter)){let a=document.querySelector('[title="User"]'),x=(p=a==null?void 0:a.textContent)==null?void 0:p.trim(),L=a==null?void 0:a.getAttribute("href");x&&L&&(l.submitter={name:x,url:new URL(L,r).toString()})}if(n===!0||n!==!1&&(n!=null&&n.information)){let a=document.querySelector("body > div > div:nth-child(1) > div.panel-body > div:nth-child(3) > div:nth-child(2)"),x=(T=a==null?void 0:a.textContent)==null?void 0:T.trim();l.information=x}if(n===!0||n!==!1&&(n!=null&&n.description)){let a=document.getElementById("torrent-description");switch(n){case!0:{l.description=(D=a==null?void 0:a.outerHTML)!=null?D:a==null?void 0:a.getHTML();break}default:switch(n.description){case"text":{l.description=(P=(S=a==null?void 0:a.textContent)!=null?S:a==null?void 0:a.innerText)!=null?P:"";break}default:l.description=(R=a==null?void 0:a.outerHTML)!=null?R:a==null?void 0:a.getHTML()}}}if(n===!0||n!==!1&&(n!=null&&n.files)){let a=function(I){let A={B:1,KiB:1024,MiB:1048576,GiB:1073741824,TiB:1099511627776,PiB:0x4000000000000},f=/([\d.]+)\s*(B|KiB|MiB|GiB|TiB|PiB)/i,g=I.match(f);if(!g)throw new Error(`Invalid size format: ${I}`);let[,b,O]=g,F=parseFloat(b);if(!Object.prototype.hasOwnProperty.call(A,O))throw new Error(`Unknown unit: ${O}`);let k=A[O];return F*k},x=function(I){var g,b,O,F;let A=[],f=Array.from(I.querySelectorAll(":scope > ul > li"));for(let k of f){let z=k.querySelector(":scope > a.folder");if(z){let N=(g=z.textContent)==null?void 0:g.trim();if(!N)return A;let J=x(k);A.push({type:o.Folder,name:N,files:J})}else{let N=(O=(b=k.querySelector(":scope > span.file-size"))==null?void 0:b.textContent)==null?void 0:O.trim();if(!N)return A;let J=(F=k.textContent)==null?void 0:F.replaceAll(N,"");if(!J)return A;let re=N.replace("(","").replace(")","");A.push({type:o.File,fileName:J,readableSize:re,sizeInBytes:a(re)})}}return A};var m=a,u=x;let L=document.querySelector(".torrent-file-list");if(L){let I=x(L);l.files=I}}if(n===!0||n!==!1&&(n!=null&&n.comments)){let a=document.querySelectorAll(".comment-panel"),x=[];for(let L of Array.from(a)){let I=(B=(E=L.querySelector("img"))==null?void 0:E.src)!=null?B:"",A=(K=(M=(U=L.querySelector('[title="User"]'))==null?void 0:U.textContent)==null?void 0:M.trim())!=null?K:"",f=(d=($=(q=(j=L.querySelector('[title="User"]'))==null?void 0:j.parentElement)==null?void 0:q.textContent)==null?void 0:$.trim().includes("uploader"))!=null?d:!1,g=(v=(w=(h=L.querySelector("[markdown-text]"))==null?void 0:h.textContent)==null?void 0:w.trim())!=null?v:"",b=L.querySelector("[data-timestamp-swap]"),O=b==null?void 0:b.getAttribute("data-timestamp"),F=b==null?void 0:b.getAttribute("title");!I||!A||!g||!O||!F||x.push({avatarURL:I,userName:A,message:g,publishDate:F,timestamp:parseInt(O),isUploader:f})}x.length>0&&(l.comments=x)}return l},this.url,e,H,ne).then(r=>{var n,c;return e===!0||e!==!1&&(e==null?void 0:e.description)===!0||e!==!1&&!["html","text"].includes((n=e==null?void 0:e.description)!=null?n:"")?se(ae({},r),{description:Fe.translate((c=r.description)!=null?c:"")}):r}).finally(()=>y(this,null,function*(){return yield this.job.finishedTask()}))})}waitForConnect(t){return y(this,null,function*(){let e;do{e&&(yield new Promise(r=>setTimeout(()=>r(),this.job.timeout)));let i=yield this.job.api.goto(t,{waitUntil:"domcontentloaded"});(i==null?void 0:i.status())===429&&console.log("The site detected too many hits: 429 Too Many Requests"),e=i==null?void 0:i.status()}while(e===429)})}};var me=class s{constructor(t){this.pagesToLoad=1;var e;this.client=t.client,t!=null&&t.pagesToLoad&&(this.pagesToLoad=t.pagesToLoad),this.loadAdditionalInfo=(e=t==null?void 0:t.loadAdditionalInfo)!=null?e:!1}search(t,e,i){return y(this,null,function*(){var m,u;let r=Date.now(),n=(yield Promise.all(Array.from({length:e!=null&&e.loadOnlyPage&&(e!=null&&e.page)?1:this.pagesToLoad},(p,T)=>y(this,null,function*(){let D=e!=null&&e.loadOnlyPage&&(e!=null&&e.page)?e.page:(e!=null&&e.page?e.page-1:0)+(T+1),S=yield this.client.requestJob(),P=s.getSearchParams(t,D,e==null?void 0:e.filter);switch(this.client.runTime){case"puppeteer":return new W({job:S,url:this.client.getURL(),loadAdditionalInfo:this.loadAdditionalInfo}).extract(P);case"cheerio":return new Q({job:S,loadAdditionalInfo:this.loadAdditionalInfo,url:this.client.getURL()}).extract(P)}})))).filter(p=>p!==void 0),c=n.reduce((p,T)=>p.concat(T.torrents),(u=(m=i==null?void 0:i.getData())==null?void 0:m.torrents)!=null?u:[]);this.loadAdditionalInfo!==!1&&(yield Promise.all(Array.from({length:n.length},(p,T)=>y(this,null,function*(){let D=n[T];yield Promise.all(Array.from({length:D.torrents.length},(S,P)=>y(this,null,function*(){let R=D.torrents[P],E=yield this.details(R.id,this.loadAdditionalInfo);R.details=E})))}))));let o=n.reduce((p,T)=>T.metadata.current>p.metadata.current?T:p,{metadata:{current:-1/0}}),l=Date.now();return new ie({scraper:this,search:{content:t,filter:e==null?void 0:e.filter},data:{type:"list",metadata:se(ae({},o.metadata),{timeTaken:(l-r)/1e3}),count:c.length,torrents:c}})})}details(t,e){return y(this,null,function*(){let i=s.normalizeId(t);if(!i)throw new Error(`Invalid torrent identifier: ${t}`);t=i;let r=yield this.client.requestJob(),n=new URL(`/view/${t}`,this.client.getURL()).toString();switch(this.client.runTime){case"puppeteer":return yield new W({url:this.client.getURL(),job:r,loadAdditionalInfo:this.loadAdditionalInfo}).details(n,e);default:return yield new Q({url:this.client.getURL(),job:r,loadAdditionalInfo:this.loadAdditionalInfo}).details(n,e)}})}static getSearchParams(t,e=1,i){var o,l;let r=String((o=i==null?void 0:i.filter)!=null?o:0),n=(l=i==null?void 0:i.category)==null?void 0:l.split("."),c="0_0";if(n){let[m,u]=n,p=Object.entries(ce).findIndex(([D])=>D===m),T=Object.entries(ce)[p][1].findIndex(D=>u===D);c=`${p+1}_${T+1}`}return new URLSearchParams({f:r,c,q:t,p:String(e)}).toString()}static normalizeId(t){if(typeof t=="number")return t;if(typeof t=="string"){let e=parseInt(t,10);if(!isNaN(e))return e;let i=s.extractId(t);return i!=null?i:null}return typeof t=="object"&&"id"in t&&typeof t.id=="number"?t.id:null}static extractId(t){var r;let e=/^https:\/\/nyaa\.si\/view\/(\d+)$/,i=(r=t.match(e))==null?void 0:r[1];return i?parseInt(i):void 0}};0&&(module.exports={Cheerio,Client,DataTypes,ElementPropertyTypes,Exporter,FileEntityType,FilterObject,Filters,Job,JobStatus,Method,Puppeteer,RunTimes,Scraper,SessionManager,TypedEventEmitter});