3
œb`.  ã               @   sd   d dl mZ 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
mZmZmZ G dd	„ d	eƒZdS )
é    )Úunicode_literalsNé   )ÚInfoExtractoré   )Úcompat_HTTPError)Údetermine_extÚint_or_noneÚ
js_to_jsonÚExtractorErrorÚurlencode_postdatac               @   sr   e Zd ZdZdZdZdddddd	d
dœddidœdddddddœddidœdddœgZdd„ Zdd„ Zdd„ Z	dS )ÚFunimationIEzKhttps?://(?:www\.)?funimation(?:\.com|now\.uk)/shows/[^/]+/(?P<id>[^/?#&]+)Ú
funimationNz4https://www.funimation.com/shows/hacksign/role-play/Z91144z	role-playÚmp4z.hack//SIGN - Role Playz$md5:b602bdc15eef4c9bbb201bb6e6a4a2ddzre:https?://.*\.jpg)ÚidÚ
display_idÚextÚtitleÚdescriptionÚ	thumbnailÚskip_downloadT)ÚurlÚ	info_dictÚparamszShttps://www.funimation.com/shows/attack-on-titan-junior-high/broadcast-dub-preview/Z210051zbroadcast-dub-previewz4Attack on Titan: Junior High - Broadcast Dub Previewzre:https?://.*\.(?:jpg|png))r   r   r   r   r   zJhttps://www.funimationnow.uk/shows/puzzle-dragons-x/drop-impact/simulcast/)r   Úonly_matchingc             C   sª   | j ƒ \}}|d krd S y*| jdd dt||dœƒd�}|d | _W nb tk
r¤ } zFt|jtƒr’|jjdkr’| j	|jj
ƒ jƒ d ƒd }t|dd	�‚‚ W Y d d }~X nX d S )
Nz>https://prod-api-funimationnow.dadcdigital.com/api/auth/login/z
Logging in)ÚusernameÚpassword)ÚdataÚtokeni‘  ÚerrorT)Úexpected)Ú_get_login_infoÚ_download_jsonr   Ú_TOKENr
   Ú
isinstanceÚcauser   ÚcodeÚ_parse_jsonÚreadÚdecode)Úselfr   r   r   Úer   © r+   úKC:\Users\Broad\Desktop\All-Down\binaries\youtube_dl\extractor\funimation.pyÚ_login8   s    zFunimationIE._loginc             C   s   | j ƒ  d S )N)r-   )r)   r+   r+   r,   Ú_real_initializeJ   s    zFunimationIE._real_initializec                sr  ˆ j |ƒ}ˆ j||ƒ‰‡ ‡fdd„}ˆ jˆ jdˆddd�|tdd�pFi }|jd	ƒpfˆ jd
dgˆdd d�}|s�ˆ jdddgˆdd�}ˆ jd|dƒ}|jdƒpª|dƒpªˆ jˆƒ }}|dƒ}	|	rÈd|	|f }ˆ jddgˆdd�}
yNi }ˆ jrödˆ j |d< ˆ j	d| ||ddj
dd„ tdƒD ƒƒid �d! }W n„ tk
�r® } zft|jtƒ�rœ|jjd"k�rœˆ j|jjƒ |ƒd# d$ }td%ˆ j|jd&ƒ�p�|jdƒf dd'�‚‚ W Y d d }~X nX g }xl|D ]d}|jd(ƒ}|�sÒ�qº|jd)ƒ�pät|ƒ}|d*k�r|jˆ j||d+d,dd-�ƒ n|j||d.œƒ �qºW ˆ j|ƒ ||||
ˆ jˆƒ|	t|jd/ƒ�pR|d0ƒƒt|jd1ƒƒ||jd2ƒ|d3œS )4Nc                s   ˆ j d|  ˆ| d d�S )Nz*KANE_customdimensions\.%s\s*=\s*'([^']+)';)Údefault)Ú_search_regex)Úname)r)   Úwebpager+   r,   Ú_search_kaneQ   s    z0FunimationIE._real_extract.<locals>._search_kanezTITLE_DATA\s*=\s*({[^}]+})z
title dataÚ )r/   F)Úfatalr   z,KANE_customdimensions.videoID\s*=\s*'(\d+)';z<iframe[^>]+src="/player/(\d+)Úvideo_idz
al:web:urlzog:video:urlzog:video:secure_urlTz/player/(\d+)zvideo idr   Ú
videoTitleÚshowNamez%s - %sr   zog:descriptionzToken %sÚAuthorizationz1https://www.funimation.com/api/showexperience/%s/Zpinst_idc             S   s   g | ]}t jtjtj ƒ‘qS r+   )ÚrandomÚchoiceÚstringÚdigitsÚascii_letters)Ú.0Ú_r+   r+   r,   ú
<listcomp>t   s    z.FunimationIE._real_extract.<locals>.<listcomp>é   )ÚheadersÚqueryÚitemsi“  Úerrorsr   z%s said: %sÚdetail)r   ÚsrcZ	videoTypeÚm3u8r   Úhls)Úm3u8_idr5   )Ú	format_idr   Z	seasonNumÚseasonZ
episodeNumZseriesId)r   r   r   r   r   ÚseriesÚseason_numberÚepisode_numberÚepisodeÚ	season_idÚformats)Ú	_match_idÚ_download_webpager&   r0   r	   ÚgetÚ_html_search_metaÚ_og_search_titler"   r!   ÚjoinÚranger
   r#   r$   r   r%   r'   ÚIE_NAMEr   ÚextendÚ_extract_m3u8_formatsÚappendÚ_sort_formatsÚ_og_search_thumbnailr   )r)   r   r   r3   Z
title_datar6   Ú
player_urlr   rQ   rN   r   rC   Úsourcesr*   r   rS   ÚsourceÚ
source_urlÚsource_typer+   )r)   r2   r,   Ú_real_extractM   s~    


 
$



zFunimationIE._real_extract)
Ú__name__Ú
__module__Ú__qualname__Ú
_VALID_URLÚ_NETRC_MACHINEr"   Ú_TESTSr-   r.   rf   r+   r+   r+   r,   r      s.   


r   )Ú
__future__r   r:   r<   Úcommonr   Úcompatr   Úutilsr   r   r	   r
   r   r   r+   r+   r+   r,   Ú<module>   s   	