
    OJniK                     ^    d dl Z d dl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   )utils)
StreamDatawrap_stream)	async_req   )BaseLiveStreamc                        e Zd ZdZ	 	 ddedz  dedz  dedz  dedz  dedz  f
 fdZd	efd
Zd	edz  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 )TwitCastingLiveStreamzR
    A class for fetching and processing TwitCasting live stream information.
    N
proxy_addrcookiesusernamepasswordaccount_typec                 z    t         |   ||       || _        || _        || _        | j                         | _        y N)super__init__r   r   r   _get_mobile_headersmobile_headers)selfr   r   r   r   r   	__class__s         \/home/uftp/myenv/lib/python3.12/site-packages/streamget/platforms/twitcasting/live_stream.pyr   zTwitCastingLiveStream.__init__   s9    W-  ("668    returnc                 0    ddddd| j                   xs ddS )Nztext/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7z/zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6z!application/x-www-form-urlencodedz\https://twitcasting.tv/indexcaslogin.php?redir=%2Findexloginwindow.php%3Fnext%3D%252F&keep=1z;ios/7.830 (ios 17.0; ; iPhone 15 (A2846/A3089/A3090/A3092))zihl=zh; did=377eda93b5320f104357ab1bc98dfe4d; _ga=GA1.1.869052351.1747879503; keep=1; chid=relay_trade_jp;)acceptzaccept-languagezcontent-typerefererz
user-agentcookie)r   )r   s    r   r   z)TwitCastingLiveStream._get_mobile_headers   s1    IP?uWll E 'E	
 		
r   c                 4  K   | j                   dk(  rd}d}nd}d}t        || j                  | j                         d {   }t	        j
                  d|      j                  d      }| j                  | j                  d	|d
}	 t        || j                  | j                  |dd       d {   }d|v r?t        j                  |      | _        | j                  | j                  d<   | j                  S y 7 7 J# t        $ r}t        d|      d }~ww xY ww)Ntwitterz-https://twitcasting.tv/indexpasswordlogin.phpzYhttps://twitcasting.tv/indexpasswordlogin.php?redir=/indexloginwindow.php?next=%2F&keep=1z9https://twitcasting.tv/indexcaslogin.php?redir=%2F&keep=1zThttps://twitcasting.tv/indexcaslogin.php?redir=/indexloginwindow.php?next=%2F&keep=1r   headersz8<input type="hidden" name="cs_session_id" value="(.*?)">   login)r   r   actioncs_session_idT   )r   r#   datareturn_cookiestimeouttc_ssr   zTwitCasting login error,)r   r   r   r   researchgroupr   r   r   dict_to_cookie_strr   	Exception)r   	login_url	login_apihtml_strr'   r)   cookie_dictes           r   login_twitcastingz'TwitCastingLiveStream.login_twitcasting"   s    	)GIsISInI"9RVReReff		"\^fgmmnop *	
		; )doot?R?R$!< <K +%$77D04##H-||# & g<  	;6::	;sI   >D C7AD(C; .C9/AC; 5D9C; ;	DDDDurlprocess_datac                    K   j                  d      d   }dt        f fd}ddd}d}	  j                  d	      }|d
k(  r4 j                          d{   }|st	        d      | j
                  d<    |        d{   \  }}	}
||d<   |	d
k(  rd| d}t        | j                   j
                         d{   }t        j                  |      }|j                  d      r|d   j                  d      st	        d      |d   d   }ddddt        |j                         fd      }|D cg c]  \  }}|	 }}}||
d|dz  }||d<   |S 7 7 # t        $ rM  j                          d{  7  }|st	        d      | j
                  d<    |        d{  7  \  }}	}
Y )w xY w7 c c}}w 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   r   c                    K   t        j                  j                         d {   } t        j                  d|       }t        j                  d|       }t        j                  d|       }t        j                  d|       }|j                  d      j                          d|j                  d       d|j                  d       |j                  d      |j                  d      fS 7 ­w)	Nr"   u)   <title>(.*?) \(@(.*?)\)  的直播 - Twitz4<meta name="twitter:title" content="(.*?)">
\s+<metaz(data-is-onlive="(.*?)"
\s+data-view-modez&data-movie-id="(.*?)" data-audience-idr$   -r   )r   r   r   r-   r.   r/   strip)r4   anchortitlestatusmovie_idr   r8   s        r   get_dataz=TwitCastingLiveStream.fetch_web_stream_data.<locals>.get_dataK   s     &stPTPcPcddHYYLhWFIIVX`aEYYKXVFyy!I8THll1o++-.aQ/@(..QRBSATUW]WcWcdeWfhmhshstuhvvv es   &C.C,CC. F)anchor_nameis_liveNr%   truezcTwitCasting login failed, please check if the account password in the configuration file is correctCookiezhTwitCasting login failed, please check if the account and password in the configuration file are correctrE   z/https://twitcasting.tv/streamserver.php?target=z&mode=client&player=pc_webr"   ztc-hlsstreamsz No m3u8_url,please check the urlr   r$   r   )highmediumlowc                     | d      S )Nr    )itemquality_orders    r   <lambda>z=TwitCastingLiveStream.fetch_web_stream_data.<locals>.<lambda>t   s    -X\]^X_J` r   )keyT)r@   rF   play_url_listnew_cookies)splittuple
get_paramsr7   RuntimeErrorr   AttributeErrorr   r   jsonloadsgetsorteditems)r   r8   r9   	anchor_idrC   result
new_cookieto_loginrE   live_status
live_titleurl_streamserverTwitcasting_str	json_datastream_dictsorted_streamsqualityrS   rP   s   ``                @r   fetch_web_stream_dataz+TwitCastingLiveStream.fetch_web_stream_data>   s     IIcN1%		w 	w "$6
	DsG4H6!#'#9#9#;;
!& (G H H 1;##H-9A3C0Kj !,}& !PQZP[[uv$-.>4??dhdwdw$xxO

?3I==*)H2E2I2I)2T"#EFF#H-i8K%&!A>M#K$5$5$7=`aN5CD\WcSDMD
tm\\F *}? < 4D 	D#55777J" $D E E -7D)9A3C3C0Kj	D y Es   )G*E! E*E! EE! 5GF:A:G=F<	GE! E! !F7=F >+F7)F,*	F73G6F77G<Grg   video_qualityc                 ^   K   | j                  ||dd       d{   }t        |      S 7 w)z_
        Fetches the stream URL for a live room and wraps it into a StreamData object.
        FTwitCasting)specplatformN)get_stream_urlr   )r   rg   rl   r)   s       r   fetch_stream_urlz&TwitCastingLiveStream.fetch_stream_urlz   s6      ((MXe(ff4   gs   -+-)NNNNN)Tr   )__name__
__module____qualname____doc__strr   dictr   r7   boolrk   intr   rr   __classcell__)r   s   @r   r   r   
   s     jnIM93: 9sTz 9\_bf\f 9:9<?$J9

T 

;t ;8:s :$ :RV :x! !S3YQUEU !ak !r   r   )rZ   r-   rD   r   r)   r   r   requests.async_httpr   baser	   r   rN   r   r   <module>r~      s%     	  + , !u!N u!r   