
    OJni                     j    d dl Z d dlZd dlmZ ddlmZ ddlmZm	Z	 ddl
mZ ddlmZ  G d	 d
e      Zy)    N)
itemgetter   )utils)
StreamDatawrap_stream)	async_req   )BaseLiveStreamc                        e Zd ZdZddedz  dedz  f fdZdefdZdefdZ	dd	ed
e
defdZddedeez  dz  defdZ xZS )AcfunLiveStreamzP
        A class for fetching and processing Acfun live stream information.
    N
proxy_addrcookiesc                 P    t         |   ||       | j                         | _        y N)super__init___get_pc_headers
pc_headers)selfr   r   	__class__s      V/home/uftp/myenv/lib/python3.12/site-packages/streamget/platforms/acfun/live_stream.pyr   zAcfunLiveStream.__init__   s"    W-..0    returnc                 ,    dd| j                   xs dddS )Nzhttps://live.acfun.cnhttps://live.acfun.cn/z!__ac_nonce=064caded4009deafd8b89;z}Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0)OriginReferercookiez
User-Agent)r   )r   s    r   r   zAcfunLiveStream._get_pc_headers   s%    -/llI&II	
 	
r   c                    K   dt        j                  d       }ddd| dd}dd	i}d
}t        ||| j                  |       d {   }t	        j
                  |      }|d   }|d   }|||fS 7 (w)Nweb_   r   zPMozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0z_did=;)refererz
user-agentr   sidzacfun.api.visitorz.https://id.app.acfun.cn/rest/app/visitor/logindatar   headersuserIdacfun.api.visitor_st)r   generate_random_stringr   r   jsonloads)	r   didr'   r&   apijson_str	json_datauser_id
visitor_sts	            r   _get_acfun_sign_paramsz&AcfunLiveStream._get_acfun_sign_params   s     U11"567/lcU!n
 &
 ?"3TdooW^__JJx(	H%56
Z''	 `s   AA2A0)A2urlprocess_datac                   K   |j                  d      d   j                  dd      d   }d| }t        || j                  | j                         d{   }t        j                  |      }|d	   d
   }d|d	   v }|dd}	|rd|	d<   | j                          d{   \  }
}}ddd|
||d}|dd}dt        j                  j                  |       }t        ||| j                  | j                         d{   }t        j                  |      }|d   d   }|d   d   }t        j                  |      d   d   d   d   }t        |t        d      d      }|	||d z  }	|	S 7 7 7 pw)!a)  
        Fetches web stream data for a live room.

        Args:
            url (str): The room URL.
            process_data (bool): Whether to process the data. Defaults to True.

        Returns:
            dict: A dictionary containing anchor name, live status, room URL, and title.
        ?r   /   )maxsplitz:https://live.acfun.cn/rest/pc-direct/user/userInfo?userId=)r   r'   NprofilenameliveIdF)anchor_nameis_liveTr?   mainApp	ACFUN_APPPC_WEB)subBizkpnkpfr(   r-   r)   FLV)authorIdpullStreamTypez6https://api.kuaishouzt.com/rest/zt/live/web/startPlay?r%   r&   captionvideoPlayResliveAdaptiveManifestadaptationSetrepresentationbitrate)keyreverse)play_url_listtitle)splitrsplitr   r   r   r+   r,   r3   urllibparse	urlencodesortedr   )r   r4   r5   	author_iduser_info_apir/   r0   r>   statusresultr1   r-   r2   paramsr&   play_api
live_titlerJ   rQ   s                      r   fetch_web_stream_dataz%AcfunLiveStream.fetch_web_stream_data-   s     IIcN1%,,S1,=a@	TU^T_`"=T__VZVeVeffJJx(		*62Yy11!,? $F9-1-H-H-J'J$GS*#"!(2F &"'D
 PPVP\P\PfPfgmPnOopH&xdt`d`o`oppH

8,I"6*95J$V,^<L JJ|45KLQOP_`aqrM"=j6KUYZM
KKF= g (K  qs9   AE/E(AE/E+AE/<E-=A,E/+E/-E/r0   video_qualityc                 `   K   | j                  ||ddd       d{   }t        |      S 7 w)z_
        Fetches the stream URL for a live room and wraps it into a StreamData object.
        flvr4   Acfun)url_typeflv_extra_keyplatformN)get_stream_urlr   )r   r0   ra   r&   s       r   fetch_stream_urlz AcfunLiveStream.fetch_stream_urlZ   sA      ((}uET[ ) ] ]4  ]s   .,.)NN)Tr   )__name__
__module____qualname____doc__strr   dictr   tupler3   boolr`   intr   ri   __classcell__)r   s   @r   r   r      s|    13: 1sTz 1
 
(e ("+s +$ +RV +Z! !S3YQUEU !ak !r   r   )r+   urllib.parserU   operatorr    r   r&   r   r   requests.async_httpr   baser
   r    r   r   <module>rz      s(        + , !U!n U!r   