o
    i(                     @   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 )RedGifsBaseIE   i  N)gifsdhdzhttps://www.redgifs.com/zhttps://www.redgifs.comzapplication/json)refereroriginzcontent-typec           	         s  | d}tt| j }t| d t| fdd}g }| j D ](\}}|d  |}|s3q%t |p8 }|	|||rD|| nd |||d q%|d| t
 dd	| d
p_g pbdt| d| dt| dt| dt| d| d
pg | d
d|dS )Nidheightc                    s    | d  S )Nwidth )xorig_heightr   L/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/redgifs.py<lambda>!   s    z/RedGifsBaseIE._parse_gif_data.<locals>.<lambda>urls)url	format_idr   r   qualityzhttps://redgifs.com/watch/RedGifs tags
createDateuserNamedurationviewslikes   )r   webpage_urlextractor_key	extractortitle	timestampuploaderr$   
view_count
like_count
categoriesr!   	age_limitformats)getr	   tuple_FORMATSkeysr   r
   itemsminappend	RedGifsIEie_keyjoin)	selfgif_datavideo_idr   aspect_ratior2   r   r   	video_urlr   r   r   _parse_gif_data   s@   

	zRedGifsBaseIE._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_jsonr3   r   _API_HEADERS)r=   r?   authr   r   r   _fetch_oauth_tokenB   s   
z RedGifsBaseIE._fetch_oauth_tokenc                 K   s   dD ]O}d| j vr| | zt| j }d| |d< | jd| |fd|i|}W  n% tyQ } z|rLt|jtrL|jjdkrL| j d= W Y d }~q d }~ww d|v rbtd	|d  d
|d|S )N)TFrF   zhttps://www.redgifs.com/watch/zx-customheaderzhttps://api.redgifs.com/v2/headersi  errorzRedGifs said: T)expectedr?   )	rH   rJ   dictrG   r   
isinstancecauser   status)r=   epr?   kwargsfirst_attemptrK   dataer   r   r   	_call_apiJ   s0   



zRedGifsBaseIE._call_apic                 c   sH    |d |d< | j |||d|d  d}|d D ]}| |V  qd S )Nr   pagezDownloading JSON metadata page )queryrD   gifs)rW   rB   )r=   rR   r?   rY   rX   rU   entryr   r   r   _fetch_page^   s   zRedGifsBaseIE._fetch_pagec                    s$    fdd|  D }dd |D S )Nc                    s&   g | ]\}}|  ||fd  fqS )r   )r3   ).0
field_namedefaultrY   r   r   
<listcomp>g   s    z4RedGifsBaseIE._prepare_api_query.<locals>.<listcomp>c                 S   s   i | ]\}}|d ur||qS Nr   )r]   keyvalr   r   r   
<dictcomp>k   s    z4RedGifsBaseIE._prepare_api_query.<locals>.<dictcomp>)r7   )r=   rY   fields	api_queryr   r`   r   _prepare_api_queryf   s   
z RedGifsBaseIE._prepare_api_queryc              	   C   sF   t |ddd }t| j||| ||}|r||S t|| jS )NrX   rb   r   )r   r3   	functoolspartialr\   rh   r   
_PAGE_SIZE)r=   rR   item_idrY   rf   rX   page_fetcherr   r   r   _paged_entriesm   s
   zRedGifsBaseIE._paged_entries)__name__
__module____qualname__r5   rH   rB   rJ   rW   r\   rh   rn   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dddddddd	eeed
eddgZdd ZdS )r:   z[https?://(?:(?:www\.)?redgifs\.com/(?:watch|ifr)/|thumbs2\.redgifs\.com/)(?P<id>[^-/?#\.]+)z3https://www.redgifs.com/watch/squeakyhelplesswisentsqueakyhelplesswisentmp4zHotwife Legs ThickiŇa20211107	ignored52   r'   )r   extr+   r,   upload_dater-   r$   r.   r/   r0   r1   r!   )r   	info_dictz@https://thumbs2.redgifs.com/SqueakyHelplessWisent-mobile.mp4#t=0z1https://www.redgifs.com/ifr/squeakyhelplesswisentc                 C   s4   |  | }| jd| d|dd}| |d S )Nzgifs/z
?views=yeszDownloading video inforC   r   )	_match_idlowerrW   rB   )r=   r   r?   
video_infor   r   r   _real_extract   s
   zRedGifsIE._real_extractN)ro   rp   rq   
_VALID_URLintlist_TESTSr}   r   r   r   r   r:   t   sb    2r:   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=LesbianLesbianz/RedGifs search for Lesbian, ordered by trendingr   r+   descriptiond   r   ry   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   ry   playlist_countc              	   C   s   |  |d}tj|}|dstddd|dd }|ddd }|g|d	< | d
||d dd d}| |||d| d| S )NrY   r!   zInvalid query tagsT)rM   r   order)trendingsearch_textzgifs/searchr   )r   r   typezRedGifs search for , ordered by )	_match_valid_urlgroupurllibparseparse_qsr3   r   rn   playlist_result)r=   r   	query_strrY   r!   r   entriesr   r   r   r}      s   


zRedGifsSearchIE._real_extractNro   rp   rq   IE_DESCr~   rk   r   r}   r   r   r   r   r      s6    

r   c                	   @   sn   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gZdd ZdS )RedGifsUserIEzRedgifs userzRhttps?://(?:www\.)?redgifs\.com/users/(?P<username>[^/?#]+)(?:\?(?P<query>[^#]+))?r   z(https://www.redgifs.com/users/lamsinka89
lamsinka89z*RedGifs user lamsinka89, ordered by recentr   i  r   z/https://www.redgifs.com/users/lamsinka89?page=3zlamsinka89?page=3r   z:https://www.redgifs.com/users/lamsinka89?order=best&type=gzlamsinka89?order=best&type=gz(RedGifs user lamsinka89, ordered by bestz'https://www.redgifs.com/users/ignored52z,https://github.com/yt-dlp/yt-dlp/issues/7382ru   z)RedGifs user ignored52, ordered by recenty   )r   rD   ry   r   c              	   C   s   |  |dd\}}|r| d| n|}tj|}|ddd }| d| d||d	d d
}| |||d| d| S )NusernamerY   ?r   )recentr   zusers/z/searchr   )r   r   zRedGifs user r   )r   r   r   r   r   r3   rn   r   )r=   r   r   r   playlist_idrY   r   r   r   r   r   r}     s   zRedGifsUserIE._real_extractNr   r   r   r   r   r      sF    


(r   )ri   urllib.parser   commonr   networking.exceptionsr   utilsr   r   r   r	   r
   r   r:   r   r   r   r   r   r   <module>   s    	e;6