xStream Downloader updates
- Streaming-Site Auflösung für vidara.to (m3u8 via API) - HTML-Erkennung für Streaming-Seiten - Dateiname beim Speichern ohne Prefix - Speichern-Button deaktiviert nach Klick - Farbiger Fortschrittsbalken (grün/gelb/rot) - Status-Text statt ungenauer Prozentanzeige - Auto-Refresh Timeout bei View-Wechsel abgebrochen - HLS Download mit korrekten Headern (Referer, User-Agent) - Diverse Bugfixes und Verbesserungen
This commit is contained in:
@@ -515,7 +515,49 @@ def _scan_sites():
|
||||
# ============================================================================
|
||||
# 6. URL-Resolver
|
||||
# ============================================================================
|
||||
def _resolve_streaming_api(url):
|
||||
"""Löst Streaming-Sites (vidara.to etc.) auf: Extrahiert filecode und holt m3u8 von API."""
|
||||
import re, json
|
||||
from urllib.parse import urlparse
|
||||
|
||||
# Pattern für vidara.to und ähnliche: /e/<filecode>
|
||||
parsed = urlparse(url)
|
||||
path_match = re.match(r'/e/([a-zA-Z0-9]+)', parsed.path)
|
||||
|
||||
if not path_match:
|
||||
return (None, None) # Kein Streaming-Site Pattern
|
||||
|
||||
filecode = path_match.group(1)
|
||||
base_url = f"{parsed.scheme}://{parsed.netloc}"
|
||||
api_url = f"{base_url}/api/stream"
|
||||
|
||||
try:
|
||||
import requests
|
||||
resp = requests.post(api_url,
|
||||
json={"filecode": filecode, "device": "web"},
|
||||
headers={"Content-Type": "application/json", "Referer": url},
|
||||
timeout=10)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
m3u8_url = data.get('streaming_url')
|
||||
if m3u8_url:
|
||||
return (m3u8_url, None)
|
||||
return (None, 'streaming_url nicht in API-Antwort')
|
||||
except Exception as e:
|
||||
return (None, f'Streaming-API Fehler: {e}')
|
||||
|
||||
|
||||
def _resolve_url(url):
|
||||
# Erst prüfen ob es eine bekannte Streaming-Site ist
|
||||
m3u8_url, err = _resolve_streaming_api(url)
|
||||
if m3u8_url:
|
||||
return (m3u8_url, None)
|
||||
if err:
|
||||
# Streaming-Site erkannt aber Fehler
|
||||
return (None, err)
|
||||
|
||||
# Falls resolveurl nicht verfügbar, URL direkt zurückgeben
|
||||
# (viele URLs sind bereits direkte Stream-URLs)
|
||||
try:
|
||||
import resolveurl
|
||||
r = resolveurl.resolve(url)
|
||||
@@ -523,7 +565,8 @@ def _resolve_url(url):
|
||||
return (r, None)
|
||||
return (None, 'resolveurl konnte URL nicht aufloesen')
|
||||
except ImportError:
|
||||
return (None, 'resolveurl nicht verfuegbar')
|
||||
# resolveurl nicht verfügbar -> URL direkt verwenden
|
||||
return (url, None)
|
||||
except Exception as e:
|
||||
return (None, str(e))
|
||||
|
||||
@@ -566,17 +609,102 @@ class DownloadManager:
|
||||
)
|
||||
return
|
||||
with self.lock:
|
||||
self.downloads[did]['status'] = 'downloading'
|
||||
self.downloads[did]['resolved_url'] = final_url
|
||||
|
||||
# HLS/m3u8 Stream?
|
||||
if '.m3u8' in final_url.lower():
|
||||
self._run_hls(did, final_url, title)
|
||||
else:
|
||||
self._run_direct(did, final_url, title)
|
||||
|
||||
def _run_hls(self, did, url, title):
|
||||
"""HLS-Stream herunterladen mit ffmpeg"""
|
||||
import subprocess
|
||||
import shutil
|
||||
from urllib.parse import urlparse
|
||||
|
||||
with self.lock:
|
||||
self.downloads[did]['status'] = 'downloading'
|
||||
|
||||
safe = re.sub(r'[^\w\-\.]', '_', title)[:80] or 'video'
|
||||
path = self.dir / f'{did}_{safe}.mp4'
|
||||
tmp_ts = self.dir / f'{did}_{safe}.ts'
|
||||
|
||||
# Extrahiere Domain für Referer Header
|
||||
parsed = urlparse(url)
|
||||
referer = f"{parsed.scheme}://{parsed.netloc}/"
|
||||
ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36'
|
||||
|
||||
# ffmpeg Kommando mit Header für Streaming-Sites
|
||||
cmd = [
|
||||
shutil.which('ffmpeg') or 'ffmpeg',
|
||||
'-y', # overwrite
|
||||
'-headers', f'Referer: {referer}\r\nUser-Agent: {ua}',
|
||||
'-i', url,
|
||||
'-c', 'copy', # no re-encoding
|
||||
'-bsf:a', 'aac_adtstoasc', # fix audio
|
||||
'-progress', 'pipe:1',
|
||||
str(tmp_ts)
|
||||
]
|
||||
|
||||
try:
|
||||
proc = subprocess.Popen(
|
||||
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
||||
universal_newlines=True, bufsize=1
|
||||
)
|
||||
for line in proc.stdout:
|
||||
line = line.strip()
|
||||
# ffmpeg progress format: "out_time_ms=12345" or "progress=end"
|
||||
if line.startswith('progress=end'):
|
||||
break
|
||||
if '=' in line:
|
||||
key, val = line.split('=', 1)
|
||||
if key == 'out_time_ms' and val:
|
||||
# convert time to percentage (rough estimate)
|
||||
try:
|
||||
ms = int(val)
|
||||
# estimate based on 2hr max for unknown duration
|
||||
estimated_pct = min(ms / (2 * 3600 * 1000) * 100, 99)
|
||||
with self.lock:
|
||||
self.downloads[did]['progress'] = int(estimated_pct)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
proc.wait()
|
||||
if proc.returncode == 0 and tmp_ts.exists():
|
||||
tmp_ts.rename(path)
|
||||
with self.lock:
|
||||
self.downloads[did].update({
|
||||
'status': 'complete', 'progress': 100,
|
||||
'path': str(path), 'finished': time.time(),
|
||||
})
|
||||
else:
|
||||
raise Exception(f'ffmpeg exit code: {proc.returncode}')
|
||||
except Exception as e:
|
||||
# Cleanup
|
||||
if tmp_ts.exists():
|
||||
tmp_ts.unlink()
|
||||
with self.lock:
|
||||
self.downloads[did].update({
|
||||
'status': 'error', 'error': str(e),
|
||||
'finished': time.time(),
|
||||
})
|
||||
|
||||
def _run_direct(self, did, url, title):
|
||||
"""Direkter Download via requests (mp4/etc.)"""
|
||||
import requests as _req
|
||||
with self.lock:
|
||||
self.downloads[did]['status'] = 'downloading'
|
||||
|
||||
safe = re.sub(r'[^\w\-\.]', '_', title)[:80] or 'video'
|
||||
path = self.dir / f'{did}_{safe}.mp4'
|
||||
try:
|
||||
r = _req.get(
|
||||
final_url, stream=True, timeout=30,
|
||||
url, stream=True, timeout=30,
|
||||
headers={'User-Agent': 'Mozilla/5.0'}
|
||||
)
|
||||
r.raise_for_status()
|
||||
content_type = r.headers.get('content-type', '').lower()
|
||||
total = int(r.headers.get('content-length', 0))
|
||||
downloaded = 0
|
||||
with open(path, 'wb') as f:
|
||||
@@ -587,12 +715,27 @@ class DownloadManager:
|
||||
pct = int(100 * downloaded / total) if total else 0
|
||||
with self.lock:
|
||||
self.downloads[did]['progress'] = pct
|
||||
|
||||
# Prüfe ob es HTML ist (kein Video)
|
||||
if path.exists() and path.stat().st_size < 50000:
|
||||
with open(path, 'rb') as f:
|
||||
first_bytes = f.read(200)
|
||||
if b'<!DOCTYPE' in first_bytes or b'<html' in first_bytes or b'<script' in first_bytes:
|
||||
path.unlink()
|
||||
raise Exception('Streaming-Seite (HTML), kein Video. Video-Host wird nicht unterstützt.')
|
||||
|
||||
with self.lock:
|
||||
self.downloads[did].update({
|
||||
'status': 'complete', 'progress': 100,
|
||||
'path': str(path), 'finished': time.time(),
|
||||
})
|
||||
except Exception as e:
|
||||
# Cleanup
|
||||
if path.exists():
|
||||
try:
|
||||
path.unlink()
|
||||
except Exception:
|
||||
pass
|
||||
with self.lock:
|
||||
self.downloads[did].update({
|
||||
'status': 'error', 'error': str(e),
|
||||
@@ -607,6 +750,37 @@ class DownloadManager:
|
||||
with self.lock:
|
||||
return dict(self.downloads.get(did, {}))
|
||||
|
||||
def cancel(self, did):
|
||||
"""Download abbrechen"""
|
||||
with self.lock:
|
||||
d = self.downloads.get(did)
|
||||
if not d:
|
||||
return False
|
||||
if d['status'] in ('complete', 'error'):
|
||||
return False # already done
|
||||
d['status'] = 'cancelled'
|
||||
d['finished'] = time.time()
|
||||
# Kill associated thread if we can track it
|
||||
if did in self._threads:
|
||||
self._threads[did]['cancelled'] = True
|
||||
return True
|
||||
|
||||
def delete(self, did):
|
||||
"""Download aus Liste entfernen und Datei löschen"""
|
||||
with self.lock:
|
||||
d = self.downloads.get(did)
|
||||
if not d:
|
||||
return False
|
||||
# Datei löschen falls vorhanden
|
||||
p = d.get('path')
|
||||
if p and os.path.exists(p):
|
||||
try:
|
||||
os.unlink(p)
|
||||
except Exception:
|
||||
pass
|
||||
del self.downloads[did]
|
||||
return True
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 8. Flask-App und API
|
||||
@@ -952,6 +1126,11 @@ def api_downloads():
|
||||
return jsonify(DM.list())
|
||||
|
||||
|
||||
@app.route('/api/downloads/dir')
|
||||
def api_downloads_dir():
|
||||
return jsonify({'dir': str(DM.dir)})
|
||||
|
||||
|
||||
@app.route('/api/downloads/<did>')
|
||||
def api_download_info(did):
|
||||
d = DM.get(did)
|
||||
@@ -968,7 +1147,24 @@ def api_download_file(did):
|
||||
p = d.get('path')
|
||||
if not p or not os.path.exists(p):
|
||||
return jsonify({'error': 'file missing'}), 404
|
||||
return send_file(p, as_attachment=True, download_name=os.path.basename(p))
|
||||
# Sauberer Titel als Dateiname (ohne Prefix)
|
||||
safe_title = re.sub(r'[^\w\-\. ]', '_', d.get('title', 'video'))[:80]
|
||||
download_name = f"{safe_title}.mp4"
|
||||
return send_file(p, as_attachment=True, download_name=download_name)
|
||||
|
||||
|
||||
@app.route('/api/downloads/<did>', methods=['DELETE'])
|
||||
def api_download_delete(did):
|
||||
if DM.delete(did):
|
||||
return jsonify({'ok': True})
|
||||
return jsonify({'error': 'not found'}), 404
|
||||
|
||||
|
||||
@app.route('/api/downloads/<did>/cancel', methods=['POST'])
|
||||
def api_download_cancel(did):
|
||||
if DM.cancel(did):
|
||||
return jsonify({'ok': True})
|
||||
return jsonify({'error': 'cannot cancel'}), 400
|
||||
|
||||
|
||||
@app.errorhandler(Exception)
|
||||
@@ -987,14 +1183,14 @@ if __name__ == '__main__':
|
||||
print(' XBMC-konformer Plugin-Wrapper mit Browser-UI')
|
||||
print('=' * 60)
|
||||
print(f' Sites: {len(_scan_sites())} verfuegbar')
|
||||
print(f' URL: http://localhost:8769')
|
||||
print(f' URL: http://localhost:8770')
|
||||
print(f' Downloads: {DM.dir}')
|
||||
print('=' * 60)
|
||||
# SO_REUSEADDR auf Socket-Ebene aktivieren
|
||||
import socket
|
||||
from werkzeug.serving import make_server
|
||||
srv = make_server('0.0.0.0', 8769, app, threaded=True)
|
||||
srv = make_server('0.0.0.0', 8770, app, threaded=True)
|
||||
# Socket-Option: SO_REUSEADDR — erlaubt Bind an orphaned Sockets
|
||||
srv.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
print(' Server startet auf http://localhost:8769')
|
||||
print(' Server startet auf http://localhost:8770')
|
||||
srv.serve_forever()
|
||||
Reference in New Issue
Block a user