U
    g*%                     @   s   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G d	d
 d
eZG dd deZG dd deZdS )    N   )InfoExtractor   )	HTTPError)ExtractorErrorOnDemandPagedListint_or_none	qualitiestry_getc                   @   sT   e Zd ZddddZddddZd	d
 Zdd Zdd Zdd Zdd Z	dd Z
dS )RedGifsBaseInfoExtractor   i  N)gifsdZhdzhttps://www.redgifs.com/zhttps://www.redgifs.comzapplication/json)Zrefereroriginzcontent-typec           	         s  | d}tt| j }t| d t| fdd}g }| j D ]P\}}|d  |}|sfqJt |pp }|	|||r|| nd |||d qJ|d| t
 dd	| d
pg pdt| d| dt| dt| dt| d| d
p
g | d
d|dS )Nidheightc                    s    | d  S )Nwidth )xZorig_heightr   </tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/redgifs.py<lambda>!       z:RedGifsBaseInfoExtractor._parse_gif_data.<locals>.<lambda>urls)url	format_idr   r   qualityzhttps://redgifs.com/watch/ZRedGifs tagsZ
createDateZuserNamedurationZviewsZlikes   )r   Zwebpage_urlZextractor_keyZ	extractortitle	timestampuploaderr   
view_count
like_count
categoriesr   	age_limitformats)getr	   tuple_FORMATSkeysr   r
   itemsminappend	RedGifsIEZie_keyjoin)	selfZgif_datavideo_idr   Zaspect_ratior(   r   r   Z	video_urlr   r   r   _parse_gif_data   s@    

	z(RedGifsBaseInfoExtractor._parse_gif_datac                 C   s:   | j d|dd}|ds"tdd|d  | jd< d S )Nz)https://api.redgifs.com/v2/auth/temporaryzFetching temporary tokennotetokenzUnable to get temporary tokenzBearer authorization)_download_jsonr)   r   _API_HEADERS)r2   r3   authr   r   r   _fetch_oauth_tokenB   s     
z+RedGifsBaseInfoExtractor._fetch_oauth_tokenc                 K   s   dD ]}d| j kr| | z@t| j }d| |d< | jd| |fd|i|}W  qW q tk
r } z2|rt|jtr|jjdkr| j d= W Y q W 5 d }~X Y qX qd|krtd	|d  d
|d|S )N)TFr8   zhttps://www.redgifs.com/watch/zx-customheaderzhttps://api.redgifs.com/v2/headersi  errorzRedGifs said: T)expectedr3   )	r:   r<   dictr9   r   
isinstancecauser   status)r2   epr3   kwargsZfirst_attemptr=   dataer   r   r   	_call_apiJ   s.    


 
z"RedGifsBaseInfoExtractor._call_apic                 c   sF   |d |d< | j |||d|d  d}|d D ]}| |V  q0d S )Nr   pagezDownloading JSON metadata page )queryr6   Zgifs)rH   r4   )r2   rD   r3   rJ   rI   rF   entryr   r   r   _fetch_page^   s       z$RedGifsBaseInfoExtractor._fetch_pagec                    s$    fdd|  D }dd |D S )Nc                    s&   g | ]\}}|  ||fd  fqS )r   )r)   ).0
field_namedefaultrJ   r   r   
<listcomp>g   s   z?RedGifsBaseInfoExtractor._prepare_api_query.<locals>.<listcomp>c                 S   s   i | ]\}}|d k	r||qS Nr   )rM   keyvalr   r   r   
<dictcomp>k   s       z?RedGifsBaseInfoExtractor._prepare_api_query.<locals>.<dictcomp>)r-   )r2   rJ   fieldsZ	api_queryr   rP   r   _prepare_api_queryf   s    
z+RedGifsBaseInfoExtractor._prepare_api_queryc              	   C   sF   t |ddd }t| j||| ||}|r:||S t|| jS )NrI   rR   r   )r   r)   	functoolspartialrL   rW   r   
_PAGE_SIZE)r2   rD   Zitem_idrJ   rV   rI   Zpage_fetcherr   r   r   _paged_entriesm   s       
z'RedGifsBaseInfoExtractor._paged_entries)__name__
__module____qualname__r+   r:   r4   r<   rH   rL   rW   r[   r   r   r   r   r      s   &r   c                   @   s`   e Zd ZdZdddddddd	eeed
edddddddddd	eeed
eddgZdd ZdS )r0   zShttps?://(?:(?:www\.)?redgifs\.com/watch/|thumbs2\.redgifs\.com/)(?P<id>[^-/?#\.]+)z3https://www.redgifs.com/watch/squeakyhelplesswisentZsqueakyhelplesswisentZmp4zHotwife Legs ThickiŇaZ20211107Z	ignored52   r    )r   extr!   r"   Zupload_dater#   r   r$   r%   r&   r'   r   )r   	info_dictz@https://thumbs2.redgifs.com/SqueakyHelplessWisent-mobile.mp4#t=0c                 C   s4   |  | }| jd| d|dd}| |d S )Nzgifs/z
?views=yeszDownloading video infor5   r   )Z	_match_idlowerrH   r4   )r2   r   r3   Z
video_infor   r   r   _real_extract   s    
  zRedGifsIE._real_extractN)r\   r]   r^   
_VALID_URLintlist_TESTSrc   r   r   r   r   r0   t   sB   "r0   c                   @   sZ   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gZdd ZdS )RedGifsSearchIEzRedgifs searchz8https?://(?:www\.)?redgifs\.com/browse\?(?P<query>[^#]+)P   z+https://www.redgifs.com/browse?tags=Lesbianztags=LesbianZLesbianz/RedGifs search for Lesbian, ordered by trendingr   r!   descriptiond   r   ra   Zplaylist_mincountz?https://www.redgifs.com/browse?type=g&order=latest&tags=Lesbianz type=g&order=latest&tags=Lesbianz-RedGifs search for Lesbian, ordered by latestzFhttps://www.redgifs.com/browse?type=g&order=latest&tags=Lesbian&page=2z'type=g&order=latest&tags=Lesbian&page=2r   ra   Zplaylist_countc              	   C   s   |  |d}tj|}|ds2tddd|dd }|ddd }|g|d	< | d
||d dd d}| |||d| d| S )NrJ   r   zInvalid query tagsT)r?   r   order)trendingsearch_textzgifs/searchrp   )rq   ro   typezRedGifs search for , ordered by )	_match_valid_urlgroupurllibparseparse_qsr)   r   r[   playlist_result)r2   r   	query_strrJ   r   ro   entriesr   r   r   rc      s$    


   zRedGifsSearchIE._real_extractNr\   r]   r^   ZIE_DESCrd   rZ   rg   rc   r   r   r   r   rh      s4   

rh   c                   @   sZ   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	gZdd ZdS )RedGifsUserIEzRedgifs userzRhttps?://(?:www\.)?redgifs\.com/users/(?P<username>[^/?#]+)(?:\?(?P<query>[^#]+))?   z(https://www.redgifs.com/users/lamsinka89Z
lamsinka89z*RedGifs user lamsinka89, ordered by recentrj   rl   rm   z/https://www.redgifs.com/users/lamsinka89?page=3zlamsinka89?page=3rn   z:https://www.redgifs.com/users/lamsinka89?order=best&type=gzlamsinka89?order=best&type=gz(RedGifs user lamsinka89, ordered by bestc              	   C   s   |  |dd\}}|r(| d| n|}tj|}|ddd }| d| d||d	d d
}| |||d| d| S )NusernamerJ   ?ro   )recentr   zusers/z/searchr   )ro   rr   zRedGifs user rs   )rt   ru   rv   rw   rx   r)   r[   ry   )r2   r   r   rz   Zplaylist_idrJ   ro   r{   r   r   r   rc      s       zRedGifsUserIE._real_extractNr|   r   r   r   r   r}      s4   

r}   )rX   urllib.parserv   commonr   Znetworking.exceptionsr   utilsr   r   r   r	   r
   r   r0   rh   r}   r   r   r   r   <module>   s   	e+6