|
| 1 | +from datetime import datetime |
| 2 | +from pathlib import Path |
| 3 | +from typing import List, Optional, Self |
| 4 | + |
| 5 | +from youtool import YouTube |
| 6 | + |
| 7 | +from .base import Command |
| 8 | + |
| 9 | + |
| 10 | +class VideoLiveChat(Command): |
| 11 | + """Get live chat comments from a video ID, generate CSV output (same schema for chat_message dicts)""" |
| 12 | + |
| 13 | + name = "video-livechat" |
| 14 | + arguments = [ |
| 15 | + {"name": "--ids", "type": str, "help": "Video ID", "required": True}, |
| 16 | + {"name": "--output-file-path", "type": Path, "help": "Output CSV file path"}, |
| 17 | + {"name": "--expand-emojis", "action": "store_true", "help": "Expand emojis in chat messages"}, |
| 18 | + ] |
| 19 | + |
| 20 | + CHAT_COLUMNS: List[str] = [ |
| 21 | + "id", |
| 22 | + "video_id", |
| 23 | + "created_at", |
| 24 | + "type", |
| 25 | + "action", |
| 26 | + "video_time", |
| 27 | + "author", |
| 28 | + "author_id", |
| 29 | + "author_image_url", |
| 30 | + "text", |
| 31 | + "money_currency", |
| 32 | + "money_amount", |
| 33 | + ] |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def parse_timestamp(timestamp: str) -> str: |
| 37 | + try: |
| 38 | + return datetime.fromisoformat(timestamp.replace("Z", "")).strftime("%Y-%m-%d %H:%M:%S") |
| 39 | + except Exception: |
| 40 | + return timestamp |
| 41 | + |
| 42 | + @staticmethod |
| 43 | + def parse_decimal(value: Optional[str]) -> Optional[float]: |
| 44 | + if value is None: |
| 45 | + return None |
| 46 | + try: |
| 47 | + return float(str(value).replace(",", "")) |
| 48 | + except Exception: |
| 49 | + return None |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def execute(cls: Self, **kwargs) -> str: |
| 53 | + """ |
| 54 | + Execute the video-livechat command to fetch live chat messages from a YouTube video and save them to a CSV file. |
| 55 | +
|
| 56 | + - a YouTube video ID (`--ids`). |
| 57 | +
|
| 58 | + Args: |
| 59 | + ids (str): The ID of the YouTube video. |
| 60 | + output_file_path (Path): Path to the output CSV file where chat messages will be saved. |
| 61 | + expand_emojis (bool): Whether to expand emojis in chat messages. Defaults to True. |
| 62 | + api_key (str): The API key to authenticate with the YouTube Data API. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + A message indicating the result of the command. If output_file_path is specified, |
| 66 | + the message will include the path to the generated CSV file. |
| 67 | + Otherwise, it will return the result as a string. |
| 68 | + """ |
| 69 | + ids = kwargs.get("ids") |
| 70 | + output_file_path = kwargs.get("output_file_path") |
| 71 | + expand_emojis = kwargs.get("expand_emojis", True) |
| 72 | + api_key = kwargs.get("api_key") |
| 73 | + |
| 74 | + youtube = YouTube([api_key], disable_ipv6=True) |
| 75 | + |
| 76 | + chat_messages = list(youtube.video_livechat(ids, expand_emojis)) |
| 77 | + |
| 78 | + return cls.data_to_csv(data=chat_messages, output_file_path=output_file_path) |
0 commit comments