This commit is contained in:
2025-11-11 21:26:34 +01:00
parent 7bd2fb5b7c
commit 08be6a2455
5 changed files with 241 additions and 248 deletions

View File

@@ -1,27 +1,17 @@
# Standard library
from datetime import timedelta
from io import BytesIO
import time
import time, os
# Third-party libraries
import requests
from flask import render_template, request, redirect, url_for, session, flash, jsonify, make_response
from flask_login import logout_user, login_required, current_user
from flask import request, render_template, redirect, url_for, flash
# Project imports
from backend.utils import format_size, calculate_age
from backend.auth import authenticate_user
from backend.api import search_torrents, filter_and_format_torrents, download_torrent_ygg, upload_to_alldebrid, get_alldebrid_links
MAX_ATTEMPTS = 5
BLOCK_TIME = timedelta(minutes=15)
MAX_SIZE_BYTES = 5*1024**3
NB_PAGES = 2
YGG_PASSKEY = "xj1MgNuyzFKCjOtnawGBC2egDOciUg04"
ALLDEBRID_KEY = "mtrQI4h583rHe2ZpvpbC"
YGG_PASSKEY = os.getenv("YGG_PASSKEY")
ALLDEBRID_KEY = os.getenv("ALLDEBRID_API_KEY")
def init_app(app):
@@ -66,46 +56,11 @@ def init_app(app):
query = request.args.get('query')
category_id = request.args.get('category_id')
# Préparer l'URL
url = "https://yggapi.eu/torrents"
# 1. Recherche brute
torrents = search_torrents(query, category_id)
# Préparer les paramètres
params = {
"page": 1,
"q": query,
"order_by": "uploaded_at",
"per_page": 100
}
# Ajouter la catégorie seulement si elle est renseignée et non vide
if category_id:
params["category_id"] = category_id
results = []
# Appeler l'API
try:
for page in range(1, NB_PAGES):
params['page'] = page
print("Appel API page", page, "avec params:", params)
response = requests.get(url, params=params, timeout=5)
response.raise_for_status()
data = response.json() # ici c'est une liste directement
print(f"Nombre de torrents reçus page {page}:", len(data))
results.extend(data)
except Exception as e:
print("Erreur API Yggtorrent:", e)
results = []
filtered_results = []
for torrent in results:
if torrent['size'] <= MAX_SIZE_BYTES:
torrent['size'] = format_size(torrent['size'])
days, human = calculate_age(torrent['uploaded_at'])
torrent['age_days'] = days # to show
torrent['age_human'] = human # to sort
filtered_results.append(torrent)
# 2. Filtrage et formatage
filtered_results = filter_and_format_torrents(torrents)
return render_template(
'search_results.html',
@@ -117,124 +72,24 @@ def init_app(app):
@app.route("/torrent/<torrent_id>")
@login_required
def torrent_details(torrent_id):
# --- 1er bloc : récupération du torrent ---
try:
url = f"https://yggapi.eu/torrent/{torrent_id}/download"
params = {
"passkey": YGG_PASSKEY,
"tracker_domain": "tracker.p2p-world.net"
}
response = requests.get(url, params=params, timeout=10, allow_redirects=True)
response.raise_for_status()
torrent_file_content = response.content
torrent_filename = f"{torrent_id}.torrent"
# 1. Téléchargement depuis Ygg
torrent_content, filename = download_torrent_ygg(torrent_id)
# 2. Upload sur Alldebrid
debrid_id = upload_to_alldebrid(torrent_content, filename)
# Pause courte si nécessaire (simule un délai API)
time.sleep(2)
# 3. Récupération des liens directs
direct_links = get_alldebrid_links(debrid_id)
# Request error
except requests.exceptions.RequestException as e:
print("Erreur lors de la récupération du torrent depuis Yggtorrent:", e)
return "Erreur récupération torrent", 500
print("Erreur HTTP:", e)
return "Erreur lors de la communication avec l'API", 500
except RuntimeError as e:
print(e)
return str(e), 500
# --- 2e bloc : upload à Alldebrid ---
try:
files = {'files[]': (torrent_filename, BytesIO(torrent_file_content))}
headers = {"Authorization": f"Bearer {ALLDEBRID_KEY}"}
upload_request = requests.post(
"https://api.alldebrid.com/v4/magnet/upload/file",
headers=headers,
files=files,
timeout=20
)
upload_request.raise_for_status()
first_answer = upload_request.json()
debrid_id = first_answer["data"]["files"][0]["id"]
# API error
if first_answer["status"] != "success":
print("API error 1 : upload Alldebrid:", first_answer)
return "API error 1 : upload Alldebrid", 500
# Request error
except requests.exceptions.RequestException as e:
print("Request error 1 : upload Alldebrid:", e)
return "Request error 1 : upload Alldebrid:", 500
print("Alldebrid id:",debrid_id)
time.sleep(2)
# --- 3e bloc : second appel Alldebrid ---
try:
headers = {"Authorization": f"Bearer {ALLDEBRID_KEY}"}
alldebrid_link_request = requests.get(
"https://api.alldebrid.com/v4/magnet/files",
headers=headers,
params={"id[]": debrid_id},
timeout=10
)
alldebrid_link_request.raise_for_status()
second_answer = alldebrid_link_request.json()
# API error
if second_answer.get("status") != "success":
print("API error 2 : get Alldebrid link:", second_answer)
return "API error 2 : get Alldebrid link", 500
alldebrid_links = []
for magnet in second_answer.get("data", {}).get("magnets", []):
for file in magnet.get("files", []):
# Cas où il y a une liste "e"
if "e" in file:
for entry in file["e"]:
if "l" in entry:
alldebrid_links.append(entry["l"])
# Cas où le lien est directement dans "l"
elif "l" in file:
alldebrid_links.append(file["l"])
# Request error
except requests.exceptions.RequestException as e:
print("Request error 2 : get Alldebrid link:", e)
return "Request error 2 : get Alldebrid link", 500
print("Alldebrid links:",alldebrid_links)
# --- 4e bloc : troisieme appel Alldebrid ---
direct_links = []
try:
headers = {"Authorization": f"Bearer {ALLDEBRID_KEY}"}
for link in alldebrid_links:
direct_link_request = requests.get(
"https://api.alldebrid.com/v4/link/unlock",
headers=headers,
params={"link": link},
timeout=10
)
direct_link_request.raise_for_status()
third_anwser = direct_link_request.json()
# API error
if third_anwser.get("status") != "success":
print("API error 3 : get direct link:", third_anwser)
return "API error 3 : get direct link", 500
# Récupérer les liens directs
direct_links.append({
"name": third_anwser["data"]["filename"],
"size": third_anwser["data"]["filesize"],
"link": third_anwser["data"]["link"]
})
# Request error
except requests.exceptions.RequestException as e:
print("API error 3 : get direct link:", e)
return "API error 3 : get direct link", 500
print("Direct links:",direct_links)
# 3. Retour du lien direct au client
return jsonify(direct_links)