3
×b`ø  ã               @   s€   d dl mZ d dlZd dlZd dlZd dlZddlmZmZ ddl	m
Z
mZmZmZmZmZ dd„ Zdd	„ ZG d
d„ deƒZdS )é    )Úunicode_literalsNé   )Úcompat_urlparseÚcompat_kwargs)Úcheck_executableÚencodeArgumentÚExtractorErrorÚget_exe_versionÚis_outdated_versionÚstd_headersc             C   s¾   | j | jdœ}| jr| j|d< | jr.| j|d< | jr>| j|d< | jd k	rR| j|d< | j	d k	rf| j	|d< | j
d k	rz| j
|d< y*| jdƒsš| jd	ƒsš| jd
ƒr¢d|d	< W n tk
r¸   Y nX |S )N)ÚnameÚvalueÚportÚdomainÚpathÚexpiresÚsecureÚdiscardÚhttpOnlyÚhttponlyÚHttpOnlyT)r   r   Úport_specifiedr   Údomain_specifiedr   Úpath_specifiedr   r   r   r   Úhas_nonstandard_attrÚ	TypeError)ÚcookieZcookie_dict© r   úNC:\Users\Broad\Desktop\BeinConnetFix\binaries\youtube_dl\extractor\openload.pyÚcookie_to_dict   s,    












r   c             C   s   dd„ | D ƒS )Nc             S   s   g | ]}t |ƒ‘qS r   )r   )Ú.0r   r   r   r   ú
<listcomp>3   s    z&cookie_jar_to_list.<locals>.<listcomp>r   )Z
cookie_jarr   r   r   Úcookie_jar_to_list2   s    r"   c               @   sb   e Zd ZdZdZdddgZedd„ ƒZdd
d„Zdd„ Z	dd„ Z
dd„ Zddddi dfdd„ZdS )ÚPhantomJSwrapperz=PhantomJS wrapper class

    This class is experimental.
    aï  
        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);
        }};
        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               C   s   t ddd�S )NÚ	phantomjsz	([0-9.]+))Ú
version_re)r	   r   r   r   r   Ú_versionj   s    zPhantomJSwrapper._versionNé'  c             C   s�   i | _ tddgƒ| _| js&tddd�‚|| _|rT| jƒ }t||ƒrT| jjjd| ƒ d|i| _	x,| j
D ]"}tjdd	�}|jƒ  || j |< qfW d S )
Nr'   z-vzMPhantomJS executable not found in PATH, download it from http://phantomjs.orgT)ÚexpectedzaYour copy of PhantomJS is outdated, update it to version %s or newer if you encounter any errors.ÚtimeoutF)Údelete)Ú
_TMP_FILESr   Úexer   Ú	extractorr)   r
   Ú_downloaderÚreport_warningÚoptionsÚ_TMP_FILE_NAMESÚtempfileÚNamedTemporaryFileÚclose)Úselfr0   Úrequired_versionr,   Úversionr   Útmpr   r   r   Ú__init__n   s"    

zPhantomJSwrapper.__init__c             C   sF   x@| j D ]6}ytj| j| jƒ W q tttfk
r<   Y qX qW d S )N)r4   ÚosÚremover.   r   ÚIOErrorÚOSErrorÚKeyError)r8   r   r   r   r   Ú__del__ˆ   s
    zPhantomJSwrapper.__del__c          
   C   s|   t | jjjƒ}x4|D ],}d|kr(d|d< d|krtj|ƒj|d< qW t| jd j	dƒ�}|j
tj|ƒjdƒƒ W d Q R X d S )Nr   ú/r   r&   Úwbzutf-8)r"   r0   r1   Ú	cookiejarr   ÚurlparseÚnetlocÚopenr.   r   ÚwriteÚjsonÚdumpsÚencode)r8   Úurlr&   r   Úfr   r   r   Ú_save_cookies�   s    
zPhantomJSwrapper._save_cookiesc             C   s‚   t | jd jdƒ�}tj|jƒ jdƒƒ}W d Q R X xJ|D ]B}|d dkrTdd i|d< d|krh|d |d	< | jjf t	|ƒŽ q8W d S )
Nr&   Úrbzutf-8r   Tr   ÚrestZexpiryÚexpire_time)
rH   r.   r   rJ   ÚloadsÚreadÚdecoder0   Ú_set_cookier   )r8   rN   r&   r   r   r   r   Ú_load_cookies™   s    
zPhantomJSwrapper._load_cookieszExecuting JS on webpagezsaveAndExit();c             C   s¾  d|krt dƒ‚|s(| jj||||d�}t| jd jdƒ�}|j|jdƒƒ W dQ R X | j|ƒ | j	}	||	d< |j
d	ƒp~td	 }
|
jd
dƒ|	d< ||	d< x.| jD ]$}| j| jjddƒjd
dƒ|	|< q W t| jd jdƒ� }|j| jjf |	Žjdƒƒ W dQ R X |dk�r| jjd|f ƒ n| jjd||f ƒ tj| jd| jd jgtjtjd�}|jƒ \}}|jdk�r~t dt|ƒ ƒ‚t| jd jdƒ�}|jƒ jdƒ}W dQ R X | jƒ  |t|ƒfS )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, for example:
            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();
        zsaveAndExit();z&`saveAndExit();` not found in `jscode`)ÚnoteÚheadersr%   rD   zutf-8NrM   z
User-Agentú"z\"ÚuaÚjscodeú\z\\r$   z%sz%s: %sz--ssl-protocol=any)ÚstdoutÚstderrr   zExecuting JS failed
:rP   )r   r0   Ú_download_webpagerH   r.   r   rI   rL   rO   r3   Úgetr   Úreplacer4   Ú	_TEMPLATEÚformatÚ	to_screenÚ
subprocessÚPopenr/   ÚPIPEÚcommunicateÚ
returncoder   rT   rU   rW   )r8   rM   r%   Úvideo_idrX   Znote2rY   r\   rN   ZreplacesÚ
user_agentÚxÚpÚoutÚerrr   r   r   ra   £   s>    $
$$
zPhantomJSwrapper.get)Nr*   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rc   r4   Ústaticmethodr)   r<   rB   rO   rW   ra   r   r   r   r   r#   6   s   ,



r#   )Ú
__future__r   rJ   r=   rf   r5   Úcompatr   r   Úutilsr   r   r   r	   r
   r   r   r"   Úobjectr#   r   r   r   r   Ú<module>   s    
