v1.5.9 (Actuel)
This commit is contained in:
+86
-34
@@ -1,18 +1,18 @@
|
||||
import { CONFIG } from '../../src/utils/config.js';
|
||||
|
||||
export const CONFIG_HYDRACKER = {
|
||||
BASE_URL: (CONFIG.HYDRACKER_URL || '').replace(/\/$/, ''), // Supprime le slash final
|
||||
API_KEY: CONFIG.HYDRACKER_API_KEY,
|
||||
TIMEOUT: CONFIG.HYDRACKER_TIMEOUT || 15000,
|
||||
get BASE_URL() { return (CONFIG.HYDRACKER_URL || '').replace(/\/$/, ''); },
|
||||
get API_KEY() { return CONFIG.HYDRACKER_API_KEY; },
|
||||
get TIMEOUT() { return CONFIG.HYDRACKER_TIMEOUT || 15000; },
|
||||
};
|
||||
|
||||
const HYDRACKER_HEADERS = {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${CONFIG_HYDRACKER.API_KEY}`,
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
|
||||
};
|
||||
|
||||
const TIMEOUT = CONFIG_HYDRACKER.TIMEOUT; // 30 secondes par défaut (configurable)
|
||||
export function getHydrackerHeaders() {
|
||||
return {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${CONFIG_HYDRACKER.API_KEY}`,
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchWithRetry(
|
||||
url: string,
|
||||
@@ -26,7 +26,7 @@ async function fetchWithRetry(
|
||||
while (true) {
|
||||
attempt++;
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), TIMEOUT);
|
||||
const timeoutId = setTimeout(() => controller.abort(), CONFIG_HYDRACKER.TIMEOUT);
|
||||
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
@@ -60,11 +60,13 @@ async function fetchWithRetry(
|
||||
}
|
||||
|
||||
export async function apiGet(urlPath: string, params: Record<string, any> = {}) {
|
||||
const qs = Object.entries(params).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&');
|
||||
let qs = Object.entries(params).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&');
|
||||
// FIX: Hydracker API returns 401 if ':' is URL-encoded as '%3A'
|
||||
qs = qs.replace(/%3A/g, ':');
|
||||
const url = `${CONFIG_HYDRACKER.BASE_URL}/api/v1/${urlPath}` + (qs ? `?${qs}` : '');
|
||||
try {
|
||||
const res = await fetchWithRetry(url, {
|
||||
headers: HYDRACKER_HEADERS
|
||||
headers: getHydrackerHeaders()
|
||||
});
|
||||
if (!res.ok) {
|
||||
console.error(`[Hydracker-API] apiGet HTTP ${res.status} on ${urlPath}`);
|
||||
@@ -82,7 +84,7 @@ export async function apiPost(urlPath: string, body: any = {}) {
|
||||
try {
|
||||
const res = await fetchWithRetry(url, {
|
||||
method: 'POST',
|
||||
headers: { ...HYDRACKER_HEADERS, 'Content-Type': 'application/json' },
|
||||
headers: { ...getHydrackerHeaders(), 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
return { status: res.status, body: await res.text() };
|
||||
@@ -93,16 +95,21 @@ export async function apiPost(urlPath: string, body: any = {}) {
|
||||
}
|
||||
|
||||
export async function fetchSearch(query: string) {
|
||||
const url = `${CONFIG_HYDRACKER.BASE_URL}/api/v1/search/${encodeURIComponent(query)}?loader=searchAutocomplete`;
|
||||
const url = `${CONFIG_HYDRACKER.BASE_URL}/api/v1/titles?query=${encodeURIComponent(query)}`;
|
||||
try {
|
||||
const res = await fetchWithRetry(url, {
|
||||
headers: HYDRACKER_HEADERS
|
||||
headers: getHydrackerHeaders()
|
||||
});
|
||||
if (!res.ok) {
|
||||
console.error(`[Hydracker-API] Search HTTP ${res.status} for "${query}"`);
|
||||
return null;
|
||||
}
|
||||
return await res.json();
|
||||
const data = await res.json();
|
||||
// Transform the new API structure to match the old expected structure
|
||||
if (data && data.pagination && Array.isArray(data.pagination.data)) {
|
||||
return { results: data.pagination.data };
|
||||
}
|
||||
return data;
|
||||
} catch (e: any) {
|
||||
console.error('[Hydracker-API] Search failed:', e.message);
|
||||
return null;
|
||||
@@ -113,7 +120,7 @@ export async function fetchMovieLinks(titleId: string) {
|
||||
const url = `${CONFIG_HYDRACKER.BASE_URL}/api/v1/titles/${titleId}/download`;
|
||||
try {
|
||||
const res = await fetchWithRetry(url, {
|
||||
headers: HYDRACKER_HEADERS
|
||||
headers: getHydrackerHeaders()
|
||||
});
|
||||
if (!res.ok) return null;
|
||||
return await res.json();
|
||||
@@ -122,22 +129,67 @@ export async function fetchMovieLinks(titleId: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchSeriesLiens(titleId: string, season: number = 1) {
|
||||
const allLiens: any[] = [];
|
||||
let page = 1;
|
||||
while (true) {
|
||||
const result = await apiGet('liens', {
|
||||
title_id: titleId, loader: 'linksdl', season,
|
||||
perPage: 500, page, filters: '', paginate: 'lengthAware'
|
||||
});
|
||||
if (!result || result.error) break;
|
||||
const pagination = result.pagination || {};
|
||||
const data = pagination.data || [];
|
||||
if (!data.length) break;
|
||||
allLiens.push(...data);
|
||||
const lastPage = pagination.last_page || pagination.lastPage || 1;
|
||||
if (page >= lastPage) break;
|
||||
page++;
|
||||
/**
|
||||
* Récupère la page de download d'un titre.
|
||||
* - Films : GET /titles/{id}/download
|
||||
* - Séries : GET /titles/{id}/season/{s}/episode/{e}/download
|
||||
*
|
||||
* Retourne l'objet complet contenant: video, alternative_videos, title.seasons, last_episode, etc.
|
||||
*/
|
||||
export async function fetchDownloadPage(titleId: string, season?: number, episode?: number) {
|
||||
let urlPath: string;
|
||||
if (season && season > 0 && episode && episode > 0) {
|
||||
urlPath = `titles/${titleId}/season/${season}/episode/${episode}/download`;
|
||||
} else if (season && season > 0) {
|
||||
// On demande le premier épisode de la saison pour obtenir les métadonnées
|
||||
urlPath = `titles/${titleId}/season/${season}/episode/1/download`;
|
||||
} else {
|
||||
urlPath = `titles/${titleId}/download`;
|
||||
}
|
||||
return await apiGet(urlPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère TOUS les liens d'une saison en itérant sur chaque épisode via /download.
|
||||
* Utilise last_episode pour savoir combien d'épisodes ont des liens.
|
||||
*/
|
||||
export async function fetchSeriesLiens(titleId: string, season: number = 1) {
|
||||
// D'abord, obtenir les métadonnées pour savoir combien d'épisodes il y a
|
||||
const firstPage = await fetchDownloadPage(titleId, season, 1);
|
||||
if (!firstPage) return [];
|
||||
|
||||
const lastEpisodeMap = firstPage.last_episode || {};
|
||||
const lastEp = lastEpisodeMap[String(season)] || 0;
|
||||
|
||||
if (lastEp === 0) return [];
|
||||
|
||||
// Collecter les liens de tous les épisodes
|
||||
const allLiens: any[] = [];
|
||||
|
||||
// Extraire les liens du premier épisode qu'on a déjà chargé
|
||||
const extractLiens = (downloadData: any) => {
|
||||
const liens: any[] = [];
|
||||
if (downloadData.video) liens.push(downloadData.video);
|
||||
if (downloadData.alternative_videos) {
|
||||
for (const av of downloadData.alternative_videos) {
|
||||
// Éviter les doublons (video est souvent dans alternative_videos aussi)
|
||||
if (!liens.find(l => l.id === av.id)) {
|
||||
liens.push(av);
|
||||
}
|
||||
}
|
||||
}
|
||||
return liens;
|
||||
};
|
||||
|
||||
allLiens.push(...extractLiens(firstPage));
|
||||
|
||||
// Charger les épisodes suivants (2 à lastEp)
|
||||
for (let ep = 2; ep <= lastEp; ep++) {
|
||||
const epData = await fetchDownloadPage(titleId, season, ep);
|
||||
if (epData) {
|
||||
allLiens.push(...extractLiens(epData));
|
||||
}
|
||||
}
|
||||
|
||||
return allLiens;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user