Spaces:
Runtime error
Runtime error
Create uploader.py
Browse files- bot/youtube/uploader.py +25 -0
bot/youtube/uploader.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# PATH: bot/youtube/uploader.py
|
| 2 |
+
from typing import Callable, Awaitable
|
| 3 |
+
from bot.youtube.resume import start_resumable_session, upload_resumable
|
| 4 |
+
from bot.youtube.metadata import build_metadata
|
| 5 |
+
|
| 6 |
+
ProgressCB = Callable[[int, int], Awaitable[None]]
|
| 7 |
+
|
| 8 |
+
async def upload_video(
|
| 9 |
+
access_token: str,
|
| 10 |
+
file_path: str,
|
| 11 |
+
title: str,
|
| 12 |
+
description: str,
|
| 13 |
+
privacy: str = "private",
|
| 14 |
+
progress_cb: ProgressCB | None = None,
|
| 15 |
+
) -> str:
|
| 16 |
+
"""
|
| 17 |
+
Returns youtube video URL.
|
| 18 |
+
"""
|
| 19 |
+
meta = build_metadata(title=title, description=description, privacy=privacy)
|
| 20 |
+
upload_url = await start_resumable_session(access_token, meta)
|
| 21 |
+
resp = await upload_resumable(upload_url, file_path, access_token, progress_cb=progress_cb)
|
| 22 |
+
vid = resp.get("id")
|
| 23 |
+
if not vid:
|
| 24 |
+
raise RuntimeError("upload_ok_but_no_video_id")
|
| 25 |
+
return f"https://youtu.be/{vid}"
|