
    V|h!                         S SK r S SKrS SKrS SKrS SKrS SKrS SKrSSKJ	r	J
r
JrJrJrJrJr  S rS r " S S5      rg)    N   )ExtractorErrorPopencheck_executableformat_fieldget_exe_versionis_outdated_versionshell_quotec                    U R                   U R                  S.nU R                  (       a  U R                  US'   U R                  (       a  U R
                  US'   U R                  (       a  U R                  US'   U R                  b  U R                  US'   U R                  b  U R                  US'   U R                  b  U R                  US'   [        R                  " [        5         U R                  S5      (       d,  U R                  S	5      (       d  U R                  S
5      (       a  SUS	'   S S S 5        U$ ! , (       d  f       U$ = f)N)namevalueportdomainpathexpiressecurediscardhttpOnlyhttponlyHttpOnlyT)r   r   port_specifiedr   domain_specifiedr   path_specifiedr   r   r   r   
contextlibsuppress	TypeErrorhas_nonstandard_attr)cookiecookie_dicts     S/home/james-whalen/.local/lib/python3.13/site-packages/yt_dlp/extractor/openload.pycookie_to_dictr!      s   K $kkF &H$kkF~~!!'I}}  &H~~!!'I			Y	'''
33..z::..z::&*K
#	 
(
  
(	'
 s   (AD::
E	c                 D    U  Vs/ s H  n[        U5      PM     sn$ s  snf N)r!   )
cookie_jarr   s     r    cookie_jar_to_listr%   -   s    1;<vN6"<<<s   c                       \ rS rSrSrSrSrSr/ SQr\	S 5       r
SS	 jrS
 rS rS rSSSS0 S4S jrSSS.S jjrSrg)PhantomJSwrapper1   z5PhantomJS wrapper class

This class is experimental.
z;Please download it from https://phantomjs.org/download.htmla  
        phantom.onError = function(msg, trace) {{
          var msgStack = ['PHANTOM ERROR: ' + msg];
          if(trace && trace.length) {{
            msgStack.push('TRACE:');
            trace.forEach(function(t) {{
              msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line
                + (t.function ? ' (in function ' + t.function +')' : ''));
            }});
          }}
          console.error(msgStack.join('\n'));
          phantom.exit(1);
        }};
    a	  
        var page = require('webpage').create();
        var fs = require('fs');
        var read = {{ mode: 'r', charset: 'utf-8' }};
        var write = {{ mode: 'w', charset: 'utf-8' }};
        JSON.parse(fs.read("{cookies}", read)).forEach(function(x) {{
          phantom.addCookie(x);
        }});
        page.settings.resourceTimeout = {timeout};
        page.settings.userAgent = "{ua}";
        page.onLoadStarted = function() {{
          page.evaluate(function() {{
            delete window._phantom;
            delete window.callPhantom;
          }});
        }};
        var saveAndExit = function() {{
          fs.write("{html}", page.content, write);
          fs.write("{cookies}", JSON.stringify(phantom.cookies), write);
          phantom.exit();
        }};
        page.onLoadFinished = function(status) {{
          if(page.url === "") {{
            page.setContent(fs.read("{html}", read), "{url}");
          }}
          else {{
            {jscode}
          }}
        }};
        page.open("");
    )scripthtmlcookiesc                      [        SSS9$ )N	phantomjsz	([0-9.]+))
version_re)r        r    _versionPhantomJSwrapper._versionj   s    {|DDr0   Nc                    0 U l         [        SS/5      U l        U R                  (       d  [        SU R                   3SS9eXl        U(       aI  U R                  5       n[        XB5      (       a)  U R
                  R                  R                  SU S35        U R                   H5  n[        R                  " SS	9nUR                  5         X`R                   U'   M7     [        R                  " S
U0U R                   Vs0 s H<  nXpR                   U   R                   R#                  SS5      R#                  SS5      _M>     sn5      U l        g s  snf )Nr-   z-vzPhantomJS not found, T)expectedz9Your copy of PhantomJS is outdated, update it to version z& or newer if you encounter any errors.F)deletetimeout\z\\"\")
_TMP_FILESr   exer   INSTALL_HINT	extractorr1   r	   _downloaderreport_warning_TMP_FILE_NAMEStempfileNamedTemporaryFileclosecollectionsChainMapr   replaceoptions)selfr=   required_versionr6   versionr   tmpxs           r    __init__PhantomJSwrapper.__init__n   s/   #K$8xx #89J9J8K!LW[\\"mmoG"7==**99O'((NPQ ((D--U;CIIK$'OOD! )
 #++w-
 ))
) q!&&..tV<DDS%PP)

s   >AE
c                     U R                    HX  n[        R                  " [        [        5         [
        R                  " U R                  U   R                  5        S S S 5        MZ     g ! , (       d  f       Ml  = fr#   )	r@   r   r   OSErrorKeyErrorosremover:   r   )rH   r   s     r    __del__PhantomJSwrapper.__del__   sL    ((D$$Wh7		$//$/445 87 )77s   .A**
A9	c                    [        U R                  R                  5      nU HB  nSU;  a  SUS'   SU;  d  M  [        R                  R                  U5      R                  US'   MD     [        U R                  S   R                  S5       nUR                  [        R                  " U5      R                  5       5        S S S 5        g ! , (       d  f       g = f)Nr   /r   r+   wb)r%   r=   	cookiejarurllibparseurlparsenetlocopenr:   r   writejsondumpsencode)rH   urlr+   r   fs        r    _save_cookiesPhantomJSwrapper._save_cookies   s    $T^^%=%=>FV#!$vv%#)<<#8#8#=#D#Dx 	 
 $//),1148AGGDJJw'..01 988s   4C
Cc                 h   [        U R                  S   R                  S5       n[        R                  " UR                  5       R                  S5      5      nS S S 5        W H<  nUS   SL a  SS 0US'   SU;   a  US   US	'   U R                  R                  " S
0 UD6  M>     g ! , (       d  f       NQ= f)Nr+   rbutf-8r   Tr   restexpiryexpire_timer/   )	r^   r:   r   r`   loadsreaddecoder=   _set_cookie)rH   rd   r+   r   s       r    _load_cookiesPhantomJSwrapper._load_cookies   s    $//),1148Ajj!9:G 9Fj!T)",d!3v6!(.x(8}%NN&&00  98s   4B##
B1zExecuting JS on webpagesaveAndExit();c           	      <   SU;  a  [        S5      eU(       d  U R                  R                  XXFS9n[        U R                  S   R
                  S5       nUR                  UR                  5       5        SSS5        U R                  U5        UR                  S5      =(       d    U R                  R                  S5      S   n	U R                  R                  U R                  R                  UU	R                  S	S
5      US.5      5      nU R!                  XsUS9n
[        U R                  S   R
                  S5       nUR#                  5       R%                  S5      nSSS5        U R'                  5         X*4$ ! , (       d  f       GN= f! , (       d  f       N3= f)a  
Downloads webpage (if needed) and executes JS

Params:
    url: website url
    html: optional, html code of website
    video_id: video id
    note: optional, displayed when downloading webpage
    note2: optional, displayed when executing JS
    headers: custom http headers
    jscode: code to be executed when page is loaded

Returns tuple with:
    * downloaded website (after JS execution)
    * anything you print with `console.log` (but not inside `page.execute`!)

In most cases you don't need to add any `jscode`.
It is executed in `page.onLoadFinished`.
`saveAndExit();` is mandatory, use it instead of `phantom.exit()`
It is possible to wait for some element on the webpage, e.g.
    var check = function() {
      var elementFound = page.evaluate(function() {
        return document.querySelector('#b.done') !== null;
      });
      if(elementFound)
        saveAndExit();
      else
        window.setTimeout(check, 500);
    }

    page.evaluate(function(){
      document.querySelector('#a').click();
    });
    check();
rs   z&`saveAndExit();` not found in `jscode`)noteheadersr*   rX   Nz
User-Agenthttp_headersr8   r9   )rc   uajscoderu   rh   ri   )r   r=   _download_webpager^   r:   r   r_   rb   re   get	get_param	_TEMPLATE
format_maprG   	new_childrF   executern   ro   rq   )rH   rc   r*   video_idru   note2rv   ry   rd   
user_agentstdouts              r    r|   PhantomJSwrapper.get   sN   H 6) !IJJ>>33C3^D$//&)..5GGDKKM" 6 	3[[.h$..2J2J>2Z[g2h
**4<<+A+A$$S%0C
 ,  fU;$//&)..5668??7+D 6|% 65 65s    E;  F;
F

FzExecuting JSrz   c                   SU;  a  US-  nU R                   U-   n[        U R                  S   R                  SSS9 nUR	                  U5        SSS5        U R
                  R                  [        USS5       U 35        U R                  S	U R                  S   R                  /nU R
                  R                  S
[        U5       35         [        R                  " XPR                  S   S-  S[        R                  [        R                  S9u  pgnU(       a!  [#        U SU SUR%                  5        35      eU$ ! , (       d  f       N= f! [          a  n	[#        U S3U	S9eSn	A	ff = f)zExecute JS and return stdoutzphantom.exit();z;
phantom.exit();r)   wri   )encodingNz%s: z--ssl-protocol=anyzPhantomJS command line: r6   i  T)r6   textr   stderrz' failed: Unable to run PhantomJS binary)causez failed with returncode z:
)_BASE_JSr^   r:   r   r_   r=   	to_screenr   r;   write_debugr
   r   runrG   
subprocessPIPE	Exceptionr   strip)
rH   ry   r   ru   rd   cmdr   r   
returncodees
             r    r   PhantomJSwrapper.execute   sX   F***F'$//(+00#HAGGFO I  L4$H#I$!PQxx-tx/H/M/MN""%=k#>N=O#PQ	\).3Y@WZ^@^8<Z__]g]l]l*n&FJ  D6)A*SQWQ]Q]Q_P`!abb IH  	\ D6)P!QYZ[[	\s$   D?AE ?
E
E,E''E,)r:   r;   r=   rG   )Ni'  r#   )__name__
__module____qualname____firstlineno____doc__r<   r   r~   r@   staticmethodr1   rM   rT   re   rq   r|   r   __static_attributes__r/   r0   r    r'   r'   1   sz    
 QLHI@ 4OE E86
21 !4dB[eg  qA :x^  r0   r'   )rD   r   r`   rR   r   rA   urllib.parserZ   utilsr   r   r   r   r   r	   r
   r!   r%   r'   r/   r0   r    <module>r      s?       	     2=B Br0   