forked from zovos/bot_tg
попытка починить и оптимизировать shorties (#35)
This commit is contained in:
commit
b0e03f8e23
3 changed files with 55 additions and 6 deletions
|
|
@ -10,6 +10,7 @@ import logging
|
|||
import os
|
||||
import hashlib
|
||||
from urllib.parse import quote
|
||||
from urllib.parse import urljoin
|
||||
from urllib.parse import urlparse
|
||||
import sys
|
||||
from contextlib import asynccontextmanager
|
||||
|
|
@ -497,9 +498,52 @@ async def proxy_shorties_media(url: str, request: Request):
|
|||
raise HTTPException(status_code=resp.status, detail="Upstream error")
|
||||
|
||||
content_type = resp.headers.get("Content-Type", "application/octet-stream")
|
||||
|
||||
if ".m3u8" in parsed.path.lower() or "mpegurl" in content_type.lower():
|
||||
try:
|
||||
body = await resp.text()
|
||||
finally:
|
||||
await resp.release()
|
||||
await session.close()
|
||||
|
||||
def _proxy_media_url(target_url: str) -> str:
|
||||
return f"/api/proxy-shorties-media?url={quote(target_url, safe='')}"
|
||||
|
||||
rewritten_lines: list[str] = []
|
||||
for raw_line in body.splitlines():
|
||||
stripped = raw_line.strip()
|
||||
if not stripped:
|
||||
rewritten_lines.append(raw_line)
|
||||
continue
|
||||
|
||||
if stripped.startswith("#EXT-X-KEY") and 'URI="' in raw_line:
|
||||
prefix, rest = raw_line.split('URI="', 1)
|
||||
original_uri, suffix = rest.split('"', 1)
|
||||
absolute_uri = urljoin(url, original_uri)
|
||||
rewritten_lines.append(f'{prefix}URI="{_proxy_media_url(absolute_uri)}"{suffix}')
|
||||
continue
|
||||
|
||||
if stripped.startswith("#"):
|
||||
rewritten_lines.append(raw_line)
|
||||
continue
|
||||
|
||||
absolute_uri = urljoin(url, stripped)
|
||||
rewritten_lines.append(_proxy_media_url(absolute_uri))
|
||||
|
||||
playlist_body = "\n".join(rewritten_lines)
|
||||
return Response(
|
||||
content=playlist_body,
|
||||
media_type=content_type,
|
||||
headers={
|
||||
"Cache-Control": "public, max-age=120",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
},
|
||||
)
|
||||
|
||||
passthrough_headers = {
|
||||
"Cache-Control": "public, max-age=600",
|
||||
"Accept-Ranges": resp.headers.get("Accept-Ranges", "bytes"),
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
}
|
||||
if resp.headers.get("Content-Length"):
|
||||
passthrough_headers["Content-Length"] = resp.headers["Content-Length"]
|
||||
|
|
|
|||
|
|
@ -880,10 +880,12 @@ async function loadFurtokPage(feedEl) {
|
|||
} else {
|
||||
const poster = post.sample ? ` poster="${post.sample}"` : '';
|
||||
const directMp4 = post.mp4_url || post.url || '';
|
||||
const hasHls = isShorties && Boolean(post.hls_url);
|
||||
const videoSrc = hasHls ? '' : (isShorties ? API.proxyShortiesMediaUrl(directMp4) : post.url);
|
||||
const hlsSrc = isShorties ? escapeHtml(post.hls_url || '') : '';
|
||||
mediaHtml = `<video src="${videoSrc}" data-direct-src="${escapeHtml(directMp4)}" data-hls-src="${hlsSrc}"${poster} loop autoplay playsinline preload="auto" muted></video>`;
|
||||
const proxyMp4 = isShorties ? API.proxyShortiesMediaUrl(directMp4) : post.url;
|
||||
const proxyHls = isShorties && post.hls_url ? API.proxyShortiesMediaUrl(post.hls_url) : '';
|
||||
const useHls = isShorties && !directMp4 && Boolean(post.hls_url);
|
||||
const videoSrc = useHls ? '' : proxyMp4;
|
||||
const hlsSrc = isShorties ? escapeHtml(proxyHls) : '';
|
||||
mediaHtml = `<video src="${videoSrc}" data-direct-src="${escapeHtml(directMp4)}" data-proxy-src="${escapeHtml(proxyMp4)}" data-hls-src="${hlsSrc}"${poster} loop autoplay playsinline preload="metadata" muted></video>`;
|
||||
}
|
||||
|
||||
const title = post.title ? escapeHtml(post.title) : '';
|
||||
|
|
@ -918,8 +920,8 @@ async function loadFurtokPage(feedEl) {
|
|||
initShortiesVideoPlayback(video);
|
||||
}
|
||||
video.addEventListener('error', () => {
|
||||
if (isShorties && video.dataset.directSrc && video.src !== video.dataset.directSrc) {
|
||||
video.src = video.dataset.directSrc;
|
||||
if (isShorties && video.dataset.proxySrc && video.src !== video.dataset.proxySrc) {
|
||||
video.src = video.dataset.proxySrc;
|
||||
video.load();
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -40,6 +40,9 @@ server {
|
|||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Telegram-Init-Data $http_x_telegram_init_data;
|
||||
proxy_buffering off;
|
||||
proxy_request_buffering off;
|
||||
proxy_read_timeout 300s;
|
||||
}
|
||||
|
||||
location / {
|
||||
|
|
|
|||
Loading…
Reference in a new issue