U
    MfE&                     @   s   d dl Z d dlZd dlZd dlZddlmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZ G dd deZG d	d
 d
eZG dd deZdS )    N   )InfoExtractor   )ExtractorError
clean_htmltime_secondstry_callunified_timestampupdate_url_query)traverse_objc                   @   sP   e Zd ZdZdZdZdZdZdd Zdd	 Z	d
d Z
dd Zdd Zdd ZdS )RadikoBaseIEFN)z https://c-rpaa.smartstream.ne.jpz%https://si-c-radiko.smartstream.ne.jpz*https://tf-f-rpaa-radiko.smartstream.ne.jpz*https://tf-c-rpaa-radiko.smartstream.ne.jpz%https://si-f-radiko.smartstream.ne.jpzhttps://rpaa.smartstream.ne.jp)z%https://rd-wowza-radiko.radiko-cf.comzhttps://radiko.jpz"https://f-radiko.smartstream.ne.jp)z"https://c-radiko.smartstream.ne.jpc              	   C   s   | j dd ddddddd\}}|j}|d	 }t|d
 }t|d }|  |||  }t| }| jdd ddd||dddd }	|	dkr| j	dgd ||	f}
| j
dd|
 |
S )Nzhttps://radiko.jp/v2/api/auth1zDownloading authentication pageZpc_html5z0.0.1ZpcZ
dummy_user)zx-radiko-appzx-radiko-app-versionx-radiko-devicex-radiko-user)headersX-Radiko-AuthTokenzX-Radiko-KeyLengthzX-Radiko-KeyOffsetzhttps://radiko.jp/v2/api/auth2ZAuthenticating)r   r   zx-radiko-authtokenzx-radiko-partialkey,r   ZOUTZJP)Z	countriesradiko	auth_data)Z_download_webpage_handler   int_extract_full_keybase64	b64encodedecode_download_webpagesplitZraise_geo_restrictedcachestore)self_Zauth1_handleZauth1_header
auth_tokenklkoZraw_partial_keyZpartial_keyarea_idr    r#   ;/tmp/pip-unpacked-wheel-zk9bgjdq/yt_dlp/extractor/radiko.py_negotiate_token'   sF      
  	zRadikoBaseIE._negotiate_tokenc                 C   sL   | j dd}|d k	rD| jdd dd|d |d dd	}|d
krD|S |  S )Nr   r   z#https://radiko.jp/v2/api/auth_checkzChecking cached tokeni  r   r   )r   X-Radiko-AreaId)Zexpected_statusr   OK)r   loadr   r%   )r   Z	cachedataresponser#   r#   r$   _auth_clientH   s       zRadikoBaseIE._auth_clientc                 C   sL   | j r| j S | jdd dd}| jd|dddd}|r>| }nd	}|| _ |S )
Nz)https://radiko.jp/apps/js/playerCommon.jszDownloading player js codenotezRRadikoJSPlayer\([^,]*,\s*(['\"])pc_html5\1,\s*(['\"])(?P<fullkey>[0-9a-f]+)\2,\s*{zfull keyFZfullkey)fatalgroups(   bcd151073c03b352e1ef2fd66c32209da9ca0afa)	_FULL_KEYr   Z_search_regexencode)r   ZjscodeZfull_keyr#   r#   r$   r   R   s&        
zRadikoBaseIE._extract_full_keyc                 C   s   | j d| d|d| dd}d }|dD ]F}|jd |jd  }}t|d	}	t|d	}
|	|kr.||
k r.|} qvq.|std
|	st|
|||	||fS )Nz,https://radiko.jp/v3/program/station/weekly/.xmlzDownloading radio program for z stationr+   z.//progfttoFz*Cannot identify radio program to download!)_download_xmlfindallattribr	   r   AssertionError)r   video_idstationcursorstation_programprogpZft_strZto_strr2   r3   r#   r#   r$   _find_programe   s"    
 


zRadikoBaseIE._find_programc	                 C   sB  | j d| d|dd}	g }
t }|r*dnd}|	d| dD  ]}|j}||krVq@|| t|d	|i|d
dtjddddd}|rd n|| }t	j
|j}| j||ddd|d| ||dd}|D ]\}||| jA s|s|| jrd|d< d|d< |s|dkr|rddt|gi|d< q|
| q@|
S )Nz-https://radiko.jp/v3/station/stream/pc_html5/r1   zDownloading stream informationr+   r   r   z.//url[@timefree="z"]/playlist_create_urlZ
station_idZ15 0123456789abcdef    )kb)lZlsidtypeZm4aTFz"Downloading m3u8 information from )r&   r   )extZliver-   Zm3u8_idr,   r   iZ
preferenceznot preferredZformat_noteZffmpeg_argsz-ssZdownloader_options)r4   setr5   textaddr
   joinrandomchoicesurllibparseurlparsenetlocZ_extract_m3u8_formats
