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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
+
from huggingface_hub import upload_file
|
| 6 |
+
|
| 7 |
+
# === CONFIGURATION ===
|
| 8 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 9 |
+
REPO_ID = "Fred808/BG1"
|
| 10 |
+
DATA_PATH = "AEffects"
|
| 11 |
+
PROCESSED_FILE = "processed.json"
|
| 12 |
+
TORRENT_SERVICE_URL = "http://localhost:8000" # Change this to your deployed service URL
|
| 13 |
+
|
| 14 |
+
# Placeholder for magnet links - these will replace the original VIDEO_URLS
|
| 15 |
+
MAGNET_LINKS = [
|
| 16 |
+
"magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce"
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
# === Load processed videos ===
|
| 20 |
+
if os.path.exists(PROCESSED_FILE):
|
| 21 |
+
with open(PROCESSED_FILE, "r") as f:
|
| 22 |
+
processed_urls = set(json.load(f))
|
| 23 |
+
else:
|
| 24 |
+
processed_urls = set()
|
| 25 |
+
|
| 26 |
+
def save_processed():
|
| 27 |
+
with open(PROCESSED_FILE, "w") as f:
|
| 28 |
+
json.dump(list(processed_urls), f, indent=2)
|
| 29 |
+
|
| 30 |
+
def upload_to_dataset(filepath, filename):
|
| 31 |
+
try:
|
| 32 |
+
upload_file(
|
| 33 |
+
path_or_fileobj=filepath,
|
| 34 |
+
path_in_repo=f"{DATA_PATH}/{filename}",
|
| 35 |
+
repo_id=REPO_ID,
|
| 36 |
+
repo_type="dataset",
|
| 37 |
+
token=HF_TOKEN
|
| 38 |
+
)
|
| 39 |
+
print(f"[↑] Uploaded: {filename}")
|
| 40 |
+
return True
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"[!] Upload failed: {filename} — {e}")
|
| 43 |
+
return False
|
| 44 |
+
|
| 45 |
+
def start_torrent_download(magnet_link):
|
| 46 |
+
"""Start a torrent download on the remote service"""
|
| 47 |
+
try:
|
| 48 |
+
response = requests.post(
|
| 49 |
+
f"{TORRENT_SERVICE_URL}/api/torrent/download",
|
| 50 |
+
json={"magnet_link": magnet_link},
|
| 51 |
+
timeout=30
|
| 52 |
+
)
|
| 53 |
+
response.raise_for_status()
|
| 54 |
+
return response.json()
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"[!] Error starting download: {e}")
|
| 57 |
+
return None
|
| 58 |
+
|
| 59 |
+
def check_download_status(download_id):
|
| 60 |
+
"""Check the status of a download"""
|
| 61 |
+
try:
|
| 62 |
+
response = requests.get(
|
| 63 |
+
f"{TORRENT_SERVICE_URL}/api/torrent/status/{download_id}",
|
| 64 |
+
timeout=30
|
| 65 |
+
)
|
| 66 |
+
response.raise_for_status()
|
| 67 |
+
return response.json()
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"[!] Error checking status: {e}")
|
| 70 |
+
return None
|
| 71 |
+
|
| 72 |
+
def get_download_files(download_id):
|
| 73 |
+
"""Get list of downloaded files"""
|
| 74 |
+
try:
|
| 75 |
+
response = requests.get(
|
| 76 |
+
f"{TORRENT_SERVICE_URL}/api/torrent/files/{download_id}",
|
| 77 |
+
timeout=30
|
| 78 |
+
)
|
| 79 |
+
response.raise_for_status()
|
| 80 |
+
return response.json()
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f"[!] Error getting files: {e}")
|
| 83 |
+
return None
|
| 84 |
+
|
| 85 |
+
def download_and_upload_file(download_id, filename):
|
| 86 |
+
"""Download a file from the service and upload to HF dataset"""
|
| 87 |
+
try:
|
| 88 |
+
# Download file from service
|
| 89 |
+
response = requests.get(
|
| 90 |
+
f"{TORRENT_SERVICE_URL}/api/torrent/download-file/{download_id}/{filename}",
|
| 91 |
+
stream=True,
|
| 92 |
+
timeout=300 # 5 minute timeout for large files
|
| 93 |
+
)
|
| 94 |
+
response.raise_for_status()
|
| 95 |
+
|
| 96 |
+
# Save temporarily and upload
|
| 97 |
+
temp_path = f"/tmp/{filename}"
|
| 98 |
+
with open(temp_path, "wb") as f:
|
| 99 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 100 |
+
f.write(chunk)
|
| 101 |
+
|
| 102 |
+
# Upload to HF dataset
|
| 103 |
+
success = upload_to_dataset(temp_path, filename)
|
| 104 |
+
|
| 105 |
+
# Clean up temp file
|
| 106 |
+
if os.path.exists(temp_path):
|
| 107 |
+
os.remove(temp_path)
|
| 108 |
+
|
| 109 |
+
return success
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f"[!] Error downloading/uploading {filename}: {e}")
|
| 112 |
+
return False
|
| 113 |
+
|
| 114 |
+
def cleanup_download(download_id):
|
| 115 |
+
"""Clean up a download on the remote service"""
|
| 116 |
+
try:
|
| 117 |
+
response = requests.delete(
|
| 118 |
+
f"{TORRENT_SERVICE_URL}/api/torrent/cleanup/{download_id}",
|
| 119 |
+
timeout=30
|
| 120 |
+
)
|
| 121 |
+
response.raise_for_status()
|
| 122 |
+
print(f"[✓] Cleaned up download {download_id}")
|
| 123 |
+
except Exception as e:
|
| 124 |
+
print(f"[!] Error cleaning up download {download_id}: {e}")
|
| 125 |
+
|
| 126 |
+
def process_magnet_link(magnet_link):
|
| 127 |
+
"""Process a single magnet link"""
|
| 128 |
+
print(f"[*] Starting download for: {magnet_link}")
|
| 129 |
+
|
| 130 |
+
# Start download
|
| 131 |
+
result = start_torrent_download(magnet_link)
|
| 132 |
+
if not result:
|
| 133 |
+
return False
|
| 134 |
+
|
| 135 |
+
download_id = result.get("download_id")
|
| 136 |
+
if not download_id:
|
| 137 |
+
print("[!] No download ID received")
|
| 138 |
+
return False
|
| 139 |
+
|
| 140 |
+
print(f"[+] Download started with ID: {download_id}")
|
| 141 |
+
|
| 142 |
+
# Wait for completion
|
| 143 |
+
max_wait_time = 1800 # 30 minutes max
|
| 144 |
+
start_time = time.time()
|
| 145 |
+
|
| 146 |
+
while time.time() - start_time < max_wait_time:
|
| 147 |
+
status = check_download_status(download_id)
|
| 148 |
+
if not status:
|
| 149 |
+
time.sleep(10)
|
| 150 |
+
continue
|
| 151 |
+
|
| 152 |
+
current_status = status.get("status")
|
| 153 |
+
print(f"[~] Status: {current_status}")
|
| 154 |
+
|
| 155 |
+
if current_status == "completed":
|
| 156 |
+
break
|
| 157 |
+
elif current_status == "error":
|
| 158 |
+
print(f"[!] Download failed: {status.get('message', 'Unknown error')}")
|
| 159 |
+
cleanup_download(download_id)
|
| 160 |
+
return False
|
| 161 |
+
|
| 162 |
+
time.sleep(10) # Check every 10 seconds
|
| 163 |
+
else:
|
| 164 |
+
print("[!] Download timed out")
|
| 165 |
+
cleanup_download(download_id)
|
| 166 |
+
return False
|
| 167 |
+
|
| 168 |
+
# Get files and upload them
|
| 169 |
+
files_info = get_download_files(download_id)
|
| 170 |
+
if not files_info:
|
| 171 |
+
cleanup_download(download_id)
|
| 172 |
+
return False
|
| 173 |
+
|
| 174 |
+
files = files_info.get("files", [])
|
| 175 |
+
if not files:
|
| 176 |
+
print("[!] No files found")
|
| 177 |
+
cleanup_download(download_id)
|
| 178 |
+
return False
|
| 179 |
+
|
| 180 |
+
print(f"[+] Found {len(files)} files")
|
| 181 |
+
|
| 182 |
+
# Download and upload each file
|
| 183 |
+
success_count = 0
|
| 184 |
+
for file_info in files:
|
| 185 |
+
filename = file_info["filename"]
|
| 186 |
+
print(f"[*] Processing file: {filename}")
|
| 187 |
+
|
| 188 |
+
if download_and_upload_file(download_id, filename):
|
| 189 |
+
success_count += 1
|
| 190 |
+
|
| 191 |
+
# Clean up remote download
|
| 192 |
+
cleanup_download(download_id)
|
| 193 |
+
|
| 194 |
+
print(f"[✓] Successfully uploaded {success_count}/{len(files)} files")
|
| 195 |
+
return success_count > 0
|
| 196 |
+
|
| 197 |
+
def main():
|
| 198 |
+
unprocessed_links = [link for link in MAGNET_LINKS if link not in processed_urls]
|
| 199 |
+
|
| 200 |
+
if not unprocessed_links:
|
| 201 |
+
print("[*] No new magnet links to process")
|
| 202 |
+
return
|
| 203 |
+
|
| 204 |
+
print(f"[*] Processing {len(unprocessed_links)} magnet links...")
|
| 205 |
+
|
| 206 |
+
for link in unprocessed_links:
|
| 207 |
+
try:
|
| 208 |
+
if process_magnet_link(link):
|
| 209 |
+
processed_urls.add(link)
|
| 210 |
+
save_processed()
|
| 211 |
+
print(f"[✓] Completed: {link}")
|
| 212 |
+
else:
|
| 213 |
+
print(f"[!] Failed: {link}")
|
| 214 |
+
except Exception as e:
|
| 215 |
+
print(f"[!] Error processing {link}: {e}")
|
| 216 |
+
|
| 217 |
+
print("[⏱] Waiting 30s before next torrent...\n")
|
| 218 |
+
time.sleep(30) # Delay between torrents
|
| 219 |
+
|
| 220 |
+
print("\n✅ Done. All torrents processed.")
|
| 221 |
+
|
| 222 |
+
if __name__ == "__main__":
|
| 223 |
+
main()
|
| 224 |
+
|