Added direct link support

This commit is contained in:
Filip Znachor 2022-04-23 01:37:41 +02:00
parent 0eda7a506d
commit 84d4038c6d
3 changed files with 63 additions and 22 deletions

View file

@ -13,7 +13,7 @@ export class Links {
inst = axios.create({
httpAgent: this.tor.httpAgent(),
httpsAgent: this.tor.httpsAgent(),
httpsAgent: this.tor.httpsAgent()
});
parse_cookie(cookies: string[] | undefined) {
@ -35,10 +35,22 @@ export class Links {
return match![group] ? match![group] : undefined;
}
async link(id: string) {
async direct_link(id: string): Promise<string | undefined> {
let download_link = "https://uloz.to/download-dialog/free/download?fileSlug="+id;
let captcha_page = await this.inst.get(download_link);
let redirect = await this.inst.get(download_link, {maxRedirects: 0, validateStatus: null});
this.tor.torNewSession();
if(redirect.headers.location && redirect.headers.location.startsWith("https://download.uloz.to"))
return redirect.headers.location;
}
async captcha_link(id: string): Promise<string | undefined> {
let download_link = "https://uloz.to/download-dialog/free/download?fileSlug="+id;
let captcha_page = await this.inst.get(download_link, {maxRedirects: 0});
if(captcha_page.status !== 200) throw new Error("Status code is not 200");
let cookies = this.parse_cookie(captcha_page.headers["set-cookie"]);
let url = this.regex_parse(/<img class="xapca-image" src="([^"]*)" alt="">/gm, captcha_page.data, 1);

View file

@ -1,3 +1,4 @@
import axios from "axios";
import { Downloader } from "./downloader";
import { Links } from "./links";
@ -9,16 +10,25 @@ function sleep(ms: number) {
export class UrlPool {
urls: string[];
id: string;
is_direct = false;
urls: string[] = [];
used: boolean[] = [];
generating = false;
downloader?: Downloader;
valid = false;
constructor(urls?: string[]) {
this.urls = urls ? urls : [];
for(let i=0; i<this.urls.length; i++) {
this.used[i] = 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);
return true;
}
get_raw(): [number, string] | undefined {
@ -62,19 +72,26 @@ export class UrlPool {
return count ? available.slice(0, count) : available;
}
async generate(id: string) {
async start_generation() {
this.generating = true;
while(this.urls.length < 10) {
let link = await links.link(id);
if(link) this.add(link);
console.log(link);
this.generate();
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);
console.log(link);
return link;
} catch {
return undefined;
}
}
get_downloader() {
if(this.downloader) return this.downloader;
this.downloader = new Downloader(this)
@ -87,13 +104,15 @@ export class UrlPoolStorage {
pools: {[U: string]: UrlPool} = {};
get(id: string) {
return this.pools[id] ? this.pools[id] : this.new(id);
async get(id: string): Promise<UrlPool | undefined> {
return this.pools[id] ? this.pools[id] : await this.new(id);
}
new(id: string) {
let pool = new UrlPool;
pool.generate(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;
}

View file

@ -17,7 +17,12 @@ export class Webserver {
app.get("/status/:id", async (req, res) => {
let p = storage.get(req.params.id);
let p = await storage.get(req.params.id);
if(!p) {
res.status(404);
res.end();
return;
}
res.write(JSON.stringify({
streams: {
@ -32,7 +37,12 @@ export class Webserver {
app.get("/stream/:id", async (req, res) => {
let p = storage.get(req.params.id);
let p = await storage.get(req.params.id);
if(!p) {
res.status(404);
res.end();
return;
}
let d = p.get_downloader();
let range: Range = {from: 0, to: null};