fix: stream direct site videos
This commit is contained in:
@@ -286,6 +286,30 @@ class TelegramStorageUploader:
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
async def download_http_video(
|
||||
self,
|
||||
session: aiohttp.ClientSession,
|
||||
url: str,
|
||||
output_path: str,
|
||||
) -> dict:
|
||||
max_size = self.video_max_size_mb * 1024 * 1024
|
||||
try:
|
||||
async with session.get(url, timeout=aiohttp.ClientTimeout(total=self.download_timeout_sec)) as response:
|
||||
if response.status != 200:
|
||||
return {"error": f"video returned HTTP {response.status}", "permanent": False}
|
||||
size = 0
|
||||
with open(output_path, "wb") as fh:
|
||||
async for chunk in response.content.iter_chunked(1024 * 1024):
|
||||
size += len(chunk)
|
||||
if size > max_size:
|
||||
remove_download_files(output_path)
|
||||
return {"error": "video too large", "permanent": True}
|
||||
fh.write(chunk)
|
||||
except Exception:
|
||||
remove_download_files(output_path)
|
||||
return {"error": "video download failed", "permanent": False}
|
||||
return {"path": output_path, "size_bytes": size}
|
||||
|
||||
async def mark_media_uploaded(self, media_id: int, file_id: str, unique_id: str | None) -> None:
|
||||
await self.pool.execute(
|
||||
"""
|
||||
@@ -435,7 +459,11 @@ class TelegramStorageUploader:
|
||||
await self.mark_media_link_only(media_id, "video too long")
|
||||
continue
|
||||
temp_path = str(TMP_DIR / f"{TMP_PREFIX}{raw_post_id}_{media_id}.mp4")
|
||||
info = await self.download_video(url, temp_path)
|
||||
info = (
|
||||
await self.download_video(url, temp_path)
|
||||
if video_provider(url)
|
||||
else await self.download_http_video(session, url, temp_path)
|
||||
)
|
||||
if not info:
|
||||
await self.mark_media_failed_attempt(media_id, "video download failed")
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user