o
    iK                     @   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mZ ddlmZ G dd deZG d	d
 d
eZdS )    N   )InfoExtractor   )ExtractorErrordetermine_extfilter_dictint_or_noneparse_iso8601
update_urlurl_or_none)traverse_objc                   @   sh  e Zd ZdZdZde dg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dddddd d!d"d#	dd$d%dd&d'd(d)d*d+d,d-d.id/gd0d1d2d3dd4d5d6d7d8dd9d:id/gd;d<d=d>dd?d@dAdBdCddDdEid/gd;gZdFdGdHddIdJdKdLdMdNdOdPgZdQdRg dSiiZe	
dTe	
dUe	
dVe	
dWdXZdYZdfd\d]Zd^d_ Zd`da Zdbdc Zddde ZdZS )gLoomIEloomz@https?://(?:www\.)?loom\.com/(?:share|embed)/(?P<id>[\da-f]{32})z<iframe[^>]+\bsrc=["\'](?P<url>)z;https://www.loom.com/share/43d05f362f734614a2e81b4694a3a523 bfc2d7e9c2e0eb4813212230794b6f42 43d05f362f734614a2e81b4694a3a523mp4z#A Ruler for Windows - 28 March 2022zwILLIAM PIP20220328i^jAb   )idexttitleuploaderupload_date	timestampduration)urlmd5	info_dictz;https://www.loom.com/share/c43a642f815f4378b6f80a889bb73d8d 70f529317be8cf880fcc2c649a531900 c43a642f815f4378b6f80a889bb73d8dwebmzLilah Nielsen Intro VideozLilah Nielsen20200826iLF_   z;https://www.loom.com/share/9458bcbf79784162aa62ffb8dd66201b 7b6bfdef8181c4ffc376e18919a4dcc2 9458bcbf79784162aa62ffb8dd66201bzSharing screen with gpt-4zUSharing screen with GPT 4 vision model and asking questions to guide through blender.zSuneel Mathamzcount:320231109iLe]   )	r   r   r   descriptionr   chaptersr   r   r   z;https://www.loom.com/share/24351eb8b317420289b158e4b7e96ff2 24351eb8b317420289b158e4b7e96ff2z
OMFG clownz$md5:285c5ee9d62aa087b7e3271b08796815zBrailey Bragg20210924iENa   )r   r   r   r(   r   r   r   r   skip_downloaddashzFailed to parse JSON)r   r   paramsexpected_warningsz;https://www.loom.com/share/50e26e8aeb7940189dff5630f95ce1f4 5cc7655e7d55d281d203f8ffd14771f7 50e26e8aeb7940189dff5630f95ce1f4ziOS Mobile UploadzSimon Curran20200520iy^#   videopasswordseniorinfants2)r   r   r   r/   r0   z;https://www.loom.com/embed/ddcf1c1ad21f451ea7468b1e33917e4e f983a0f02f24331738b2f43aecb05256 ddcf1c1ad21f451ea7468b1e33917e4ezCF Reset User's PasswordzAimee Heintz20220707ib   format	bestvideozBhttps://www.loom.com/community/e1229802a8694a09909e8ba0fbb6d073-pg ec838cd01b576cf0386f32e1ae424609 e1229802a8694a09909e8ba0fbb6d073z.Rexie Jane Cimafranca - Founder's PresentationzRexie Cimafranca20230213   inc)r   r   r   r   r   r   r   z404 Not Found)r   r   r   skipGetVideoSourceacceptableMimes)DASHM3U8MP4WEBMa              query GetVideoSSR($videoId: ID!, $password: String) {
              getVideo(id: $videoId, password: $password) {
                __typename
                ... on PrivateVideo {
                  id
                  status
                  message
                  __typename
                }
                ... on VideoPasswordMissingOrIncorrect {
                  id
                  message
                  __typename
                }
                ... on RegularUserVideo {
                  id
                  __typename
                  createdAt
                  description
                  download_enabled
                  folder_id
                  is_protected
                  needs_password
                  owner {
                    display_name
                    __typename
                  }
                  privacy
                  s3_id
                  name
                  video_properties {
                    avgBitRate
                    client
                    camera_enabled
                    client_version
                    duration
                    durationMs
                    format
                    height
                    microphone_enabled
                    os
                    os_version
                    recordingClient
                    recording_type
                    recording_version
                    screen_type
                    tab_audio
                    trim_duration
                    width
                    __typename
                  }
                  playable_duration
                  source_duration
                  visibility
                }
              }
            }
a              query GetVideoSource($videoId: ID!, $password: String, $acceptableMimes: [CloudfrontVideoAcceptableMime]) {
              getVideo(id: $videoId, password: $password) {
                ... on RegularUserVideo {
                  id
                  nullableRawCdnUrl(acceptableMimes: $acceptableMimes, password: $password) {
                    url
                    credentials {
                      Policy
                      Signature
                      KeyPairId
                      __typename
                    }
                    __typename
                  }
                  __typename
                }
                __typename
              }
            }
a              query FetchVideoTranscript($videoId: ID!, $password: String) {
              fetchVideoTranscript(videoId: $videoId, password: $password) {
                ... on VideoTranscriptDetails {
                  id
                  video_id
                  source_url
                  captions_source_url
                  __typename
                }
                ... on GenericError {
                  message
                  __typename
                }
                __typename
              }
            }
a              query FetchChapters($videoId: ID!, $password: String) {
              fetchVideoChapters(videoId: $videoId, password: $password) {
                ... on VideoChapters {
                  video_id
                  content
                  __typename
                }
                ... on EmptyChaptersPayload {
                  content
                  __typename
                }
                ... on InvalidRequestWarning {
                  message
                  __typename
                }
                ... on Error {
                  message
                  __typename
                }
                __typename
              }
            }
)GetVideoSSRrB   FetchVideoTranscriptFetchChapters45a5bd4NTc                 C   st   |  d}| jd||pd|pdddd| j d| j|dd	tj|||d
| j|i | j| ddd |dS )Nr5   zhttps://www.loom.com/graphqlzDownloading GraphQL JSONzFailed to download GraphQL JSONapplication/json	loom_web_webzhttps://www.loom.com)AcceptContent-Typezx-loom-request-sourcezapollographql-client-namezapollographql-client-versionzgraphql-operation-nameOrigin)videoIdpassword)operationName	variablesquery,:
separators)headersdatafatal)		get_param_download_json_APOLLO_GRAPHQL_VERSIONjsondumps_GRAPHQL_VARIABLESget_GRAPHQL_QUERIESencode)selfoperation_namevideo_idnoteerrnoter^   rS    rm   I/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/loom.py_call_graphql_api   s2   


zLoomIE._call_graphql_apic                 C   sp   | j d| d| |d| dd| dddddtjtt d d| d	d
dd d}t|dt	hfS )Nz,https://www.loom.com/api/campaigns/sessions//zDownloading z JSONzFailed to download FrL   )rO   rP   r5   )anonIDdeviceIDforce_originalrS   rW   rZ   )r^   r\   r]   r   )
r`   rb   rc   struuiduuid4r_   rg   r   r   )rh   endpointrj   responserm   rm   rn   _call_url_api  s   

