-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
163 lines (137 loc) · 5.01 KB
/
app.py
File metadata and controls
163 lines (137 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from flask import Flask, request, jsonify, send_file, render_template, abort
import yt_dlp
import hashlib
app = Flask(__name__)
DOWNLOAD_FOLDER = "tmp/downloads"
if not os.path.exists(DOWNLOAD_FOLDER):
os.makedirs(DOWNLOAD_FOLDER)
cleanup_interval = 300 # 5 minutes
file_expiry = 1800 # 30 minutes
executor = ThreadPoolExecutor(max_workers=4)
download_locks = {}
download_status = {}
def cleanup_files():
while True:
now = time.time()
for f in os.listdir(DOWNLOAD_FOLDER):
fpath = os.path.join(DOWNLOAD_FOLDER, f)
try:
stat = os.stat(fpath)
if now - stat.st_mtime > file_expiry:
os.remove(fpath)
except Exception:
pass
time.sleep(cleanup_interval)
def hash_url(url):
return hashlib.sha256(url.encode()).hexdigest()
def download_video(url, cookiefile=None):
filename = hash_url(url) + ".mp4"
filepath = os.path.join(DOWNLOAD_FOLDER, filename)
if os.path.exists(filepath):
# Update modification time for expiry reset
os.utime(filepath, None)
return filepath
# Custom options for Pornhub (low CPU, chunked download, limited concurrency)
if cookiefile and "phub_cookies.netscape" in cookiefile.lower():
ydl_opts = {
'outtmpl': output_path, # Save directly to SSD
'cookiefile': cookiefile,
'format': 'best[ext=mp4]/best', # Hardware-friendly codec
'noplaylist': True,
'quiet': False,
'no_warnings': True,
'restrictfilenames': True,
'concurrent_fragment_downloads': 3, # Limited threads = lower CPU
'retries': 3,
'http_chunk_size': 5 * 1024 * 1024, # Chunked download (less memory overhead)
'noprogress': True, # Skip fancy progress = less CPU
'nopart': True, # No .part file (less disk I/O)
}
else:
# Default options for other sites
ydl_opts = {
'outtmpl': filepath,
'format': 'best[ext=mp4]/best',
'noplaylist': True,
'quiet': True,
'no_warnings': True,
'cookiefile': cookiefile,
'nocheckcertificate': True,
'prefer_insecure': True,
'retries': 2,
'fixup': 'detect_or_warn',
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
if os.path.exists(filepath):
return filepath
except Exception as e:
print("Download error:", e)
if os.path.exists(filepath):
os.remove(filepath)
return None
@app.route("/", methods=["GET"])
def index():
return render_template("index.html")
@app.route("/download", methods=["POST"])
def download_route():
data = request.json
if not data or "url" not in data:
return jsonify({"error": "Missing URL"}), 400
url = data["url"].strip()
cookiefile = None
# For Pornhub use .netscape cookie file path
if "pornhub.com" in url.lower():
cookiefile = "phub_cookies.netscape"
if not os.path.exists(cookiefile):
return jsonify({"error": "Phub cookie file missing on server"}), 500
hash_key = hash_url(url)
# Prevent concurrent downloads of the same file
if hash_key in download_locks:
return jsonify({"status": "Downloading in progress, please wait"}), 202
# Submit download task
download_locks[hash_key] = True
def task():
path = download_video(url, cookiefile)
if path:
download_status[hash_key] = path
else:
download_status[hash_key] = None
del download_locks[hash_key]
executor.submit(task)
return jsonify({"status": "Download started, check /status endpoint", "id": hash_key})
@app.route("/status/<file_id>", methods=["GET"])
def status(file_id):
if file_id in download_status:
if download_status[file_id]:
return jsonify({"status": "ready", "download_url": f"/file/{file_id}"})
else:
return jsonify({"status": "error", "message": "Failed to download"})
else:
return jsonify({"status": "pending"})
@app.route("/file/<file_id>", methods=["GET"])
def serve_file(file_id):
if file_id not in download_status or not download_status[file_id]:
abort(404)
filepath = download_status[file_id]
if not os.path.exists(filepath):
abort(404)
def remove_file_after_send():
try:
time.sleep(10)
if os.path.exists(filepath):
os.remove(filepath)
if file_id in download_status:
del download_status[file_id]
except Exception:
pass
threading.Thread(target=remove_file_after_send).start()
return send_file(filepath, as_attachment=True)
if __name__ == "__main__":
threading.Thread(target=cleanup_files, daemon=True).start()
app.run(host="0.0.0.0", port=5000)