import axios from "axios"; import { Downloader } from "./downloader"; import { Links } from "./links"; import { random_int } from "./tools"; let links = new Links; function sleep(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); } export class UrlPool { id: string; urls: string[] = []; used: boolean[] = []; generating = false; downloaders: Downloader[] = []; total_size: number = 0; is_direct = false; ready = false; constructor(id: string) { this.id = id; } async init() { let page = await axios.get("https://uloz.to/file/"+this.id, {validateStatus: null}); if(page.status == 404) return false; // TODO: Add quick download support // let quick_dl_url = links.regex_parse(new RegExp('href="(/quickDownload/[^"]*)"'), page.data, 1); this.is_direct = 'js-free-download-button-direct' == links.regex_parse(new RegExp('data-href="/download-dialog/free/[^"]+" +class=".+(js-free-download-button-direct).+"'), page.data, 1); await this.generate(); let url = await this.get(); if(!url) throw "No available URL in pool!"; let r = await axios.get(url[1], { responseType: 'arraybuffer', headers: { Range: `bytes=0-0` } }); this.total_size = parseInt(r.headers["content-range"].split("/")[1]); this.return(url[0]); this.ready = true; return true; } get_raw(): [number, string] | undefined { for(let i=0; i { let url = this.get_raw(); if(url) return url; return new Promise((complete) => { let check = setInterval(() => { let url = this.get_raw(); if(url) { clearInterval(check); complete(url); } }, 100); }); } return(i: number, sucess: boolean = true) { setTimeout(() => this.used[i] = false, sucess ? random_int(1000, 5000) : random_int(5000, 60000)); } add(url: string) { this.urls.push(url); this.used.push(false); } available(count?: number) { let available: number[] = []; for(let i=0; i { try { let link = await (this.is_direct ? links.direct_link(this.id) : links.captcha_link(this.id)); if(link) this.add(link); return link; } catch(e) { console.log(e); this.generating = false; return undefined; } } get_downloader(from: number) { let d = new Downloader(this, from); this.downloaders.push(d); return d; } } export class UrlPoolStorage { pools: {[U: string]: UrlPool} = {}; async get(id: string): Promise { return this.pools[id] ? this.pools[id] : await this.new(id); } async new(id: string): Promise { let pool = new UrlPool(id); let check = await pool.init(); if(!check) return undefined; pool.start_generation(); this.pools[id] = pool; return pool; } }