zLoomIE._call_url_apic           
         s   g }t |ddthfdthfddd hfdf fdd	} d
}|||ddd  d}|||ddd t |ddddthf}dd ||fD }	|rht|d d|	vrh|||ddd |S )Nvideo_propertieswidthheightmicrophone_enabledc                 S   s   | du rdS d S )NFnonerm   )xrm   rm   rn   <lambda>$  s    z)LoomIE._extract_formats.<locals>.<lambda>)r{   r|   acodecc           	      3   s   | sd S t | }tj| j}|dkrS| dd}  j| dd| d|d}dt|d	v }|D ]}|r@|d
dkr@d|d< i |t	|d |d|dV  q1d S |dkrt j
| d| dd}|D ]}i |||dV  qed S | |d| |dV  d S )Nm3u8z-split.m3u8z.m3u8r   zhls-F)m3u8_idr^   qualityr~   ).vcodecr   r   r   rV   )r   extra_param_to_segment_urlmpdzdash-)mpd_idr^   )r   r   zhttp-)r   r   	format_idr   )r   urllibparseurlparserV   replace_extract_m3u8_formatsr   re   r
   _extract_mpd_formats)	
format_urlr   r   r   rV   m3u8_formatsis_not_premergedfmtdash_formatsrh   rj   rz   rm   rn   get_formats'  sP   		
z,LoomIE._extract_formats.<locals>.get_formatszraw-urlrawr   )r   ztranscoded-url
transcodedr]   getVideonullableRawCdnUrlr   c                 S   s   g | ]
}|rt |d dqS )Nr   )r
   ).0r   rm   rm   rn   
<listcomp>X  s    z+LoomIE._extract_formats.<locals>.<listcomp>r   cdnr   )r   r   ry   extendr   r
   )
rh   rj   metadata
video_dataformatsr   raw_urltranscoded_urlcdn_url
valid_urlsrm   r   rn   _extract_formats  s    
)zLoomIE._extract_formatsc              	   C   s6   | j d|ddd}tdt|dddd	thifpd iS )
NrI   z"Downloading GraphQL subtitles JSONFr^   enr]   fetchVideoTranscript)
source_urlcaptions_source_urlr   )ro   r   r   r   )rh   rj   	subs_datarm   rm   rn   _get_subtitles^  s   zLoomIE._get_subtitlesc              
   C   s   |  |}t| jd|dddddthfpi }|ddkr/| d	s)td
