o
    i%                     @   sx   d dl Z d dlZd dlZddlmZ ddlmZmZ G dd deZ	G dd	 d	e	Z
G d
d de	ZG dd de	ZdS )    N   )InfoExtractor   )ExtractorErrorupdate_url_queryc                   @   s(   e Zd ZdZdZdZdZdZdd ZdS )	SafariBaseIEz,https://learning.oreilly.com/accounts/login/safariz#https://learning.oreilly.com/api/v1jsonFc              
   C   s  |  dd d\}}dd }||rd| _d S |j}tj|}tj|j}tjd|d d }	| j	d	d d
t
|||	d d|ddd\}
}|
d}|
dsd|
dsd|rdtd| dddD ]}| || qf|  |
dpw|	d d\}}||rd| _d S td)Nz2https://learning.oreilly.com/accounts/login-check/zDownloading login pagec                 S   s
   d| j v S )Nzlearning.oreilly.com/home/)url)urlh r   K/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/safari.py	is_logged   s   
z.SafariBaseIE._perform_login.<locals>.is_loggedTzhttps://api.oreilly.comnextr   z*https://www.oreilly.com/member/auth/login/z
Logging in)emailpasswordredirect_uriapplication/json)zContent-TypeRefereri  )dataheadersexpected_statuscredentials	logged_inr   zUnable to login: expected)groot_sessionidzorm-jwtzorm-rtzCompleting loginzUnable to log in)_download_webpage_handle	LOGGED_INr
   urllibparseurlparseparse_qsqueryurljoin_download_json_handler	   dumpsencodegetr   _apply_first_set_cookie_header)selfusernamer   _r   r   redirect_url
parsed_urlqsnext_uriauthr   cookier   r   r   _perform_login   sV   


zSafariBaseIE._perform_loginN)	__name__
__module____qualname__
_LOGIN_URL_NETRC_MACHINE	_API_BASE_API_FORMATr   r3   r   r   r   r   r      s    r   c                	   @   sl   e Zd ZdZdZ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dgZdZdZdd Z	dS )SafariIEr   z"safaribooksonline.com online videoa  (?x)
                        https?://
                            (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
                            (?:
                                library/view/[^/]+/(?P<course_id>[^/]+)/(?P<part>[^/?\#&]+)\.html|
                                videos/[^/]+/[^/]+/(?P<reference_id>[^-]+-[^/?\#&]+)
                            )
                    zhhttps://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html dcc5a425e79f2564148652616af1f2a3
0_qbqx90icmp4z/Introduction to Hadoop Fundamentals LiveLessonsijrU20150724stork)idexttitle	timestampupload_dateuploader_id)r
   md5	info_dictzYhttps://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.htmlTr
   only_matchingzehttps://www.safaribooksonline.com/library/view/learning-path-red/9780134664057/RHCE_Introduction.htmlzkhttps://www.safaribooksonline.com/videos/python-programming-language/9780134217314/9780134217314-PYMC_13_00znhttps://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838/9780133392838-00_SeriesIntrozfhttps://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/00_SeriesIntro.html192608129375172c                 C   s  |  |}|d}|r|}| j}| j}nBd|d|d}| ||\}}t| j|j	}|d}|sA| j
d|ddd}| j
d	|d
| jdd}| j
d|d| jdd}d| ||d}	| jr| j| j d| |dddddid}
|
r|
d}|r||	d< | td|	dS )Nreference_idz{}-{}	course_idpartz/data-reference-id=(["\'])(?P<id>(?:(?!\1).)+)\1zkaltura reference idrA   )groupz-data-partner-id=(["\'])(?P<id>(?:(?!\1).)+)\1zkaltura widget id)defaultrP   z(data-ui-id=(["\'])(?P<id>(?:(?!\1).)+)\1zkaltura uiconf idr,   )wid	uiconf_idzflashvars[referenceId]z&/player/kaltura_session/?reference_id=z Downloading kaltura session JSONz'Unable to download kaltura session JSONFAcceptr   )fatalr   sessionzflashvars[ks]zEhttps://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.phpKaltura)_match_valid_urlrP   _PARTNER_ID
_UICONF_IDformatr   rematch
_VALID_URLr
   _search_regexr   _download_jsonr9   r(   
url_resultr   )r*   r
   mobjrM   video_id
partner_idui_idwebpager   r#   kaltura_sessionrV   r   r   r   _real_extracts   sZ   



zSafariIE._real_extractN)
r4   r5   r6   IE_NAMEIE_DESCr^   _TESTSrY   rZ   rh   r   r   r   r   r;   G   sB    
r;   c                   @   s0   e Zd ZdZdZddddddgZdd Zd	S )
SafariApiIEz
safari:apizhttps?://(?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/api/v1/book/(?P<course_id>[^/]+)/chapter(?:-content)?/(?P<part>[^/?#&]+)\.htmlzOhttps://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.htmlTrI   zZhttps://www.safaribooksonline.com/api/v1/book/9780134664057/chapter/RHCE_Introduction.htmlc              	   C   s   |  |}| |d|d|dd}|d }d|v r@|dd}|d }|d	d
d  d	|d  d|d
 d d  }| |t S )Nz{}/{}rN   rO   zDownloading part JSONweb_urlzlibrary/viewvideosnatural_key/r   r   -)	rX   r`   r[   rP   replacersplitra   r;   ie_key)r*   r
   rb   rO   rm   natural_keysr   r   r   rh      s   
0zSafariApiIE._real_extractN)r4   r5   r6   ri   r^   rk   rh   r   r   r   r   rl      s    rl   c                       sr   e Zd ZdZdZdZdddddd	d
dddddddddddddddgZe fddZdd Z	  Z
S )SafariCourseIEzsafari:coursez$safaribooksonline.com online coursesa  (?x)
                    https?://
                        (?:
                            (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
                            (?:
                                library/view/[^/]+|
                                api/v1/book|
                                videos/[^/]+
                            )|
                            techbus\.safaribooksonline\.com
                        )
                        /(?P<id>[^/]+)
                    z]https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/9780133392838zHadoop Fundamentals LiveLessons)rA   rC      z.Requires safaribooksonline account credentials)r
   rH   playlist_countskipzQhttps://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=jsonTrI   z2http://techbus.safaribooksonline.com/9780134426365zRhttps://www.safaribooksonline.com/videos/python-programming-language/9780134217314zQhttps://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838zShttps://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/c                    s$   t |s
t|rdS t |S )NF)r;   suitablerl   super)clsr
   	__class__r   r   r|      s   
zSafariCourseIE.suitablec                    sr     |}  j d| d j |d}d|vr#td| dd fdd	|d D }|d
 } |||S )Nz/book/z/?override_format=zDownloading course JSONchapterszNo chapters found for course Tr   c                    s   g | ]
}  |t qS r   )ra   rl   ru   ).0chapterr*   r   r   
<listcomp>   s    z0SafariCourseIE._real_extract.<locals>.<listcomp>rC   )	_match_idr`   r9   r:   r   playlist_result)r*   r
   rN   course_jsonentriescourse_titler   r   r   rh      s   


zSafariCourseIE._real_extract)r4   r5   r6   ri   rj   r^   rk   classmethodr|   rh   __classcell__r   r   r   r   rw      s<    	rw   )r	   r\   urllib.parser   commonr   utilsr   r   r   r;   rl   rw   r   r   r   r   <module>   s    ;^