Skip to content

Commit

Permalink
Merge pull request #1440 from Shisuiicaro/Feat/Mediafire-Download
Browse files Browse the repository at this point in the history
Fix: Fix Mediafire hoster 403
  • Loading branch information
thegrannychaseroperation authored Feb 1, 2025
2 parents 260fc46 + d017b0c commit 5661468
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions src/main/services/hosters/mediafire.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
import axios, { AxiosResponse } from "axios";
import { JSDOM } from "jsdom";
import fetch from "node-fetch";

export class MediafireApi {
private static readonly session = axios.create();
private static readonly validMediafireIdentifierDL = /^[a-zA-Z0-9]+$/m;
private static readonly validMediafirePreDL =
/(?<=['"])(https?:)?(\/\/)?(www\.)?mediafire\.com\/(file|view|download)\/[^'"?]+\?dkey=[^'"]+(?=['"])/;
private static readonly validDynamicDL =
/(?<=['"])https?:\/\/download\d+\.mediafire\.com\/[^'"]+(?=['"])/;
private static readonly checkHTTP = /^https?:\/\//m;

public static async getDownloadUrl(mediafireUrl: string): Promise<string> {
const response: AxiosResponse<string> = await this.session.get(
mediafireUrl,
{
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
},
maxRedirects: 0,
validateStatus: (status: number) => status === 200 || status === 302,
}
);

if (response.status === 302) {
const location = response.headers["location"];
if (!location) {
throw new Error("Missing location header in 302 redirect response");
}
return location;
try {
const processedUrl = this.processUrl(mediafireUrl);
const response = await fetch(processedUrl);

if (!response.ok) throw new Error("Failed to fetch Mediafire page");

const html = await response.text();
return this.extractDirectUrl(html);
} catch (error) {
throw new Error(`Failed to get download URL`);
}
}

private static processUrl(url: string): string {
let processed = url.replace("http://", "https://");

if (this.validMediafireIdentifierDL.test(processed)) {
processed = `https://mediafire.com/?${processed}`;
}

const dom = new JSDOM(response.data);
const downloadButton = dom.window.document.querySelector(
"a#downloadButton"
) as HTMLAnchorElement;
if (!this.checkHTTP.test(processed)) {
processed = processed.startsWith("//")
? `https:${processed}`
: `https://${processed}`;
}

return processed;
}

private static extractDirectUrl(html: string): string {
const preMatch = this.validMediafirePreDL.exec(html);
if (preMatch?.[0]) {
return preMatch[0];
}

if (!downloadButton?.href) {
throw new Error("Download button URL not found in page content");
const dlMatch = this.validDynamicDL.exec(html);
if (dlMatch?.[0]) {
return dlMatch[0];
}

return downloadButton.href;
throw new Error("No valid download links found");
}
}

0 comments on commit 5661468

Please sign in to comment.