Added direct link support
This commit is contained in:
parent
0eda7a506d
commit
84d4038c6d
18
links.ts
18
links.ts
|
@ -13,7 +13,7 @@ export class Links {
|
||||||
|
|
||||||
inst = axios.create({
|
inst = axios.create({
|
||||||
httpAgent: this.tor.httpAgent(),
|
httpAgent: this.tor.httpAgent(),
|
||||||
httpsAgent: this.tor.httpsAgent(),
|
httpsAgent: this.tor.httpsAgent()
|
||||||
});
|
});
|
||||||
|
|
||||||
parse_cookie(cookies: string[] | undefined) {
|
parse_cookie(cookies: string[] | undefined) {
|
||||||
|
@ -35,10 +35,22 @@ export class Links {
|
||||||
return match![group] ? match![group] : undefined;
|
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 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 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);
|
let url = this.regex_parse(/<img class="xapca-image" src="([^"]*)" alt="">/gm, captcha_page.data, 1);
|
||||||
|
|
53
urlpool.ts
53
urlpool.ts
|
@ -1,3 +1,4 @@
|
||||||
|
import axios from "axios";
|
||||||
import { Downloader } from "./downloader";
|
import { Downloader } from "./downloader";
|
||||||
import { Links } from "./links";
|
import { Links } from "./links";
|
||||||
|
|
||||||
|
@ -9,16 +10,25 @@ function sleep(ms: number) {
|
||||||
|
|
||||||
export class UrlPool {
|
export class UrlPool {
|
||||||
|
|
||||||
urls: string[];
|
id: string;
|
||||||
|
is_direct = false;
|
||||||
|
urls: string[] = [];
|
||||||
used: boolean[] = [];
|
used: boolean[] = [];
|
||||||
generating = false;
|
generating = false;
|
||||||
downloader?: Downloader;
|
downloader?: Downloader;
|
||||||
|
valid = false;
|
||||||
|
|
||||||
constructor(urls?: string[]) {
|
constructor(id: string) {
|
||||||
this.urls = urls ? urls : [];
|
this.id = id;
|
||||||
for(let i=0; i<this.urls.length; i++) {
|
}
|
||||||
this.used[i] = false;
|
|
||||||
}
|
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 {
|
get_raw(): [number, string] | undefined {
|
||||||
|
@ -62,19 +72,26 @@ export class UrlPool {
|
||||||
return count ? available.slice(0, count) : available;
|
return count ? available.slice(0, count) : available;
|
||||||
}
|
}
|
||||||
|
|
||||||
async generate(id: string) {
|
async start_generation() {
|
||||||
this.generating = true;
|
this.generating = true;
|
||||||
while(this.urls.length < 10) {
|
while(this.urls.length < 10) {
|
||||||
|
this.generate();
|
||||||
let link = await links.link(id);
|
|
||||||
if(link) this.add(link);
|
|
||||||
console.log(link);
|
|
||||||
await sleep(2000);
|
await sleep(2000);
|
||||||
|
|
||||||
}
|
}
|
||||||
this.generating = false;
|
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() {
|
get_downloader() {
|
||||||
if(this.downloader) return this.downloader;
|
if(this.downloader) return this.downloader;
|
||||||
this.downloader = new Downloader(this)
|
this.downloader = new Downloader(this)
|
||||||
|
@ -87,13 +104,15 @@ export class UrlPoolStorage {
|
||||||
|
|
||||||
pools: {[U: string]: UrlPool} = {};
|
pools: {[U: string]: UrlPool} = {};
|
||||||
|
|
||||||
get(id: string) {
|
async get(id: string): Promise<UrlPool | undefined> {
|
||||||
return this.pools[id] ? this.pools[id] : this.new(id);
|
return this.pools[id] ? this.pools[id] : await this.new(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
new(id: string) {
|
async new(id: string): Promise<UrlPool | undefined> {
|
||||||
let pool = new UrlPool;
|
let pool = new UrlPool(id);
|
||||||
pool.generate(id);
|
let check = await pool.init();
|
||||||
|
if(!check) return undefined;
|
||||||
|
pool.start_generation();
|
||||||
this.pools[id] = pool;
|
this.pools[id] = pool;
|
||||||
return pool;
|
return pool;
|
||||||
}
|
}
|
||||||
|
|
14
webserver.ts
14
webserver.ts
|
@ -17,7 +17,12 @@ export class Webserver {
|
||||||
|
|
||||||
app.get("/status/:id", async (req, res) => {
|
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({
|
res.write(JSON.stringify({
|
||||||
streams: {
|
streams: {
|
||||||
|
@ -32,7 +37,12 @@ export class Webserver {
|
||||||
|
|
||||||
app.get("/stream/:id", async (req, res) => {
|
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 d = p.get_downloader();
|
||||||
|
|
||||||
let range: Range = {from: 0, to: null};
|
let range: Range = {from: 0, to: null};
|
||||||
|
|
Reference in a new issue