This repository has been archived on 2023-11-29. You can view files and clone it, but cannot push or open issues or pull requests.
Uloz.to-rychle/urlpool.ts

141 lines
3.2 KiB
TypeScript

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<this.urls.length; i++) {
if(this.used[i]) continue;
this.used[i] = true;
return [i, this.urls[i]];
}
}
async get(): Promise<[number, string] | undefined> {
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<this.used.length; i++) {
if(!this.used[i]) {
available.push(i);
}
}
return count ? available.slice(0, count) : available;
}
async start_generation() {
this.generating = true;
while(this.urls.length < 15 && this.generating) {
console.log(this.id, "| new link:", (await this.generate()) ? true : false);
await sleep(2000);
}
this.generating = false;
}
async generate(): Promise<string | undefined> {
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<UrlPool | undefined> {
return this.pools[id] ? this.pools[id] : await this.new(id);
}
async new(id: string): Promise<UrlPool | undefined> {
let pool = new UrlPool(id);
let check = await pool.init();
if(!check) return undefined;
pool.start_generation();
this.pools[id] = pool;
return pool;
}
}