startswith_HOSTS_FOR_LIVE'_HOSTS_FOR_TIME_FREE_FFMPEG_UNSUPPORTEDstrextend)r   r8   r9   is_onairr2   r:   r   r"   queryZm3u8_playlist_dataformatsfoundZtimefree_intelementZpcuZplaylist_urlZtime_to_skipdomainZ
subformatsZsfr#   r#   r$   _extract_formatsw   s`    
 
 
    
zRadikoBaseIE._extract_formatsc                 C   s"   t |dddd hdtjhfp d S )Nz
pfm/text().c                 S   s   t d| S )Nu   [/／、　,，])rer   )xr#   r#   r$   <lambda>       z2RadikoBaseIE._extract_performers.<locals>.<lambda>)r   rT   strip)r   r<   r#   r#   r$   _extract_performers   s        z RadikoBaseIE._extract_performers)__name__
__module____qualname__Z_GEO_BYPASSr/   rS   Z%_HOSTS_FOR_TIME_FREE_FFMPEG_SUPPORTEDrR   r%   r*   r   r>   r\   rb   r#   r#   r#   r$   r      s   !
-r   c                   @   s4   e Zd ZdZdddddddddgZdd Zd	S )
RadikoIEzGhttps?://(?:www\.)?radiko\.jp/#!/ts/(?P<station>[A-Z0-9-]+)/(?P<id>\d+)z*https://radiko.jp/#!/ts/QRR/20210425101300TurlZonly_matchingz*https://radiko.jp/#!/ts/FMT/20210810150000z.https://radiko.jp/#!/ts/JOAK-FM/20210509090000c                    s   |  | \}}t|d}| |||\ }|  \}}|t fdd|  tt fddtfdd||tfddd| j||d|||||dd	d

S )NFc                      s     djS )NtitlefindrH   r#   r<   r#   r$   r_      r`   z(RadikoIE._real_extract.<locals>.<lambda>c                      s     djS )Ninforj   r#   rl   r#   r$   r_      r`   c                      s     djS )N.//namerj   r#   )r;   r#   r$   r_      r`   c                      s   t dt  d S )NF)r	   r#   )radio_begin	radio_endr#   r$   r_      r`   T)Zstart_atr2   Zend_atr3   seekr8   r9   rV   r2   r:   r   r"   rW   )
idri   castdescriptionuploaderuploader_id	timestampdurationis_liverX   )	Z_match_valid_urlgroupsr	   r>   r*   r   rb   r   r\   )r   rh   r9   r8   Zvid_intr2   r   r"   r#   )r<   ro   rp   r;   r$   _real_extract   s:    
     zRadikoIE._real_extractNrc   rd   re   Z
_VALID_URLZ_TESTSr|   r#   r#   r#   r$   rf      s   rf   c                   @   s4   e Zd ZdZdddddddddgZdd Zd	S )
RadikoRadioIEz8https?://(?:www\.)?radiko\.jp/#!/live/(?P<id>[A-Z0-9-]+)zhttps://radiko.jp/#!/live/QRRTrg   zhttps://radiko.jp/#!/live/FMTz!https://radiko.jp/#!/live/JOAK-FMc              
   C   s   |  |}| d |  \}}tdd}| |||\}}}}	}	|dj}
t|dj}|dj}| j||d||||i d}||
| 	||||||dd		S )
NzHDownloader will not stop at the end of the program! Press Ctrl+C to stop	   )hoursri   rm   rn   Trr   )	rs   ri   rt   ru   rv   rw   rx   rX   rz   )
Z	_match_idZreport_warningr*   r   r>   rk   rH   r   r\   rb   )r   rh   r9   r   r"   Zvid_nowr<   r;   r2   r   ri   ru   Zstation_namerX   r#   r#   r$   r|      s8    


     zRadikoRadioIE._real_extractNr}   r#   r#   r#   r$   r~      s   r~   )r   rK   r]   urllib.parserM   commonr   utilsr   r   r   r   r	   r
   Zutils.traversalr   r   rf   r~   r#   r#   r#   r$   <module>   s     /