
    VGh(                         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e      Z G d	 d
e      Z G d de      Z G d de      Zy)    N   )InfoExtractor   )	HTTPError)ExtractorErrorOnDemandPagedListint_or_none	qualitiestry_getc                   H    e Zd ZddddZddddZd	 Zd
 Zd Zd Zd Z	d Z
y)RedGifsBaseIE   i  N)gifsdhdzhttps://www.redgifs.com/zhttps://www.redgifs.comzapplication/json)refereroriginzcontent-typec                 h  	 |j                  d      }t        t        | j                  j	                                     }t        |j                  d            	t        |	fd      }g }| j                  j                         D ]P  \  }}|d   j                  |      }|st        	|xs 	      }|j                  |||r||z  nd | ||      d       R |d| t        j                         ddj                  |j                  d	      xs g       xs dt        |j                  d
            |j                  d      t        |j                  d            t        |j                  d            t        |j                  d            |j                  d	      xs g |j                  d	      d|dS )Nidheightc                     | d   z  S )Nwidth )xorig_heights    a/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/yt_dlp/extractor/redgifs.py<lambda>z/RedGifsBaseIE._parse_gif_data.<locals>.<lambda>!   s    ;73K     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_ratior6   r!   r   	video_urlr   s
            @r   _parse_gif_datazRedGifsBaseIE._parse_gif_data   s   <<%E$--"4"4"678!(,,x"89x)KL!%!4!4!6 	Iv (,,Y7If&;<FNN &2>,.D "9- 	 7zB&--/"XXhll628b9FY$X\\,%?@ Z0#HLL$<=%hll7&;<%hll7&;<",,v.4"LL(
 	
r   c                     | j                  d|d      }|j                  d      st        d      d|d    | j                  d<   y )Nz)https://api.redgifs.com/v2/auth/temporaryzFetching temporary tokennotetokenzUnable to get temporary tokenzBearer authorization)_download_jsonr7   r   _API_HEADERS)rA   rC   auths      r   _fetch_oauth_tokenz RedGifsBaseIE._fetch_oauth_tokenB   sT    ""#N#+2L # Nxx  !@AA/6tG}o-F/*r   c                    dD ]Y  }d| j                   vr| j                  |       	 t        | j                         }d| |d<    | j                  d| |fd|i|} n dv rt	        d	|d    d
|      |S # t        $ rM}|rEt        |j                  t              r+|j                  j                  dk(  r| j                   d= Y d }~Ƃ d }~ww xY w)N)TFrK   zhttps://www.redgifs.com/watch/zx-customheaderzhttps://api.redgifs.com/v2/headersi  errorzRedGifs said: T)expectedrC   )	rM   rO   dictrL   r   
isinstancecauser   status)rA   eprC   kwargsfirst_attemptrQ   dataes           r   	_call_apizRedGifsBaseIE._call_apiJ   s    ( 	Md&7&77''1
t001.LXJ,W()*t**1"6]JQ]U[]	 d? >$w-!AD[cdd "  Z%CZ]H]))/:	s   6A99	CAC
	C

Cc              #      K   |dz   |d<   | j                  |||d|dz          }|d   D ]  }| j                  |        y w)Nr   pagezDownloading JSON metadata page )queryrI   gifs)r]   rF   )rA   rX   rC   r`   r_   r[   entrys          r   _fetch_pagezRedGifsBaseIE._fetch_page^   sc     qf~~.MdUVhZ,X  Z &\ 	.E&&u--	.s   AAc           	          |j                         D cg c]  \  }}||j                  ||f      d   f }}}|D ci c]  \  }}|	|| c}}S c c}}w c c}}w )Nr   )r;   r7   )rA   r`   fields
field_namedefault	api_querykeyvals           r   _prepare_api_queryz RedGifsBaseIE._prepare_api_queryf   sl     (.||~7#
G :z:1=>7	 7 *3FXS#coSFF	7 Gs   "A
A	Ac           	          t        |j                  dd      d         }t        j                  | j                  ||| j                  ||            }|r ||      S t        || j                        S )Nr_   )Nr   )r	   r7   	functoolspartialrc   rk   r   
_PAGE_SIZE)rA   rX   item_idr`   re   r_   page_fetchers          r   _paged_entrieszRedGifsBaseIE._paged_entriesm   sg    599VW5a89 ((b'4+B+B5&+QS%)|D!_/@t/__r   )__name__
__module____qualname__r9   rM   rF   rO   r]   rc   rk   rr   r   r   r   r   r      sF    H .+*L$
LG(.G`r   r   c                       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 Zy)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/   r0   upload_dater1   r(   r2   r3   r4   r5   r%   )r    	info_dictz@https://thumbs2.redgifs.com/SqueakyHelplessWisent-mobile.mp4#t=0z1https://www.redgifs.com/ifr/squeakyhelplesswisentc                     | j                  |      j                         }| j                  d| d|d      }| j                  |d         S )Nzgifs/z
?views=yeszDownloading video inforH   r   )	_match_idlowerr]   rF   )rA   r    rC   
video_infos       r   _real_extractzRedGifsIE._real_extract   sT    >>#&,,.^^H:Z((9Q $ S
##Ju$566r   N)rs   rt   ru   
_VALID_URLintlist_TESTSr   r   r   r   r>   r>   t   s    oJD))#%#
" R))#%#
" C))#%#
A0Fd7r   r>   c                   X    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 Zy)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    r~   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    r~   playlist_countc           	      ~   | j                  |      j                  d      }t        j                  j	                  |      }|j                  d      st        dd      |j                  d      d   }|j                  dd      d   }|g|d	<   | j                  d
||d dd d      }| j                  |||d| d|       S )Nr`   r%   zInvalid query tagsT)rS   r   order)trendingsearch_textzgifs/searchr   )r   r   typezRedGifs search for , ordered by )	_match_valid_urlgroupurllibparseparse_qsr7   r   rr   playlist_result)rA   r    	query_strr`   r%   r   entriess          r   r   zRedGifsSearchIE._real_extract   s    ))#.44W=	%%i0yy  !5EEyy #		'=1!4 $vm%%mYH
  ##Y(;D6ug&VX 	Xr   Nrs   rt   ru   IE_DESCr   ro   r   r   r   r   r   r   r      sx    GLJJ A$"P
 "%	
 U8"N
 "%	
 \?"N
 !	
'F<Xr   r   c            	       l    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 Zy)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/7382rz   z)RedGifs user ignored52, ordered by recenty   )r    rI   r~   r   c           	      2   | j                  |      j                  dd      \  }}|r| d| n|}t        j                  j	                  |      }|j                  dd      d   }| j                  d| d||d	d d
      }| j                  |||d| d|       S )Nusernamer`   ?r   )recentr   zusers/z/searchr   )r   r   zRedGifs user r   )r   r   r   r   r   r7   rr   r   )rA   r    r   r   playlist_idr`   r   r   s           r   r   zRedGifsUserIE._real_extract  s    "33C8>>z7S)3<
!I;/(%%i0		';/2%%xj&@+uW
 
 ##[(mH:]SXRY,Z\ 	\r   Nr   r   r   r   r   r      s    GfJJ >"%K
 "%	
 E)%K
 !	
 P4%I
 "%	
 =B!$J
 "%		
9&FP\r   r   )rm   urllib.parser   commonr   networking.exceptionsr   utilsr   r   r	   r
   r   r   r>   r   r   r   r   r   <module>r      sV      ! - b`M b`J87 87v3Xm 3Xl9\M 9\r   