U
    g                     @   sd   d dl mZ ddlmZmZmZmZmZmZm	Z	 G dd deZ
G dd de
ZG dd	 d	e
Zd
S )   )InfoExtractor   )ExtractorError
clean_htmlclean_podcast_urlint_or_nonestr_or_nonetry_geturl_or_nonec                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )StitcherBaseIEz2https?://(?:www\.)?stitcher\.com/(?:podcast|show)/c                 C   s:   | j d| ||d}t|dd }|r2t|dd|d S )Nzhttps://api.prod.stitcher.com/)queryc                 S   s   | d d d S )Nerrors    message xr   r   =/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/stitcher.py<lambda>       z*StitcherBaseIE._call_api.<locals>.<lambda>T)expecteddata)Z_download_jsonr	   r   )selfpathZvideo_idr   respZerror_massager   r   r   	_call_api   s     zStitcherBaseIE._call_apic                 C   s   t |dp|dS )NZhtml_descriptiondescription)r   get)r   r   r   r   r   _extract_description   s    z#StitcherBaseIE._extract_descriptionc                 C   s   t |dp|dS )N	audio_urlZguid)r
   r   )r   episoder   r   r   _extract_audio_url   s    z!StitcherBaseIE._extract_audio_urlc                 C   s   | d| ddS )NZimage_base_urltitle)	thumbnailseries)r   )r   showr   r   r   _extract_show_info   s    z!StitcherBaseIE._extract_show_infoc                 C   sp   t |d |d|d  | |t|dt|dt|dt|dt|dd	
}|| |S )
NidZslugr"   durationnoneZdate_publishedZseason	season_id)
r'   
display_idr"   r   r(   urlZvcodec	timestampZseason_numberr*   )strr   stripr   r   r   r   update)r   r    r   	show_infoinfor   r   r   _extract_episode%   s    


zStitcherBaseIE._extract_episodeN)	__name__
__module____qualname___VALID_URL_BASEr   r   r!   r&   r3   r   r   r   r   r      s   	r   c                   @   sx   e Zd Zejd Zdddddddd	d
ddd	ddddddddd	dddidddddddddddgZdd Zd S )!
StitcherIEz1(?:[^/]+/)+e(?:pisode)?/(?:[^/#?&]+-)?(?P<id>\d+)zMhttp://www.stitcher.com/podcast/the-talking-machines/e/40789481?autoplay=trueZ e9635098e0da10b21a0e2b85585530f6Z40789481Zmp3z,Machine Learning Mastery and Cancer Clustersz$md5:547adb4081864be114ae3831b4c2b42fiD  zre:^https?://.*\.jpgZ20151008ihVTalking Machines)	r'   extr"   r   r(   r#   Zupload_dater-   r$   )r,   md5	info_dictzihttp://www.stitcher.com/podcast/panoply/vulture-tv/e/the-rare-hourlong-comedy-plus-40846275?autoplay=trueZ40846275zthe-rare-hourlong-comedy-pluszThe CW's 'Crazy Ex-Girlfriend'z$md5:04f1e2f98eb3f5cbb094cea0f9e19b17i  )r'   r+   r:   r"   r   r(   r#   Zskip_downloadTzPage Not Found)r,   r<   paramsskipzPhttp://www.stitcher.com/podcast/marketplace-on-stitcher/e/40910226?autoplay=truer,   Zonly_matchingz~http://www.stitcher.com/podcast/panoply/getting-in/e/episode-2a-how-many-extracurriculars-should-i-have-40876278?autoplay=truezKhttps://www.stitcher.com/show/threedom/episode/circles-on-a-stick-200212584c                 C   sf   |  |}| d|d|i}|d d }| |}|s>|   t|dd tpPi }| ||| |S )Nzshows/episodesZepisode_idsepisodesr   c                 S   s   | d d S NZshowsr   r   r   r   r   r   r   i   r   z*StitcherIE._real_extract.<locals>.<lambda>)	_match_idr   r!   Zraise_login_requiredr	   dictr3   r&   )r   r,   Zaudio_idr   r    r   r%   r   r   r   _real_extracta   s     
  
  zStitcherIE._real_extractNr4   r5   r6   r   r7   Z
_VALID_URLZ_TESTSrD   r   r   r   r   r8   6   sN   

 )r8   c                   @   s<   e Zd Zejd Zdddddddd	d
dgZdd ZdS )StitcherShowIEz(?P<id>[^/#?&]+)/?(?:[?#&]|$)z4http://www.stitcher.com/podcast/the-talking-machineszthe-talking-machinesr9   z$md5:831f0995e40f26c10231af39cf1ebf0b)r'   r"   r   j   )r,   r<   Zplaylist_mincountz2https://www.stitcher.com/show/the-talking-machinesTr?   c           	      C   s   |  |}| d| d|ddi}t|dd tp6i }| |}g }|dpRg D ](}| |}|shqT|| ||| qT| 	|||d| 
|S )	Nzsearch/show/z/allEpisodescounti'  c                 S   s   | d d S rA   r   r   r   r   r   r      r   z.StitcherShowIE._real_extract.<locals>.<lambda>r@   r"   )rB   r   r	   rC   r&   r   r!   appendr3   Zplaylist_resultr   )	r   r,   Z	show_slugr   r%   r1   entriesr    r   r   r   r   rD   }   s(    

  

  zStitcherShowIE._real_extractNrE   r   r   r   r   rF   n   s   
	rF   N)commonr   utilsr   r   r   r   r   r	   r
   r   r8   rF   r   r   r   r   <module>   s   $)8