
    OJnia                     F    d dl Z d dlmZ e G d d             ZdedefdZy)    N)	dataclassc                       e Zd ZU dZdZeed<   dZeed<   dZe	ed<   dZ
eed<   dZeed<   dZeed<   dZeed	<   dZeed
<   dZeed<   dZeed<   dZeed<   defdZy)
StreamDataa  
    Represents metadata and URLs associated with a streaming session.

    This class encapsulates essential information about a stream, including platform details,
    streamer information, stream status, and URLs for different stream formats.
    It also provides a method to convert the object to a JSON string.

    Attributes:
        platform (str): The streaming platform (e.g., "Twitch", "SOOP", "TikTok").
        anchor_name (str): The name of the streamer.
        is_live (bool): Indicates whether the stream is currently live.
        title (str): The title of the stream.
        quality (str): The quality of the stream (e.g., "OD", "BD", "UHD", "HD").
        m3u8_url (str): The URL for the m3u8 stream format.
        flv_url (str): The URL for the FLV stream format.
        record_url (str): The URL for recording the stream.
        new_cookies (str): Updated cookies required for accessing the stream.
        new_token (str): Updated token required for accessing the stream.
        extra (dict): Additional metadata or custom fields.

    Example:
        >>> stream_data = StreamData(platform="Twitch", anchor_name="StreamerName", is_live=True, title="Live Title")
        >>> json_data = stream_data.to_json()
        >>> print(json_data)
        JSON representation of the stream data

    Note:
        The `extra` attribute can be used to store any additional metadata that is not explicitly defined in the class.
    Nplatformanchor_nameis_livetitlequalitym3u8_urlflv_url
record_urlnew_cookies	new_tokenextrareturnc                 F    t        j                  | j                  dd      S )a`  
        Converts the StreamData object to a JSON string.

        This method serializes the object's attributes into a JSON format, making it easy to
        transmit or store the stream data.

        Returns:
            str: A JSON representation of the StreamData object.

        Example:
            >>> stream_data = StreamData(platform="Twitch", anchor_name="StreamerName")
            >>> json_data = stream_data.to_json()
            >>> print(json_data)
            {
                "platform": "Twitch",
                "anchor_name": "StreamerName",
                ...
            }
        F   )ensure_asciiindent)jsondumps__dict__)selfs    ?/home/uftp/myenv/lib/python3.12/site-packages/streamget/data.pyto_jsonzStreamData.to_json0   s    ( zz$--eAFF    )__name__
__module____qualname____doc__r   str__annotations__r   r   boolr	   r
   r   r   r   r   r   r   dictr    r   r   r   r      s    : HcKGTE3GSHcGSJKIsE4G Gr   r   datar   c                     t        | t              st        d      g d}ddg}||z   D ]  }|| vsd| |<    t        di | S )aA  
    Wraps a dictionary into a StreamData object with default values for missing fields.

    This function ensures that all required and optional fields are present in the input dictionary.
    If a field is missing, it is set to `None`.

    Args:
        data (dict): A dictionary containing stream data.

    Returns:
        StreamData: An instance of StreamData with default values for missing fields.

    Raises:
        TypeError: If the input is not a dictionary.

    Example:
        >>> json_data = {"platform": "Bilibili", "anchor_name": "StreamerName"}
        >>> stream_data = wrap_stream(json_data)
        >>> print(stream_data)
        StreamData(platform='Bilibili', anchor_name='StreamerName', ...)

    Note:
        The function assumes that the input dictionary contains valid data types for each field.
    zInput must be a dictionary)r   r   r   r	   r
   r   r   r   r   r   Nr%   )
isinstancer$   	TypeErrorr   )r&   required_fieldsoptional_fieldsfields       r   wrap_streamr-   G   s^    2 dD!455uO$k2O ?2 DK r   )r   dataclassesr   r   r$   r-   r%   r   r   <module>r/      s;     ! >G >G >GB#d #z #r   