Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import threading
|
| 4 |
+
import shutil
|
| 5 |
+
import logging
|
| 6 |
+
import requests
|
| 7 |
+
from flask import Flask, request, jsonify
|
| 8 |
+
from huggingface_hub import HfApi, HfFileSystem
|
| 9 |
+
from mega import Mega
|
| 10 |
+
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
+
logging.basicConfig(level=logging.INFO)
|
| 13 |
+
|
| 14 |
+
DATASET_REPO = os.environ.get("DATASET_REPO")
|
| 15 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 16 |
+
RENDER_URL = os.environ.get("RENDER_URL")
|
| 17 |
+
ACCOUNTS_FILE = "accounts.txt"
|
| 18 |
+
|
| 19 |
+
api = HfApi(token=HF_TOKEN)
|
| 20 |
+
fs = HfFileSystem(token=HF_TOKEN)
|
| 21 |
+
|
| 22 |
+
def restart_space():
|
| 23 |
+
"""IP Ban yiyince Space'i yeniden başlatır (Yeni IP için)"""
|
| 24 |
+
print("♻️ IP BANLANDI! SPACE YENİDEN BAŞLATILIYOR (YENİ IP İÇİN)...", flush=True)
|
| 25 |
+
api.restart_space(repo_id=os.environ.get("SPACE_ID")) # Kendi kendini resetler
|
| 26 |
+
|
| 27 |
+
def get_accounts():
|
| 28 |
+
if not os.path.exists(ACCOUNTS_FILE): return []
|
| 29 |
+
with open(ACCOUNTS_FILE, 'r') as f:
|
| 30 |
+
return [line.strip() for line in f if ':' in line and len(line.strip()) > 5]
|
| 31 |
+
|
| 32 |
+
def report_to_render(status, message, task_id, download_url=None):
|
| 33 |
+
print(f"📡 RENDER RAPOR: {status} - {message}", flush=True)
|
| 34 |
+
if not RENDER_URL: return
|
| 35 |
+
try:
|
| 36 |
+
requests.post(f"{RENDER_URL}/webhook", json={
|
| 37 |
+
'status': status, 'message': message, 'task_id': task_id, 'download_url': download_url
|
| 38 |
+
}, timeout=10)
|
| 39 |
+
except: pass
|
| 40 |
+
|
| 41 |
+
def download_engine(link, task_id):
|
| 42 |
+
print(f"\n🚀 WORKER BAŞLADI (SMART MODE): {task_id}", flush=True)
|
| 43 |
+
|
| 44 |
+
base_dir = "downloads"
|
| 45 |
+
download_folder = os.path.abspath(f"{base_dir}/{task_id}")
|
| 46 |
+
zip_file = os.path.abspath(f"{base_dir}/{task_id}.zip")
|
| 47 |
+
|
| 48 |
+
if os.path.exists(download_folder): shutil.rmtree(download_folder)
|
| 49 |
+
os.makedirs(download_folder, exist_ok=True)
|
| 50 |
+
|
| 51 |
+
accounts = get_accounts()
|
| 52 |
+
mega = Mega()
|
| 53 |
+
is_completed = False
|
| 54 |
+
|
| 55 |
+
for i, account in enumerate(accounts):
|
| 56 |
+
if is_completed: break
|
| 57 |
+
|
| 58 |
+
email, password = account.split(":", 1)
|
| 59 |
+
print(f"\n🔄 --- HESAP {i+1} / {len(accounts)} ---", flush=True)
|
| 60 |
+
report_to_render('processing', f'İndiriliyor (Hesap {i+1})...', task_id)
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
m = mega.login(email, password)
|
| 64 |
+
print(f"📥 İndiriliyor: {link}", flush=True)
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
m.download_url(link, download_folder)
|
| 68 |
+
|
| 69 |
+
if len(os.listdir(download_folder)) > 0:
|
| 70 |
+
print("✅ İNDİRME TAMAMLANDI!", flush=True)
|
| 71 |
+
is_completed = True
|
| 72 |
+
break
|
| 73 |
+
else:
|
| 74 |
+
print("⚠️ Dosya inmedi.", flush=True)
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
err = str(e).lower()
|
| 78 |
+
print(f"❌ Hata: {err}", flush=True)
|
| 79 |
+
|
| 80 |
+
# EN ÖNEMLİ KISIM BURASI
|
| 81 |
+
# Eğer Mega "Bandwidth Limit" veya "API Limit" derse
|
| 82 |
+
# Demek ki IP Ban yedik.
|
| 83 |
+
if "bandwidth" in err or "quota" in err or "temporary" in err:
|
| 84 |
+
print("🚨 IP BAN TESPİT EDİLDİ! KOMPLE RESET ATILIYOR...", flush=True)
|
| 85 |
+
restart_space() # Kendi kendini öldür ve yeni IP ile doğ
|
| 86 |
+
return # Fonksiyondan çık, zaten restart geliyor
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f"🔥 Giriş Hatası: {e}", flush=True)
|
| 90 |
+
continue
|
| 91 |
+
|
| 92 |
+
if not is_completed:
|
| 93 |
+
report_to_render('error', 'İndirme Başarısız', task_id)
|
| 94 |
+
shutil.rmtree(download_folder, ignore_errors=True)
|
| 95 |
+
return
|
| 96 |
+
|
| 97 |
+
# ZIPLEME
|
| 98 |
+
print("📦 Paketleniyor...", flush=True)
|
| 99 |
+
report_to_render('processing', 'Paketleniyor...', task_id)
|
| 100 |
+
shutil.make_archive(f"{base_dir}/{task_id}", 'zip', download_folder)
|
| 101 |
+
|
| 102 |
+
# UPLOAD
|
| 103 |
+
print("📤 Yükleniyor...", flush=True)
|
| 104 |
+
report_to_render('uploading', 'Buluta Yükleniyor...', task_id)
|
| 105 |
+
try:
|
| 106 |
+
api.upload_file(
|
| 107 |
+
path_or_fileobj=zip_file,
|
| 108 |
+
repo_id=DATASET_REPO,
|
| 109 |
+
repo_type="dataset",
|
| 110 |
+
path_in_repo=f"uploads/{task_id}.zip"
|
| 111 |
+
)
|
| 112 |
+
final_link = f"https://huggingface.co/datasets/{DATASET_REPO}/resolve/main/uploads/{task_id}.zip?download=true"
|
| 113 |
+
report_to_render('success', 'Hazır!', task_id, download_url=final_link)
|
| 114 |
+
except Exception as e:
|
| 115 |
+
report_to_render('error', f'Hata: {e}', task_id)
|
| 116 |
+
|
| 117 |
+
shutil.rmtree(download_folder, ignore_errors=True)
|
| 118 |
+
if os.path.exists(zip_file): os.remove(zip_file)
|
| 119 |
+
|
| 120 |
+
@app.route('/process', methods=['POST'])
|
| 121 |
+
def process():
|
| 122 |
+
data = request.json
|
| 123 |
+
threading.Thread(target=download_engine, args=(data['link'], data['task_id'])).start()
|
| 124 |
+
return jsonify({'status': 'started'})
|
| 125 |
+
|
| 126 |
+
@app.route('/')
|
| 127 |
+
def health(): return "Worker V23 (Auto-Restart IP) 🟢", 200
|
| 128 |
+
|
| 129 |
+
if __name__ == '__main__':
|
| 130 |
+
app.run(host='0.0.0.0', port=7860)
|