ddtddd| d|d}| jd|ddd}t|ddthf}||| t|dddt	hf|pYd | 
|||| |dt|dt	hfdt	hfddt	hfdthfdS )NrH   z!Downloading GraphQL metadata JSONFr   r]   r   
__typenameVideoPasswordMissingOrIncorrectr5   zAThis video is password-protected, use the --video-password optionT)expectedzInvalid video passwordrB   zDownloading GraphQL video JSONrJ   z!Downloading GraphQL chapters JSONrz   r   fetchVideoChapterscontent)r   r   r)   r   	subtitlesnamer(   ownerdisplay_name	createdAt)r   r(   r   r   )	_match_idr   ro   dictre   r_   r   r   "_extract_chapters_from_descriptionrt   r   extract_subtitlesr	   )rh   r   rj   r   r   chapter_datar   rm   rm   rn   _real_extracti  sH   



zLoomIE._real_extract)NNT)__name__
__module____qualname__IE_NAME
_VALID_URL_EMBED_REGEX_TESTS_WEBPAGE_TESTSrd   textwrapdedentrf   ra   ro   ry   r   r   r   rm   rm   rm   rn   r      s    
		X	:x
?r   c                   @   s   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ddddddddddd	ddd d!dd	d"d#d$gZd%d& Zd,d(d)Zd*d+ Z	d'S )-LoomFolderIEFzloom:folderz=https?://(?:www\.)?loom\.com/share/folder/(?P<id>[\da-f]{32})zBhttps://www.loom.com/share/folder/997db4db046f43e5912f10dc5f817b5c    997db4db046f43e5912f10dc5f817b5czBlending Lessons)r   r   )r   playlist_mincountr   zBhttps://www.loom.com/share/folder/9a8a87f6b6f546d9a400c8e7575ff7f2    9a8a87f6b6f546d9a400c8e7575ff7f2zList A- a, i, ozBhttps://www.loom.com/share/folder/886e534218c24fd292e97e9563078cc4    886e534218c24fd292e97e9563078cc4zMedicare Agent Training videoszBhttps://www.loom.com/share/folder/b72c4ecdf04745da9403926d80a40c38 b72c4ecdf04745da9403926d80a40c38zQuick Altos Q & A TutorialszBhttps://www.loom.com/share/folder/8b458a94e0e4449b8df9ea7a68fafc4e    8b458a94e0e4449b8df9ea7a68fafc4ezSezer Texting Guide)r   playlist_countr   zBhttps://www.loom.com/share/folder/e056a91d290d47ca9b00c9d1df56c463=    e056a91d290d47ca9b00c9d1df56c463zUser VideoszBhttps://www.loom.com/share/folder/c2dde8cc67454f0e99031677279d8954K    c2dde8cc67454f0e99031677279d8954zHonors 1zKhttps://www.loom.com/share/folder/bae17109a68146c7803454f2893c8cf8/EdpuzzleT)r   only_matchingc                 C   s   | j d| |dddidS )Nz https://www.loom.com/v1/folders/zDownloading folder info JSONlimit10000r   )r`   )rh   	folder_idrm   rm   rn   _extract_folder_data  s   
z!LoomFolderIE._extract_folder_dataNc              	   #   s    |p|   }t|ddd fD ]}|d }| d| t||dV  qt|d fdddthfD ]
}| |E d H  q5d S )	Nvideosc                 S   s   |d S Nr   rm   _vrm   rm   rn   r     s    z6LoomFolderIE._extract_folder_entries.<locals>.<lambda>r   zhttps://www.loom.com/share/r   foldersc                    s   |d  kS r   rm   r   r   rm   rn   r     s    )r   r   
url_resultr   re   rt   _extract_folder_entries)rh   r   initial_folder_datafolder_datavideorj   subfolder_idrm   r   rn   r     s   
z$LoomFolderIE._extract_folder_entriesc              	   C   s:   |  |}| |}| | |||t|ddtjhfS )Nfolderr   )r   r   playlist_resultr   r   rt   strip)rh   r   playlist_idplaylist_datarm   rm   rn   r     s   

zLoomFolderIE._real_extract)N)
r   r   r   _WORKINGr   r   r   r   r   r   rm   rm   rm   rn   r     sj    





	=
r   )rb   r   urllib.parser   ru   commonr   utilsr   r   r   r   r	   r
   r   utils.traversalr   r   r   rm   rm   rm   rn   <module>   s    $	  z