-
Notifications
You must be signed in to change notification settings - Fork 3
/
subtitles-sender.py
executable file
·47 lines (40 loc) · 1.43 KB
/
subtitles-sender.py
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
#!/usr/bin/env python3
from __future__ import annotations
import sys
from datetime import datetime
from itertools import count
from time import sleep
from click import command, option
from loguru import logger
from requests import RequestException, Session
URL = "https://upload.youtube.com/closedcaption"
@command(context_settings={"max_content_width": 120})
@option("-c", "--cid", required=True, show_envvar=True)
@option("-f", "--datetime-format", default="%H:%M:%S", required=True, show_envvar=True, show_default=True)
def main(*, cid: str, datetime_format: str):
"""Sends the current date and time to YouTube in closed captions."""
logger.info("Starting…")
session = Session()
for seq in count(start=1):
caption = f"{datetime.utcnow().isoformat(timespec='milliseconds')}\n{datetime.now():{datetime_format}}\n"
with session.post(
URL,
headers={"Content-Type": "text/plain"},
data=caption.encode("utf-8"),
params={"cid": cid, "seq": seq},
) as response:
try:
response.raise_for_status()
except RequestException:
logger.error("{}", response.text)
sleep(1.0)
if __name__ == "__main__":
logger.remove()
logger.add(
sys.stderr,
colorize=True,
format="<level>{message}</level>",
backtrace=True,
diagnose=True,
)
main(auto_envvar_prefix="STREAM_KEEPER")