20 def cookie_to_dict(cookie
):
23 'value': cookie
.value
,
25 if cookie
.port_specified
:
26 cookie_dict
['port'] = cookie
.port
27 if cookie
.domain_specified
:
28 cookie_dict
['domain'] = cookie
.domain
29 if cookie
.path_specified
:
30 cookie_dict
['path'] = cookie
.path
31 if cookie
.expires
is not None:
32 cookie_dict
['expires'] = cookie
.expires
33 if cookie
.secure
is not None:
34 cookie_dict
['secure'] = cookie
.secure
35 if cookie
.discard
is not None:
36 cookie_dict
['discard'] = cookie
.discard
37 with contextlib
.suppress(TypeError):
38 if (cookie
.has_nonstandard_attr('httpOnly')
39 or cookie
.has_nonstandard_attr('httponly')
40 or cookie
.has_nonstandard_attr('HttpOnly')):
41 cookie_dict
['httponly'] = True
45 def cookie_jar_to_list(cookie_jar
):
46 return [cookie_to_dict(cookie
) for cookie
in cookie_jar
]
49 class PhantomJSwrapper
:
50 """PhantomJS wrapper class
52 This class is experimental.
55 INSTALL_HINT
= 'Please download it from https://phantomjs.org/download.html'
58 phantom.onError = function(msg, trace) {{
59 var msgStack = ['PHANTOM ERROR: ' + msg];
60 if(trace && trace.length) {{
61 msgStack.push('TRACE:');
62 trace.forEach(function(t) {{
63 msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line
64 + (t.function ? ' (in function ' + t.function +')' : ''));
67 console.error(msgStack.join('\n'));
73 var page = require('webpage').create();
74 var fs = require('fs');
75 var read = {{ mode: 'r', charset: 'utf-8' }};
76 var write = {{ mode: 'w', charset: 'utf-8' }};
77 JSON.parse(fs.read("{cookies}", read)).forEach(function(x) {{
80 page.settings.resourceTimeout = {timeout};
81 page.settings.userAgent = "{ua}";
82 page.onLoadStarted = function() {{
83 page.evaluate(function() {{
84 delete window._phantom;
85 delete window.callPhantom;
88 var saveAndExit = function() {{
89 fs.write("{html}", page.content, write);
90 fs.write("{cookies}", JSON.stringify(phantom.cookies), write);
93 page.onLoadFinished = function(status) {{
94 if(page.url === "") {{
95 page.setContent(fs.read("{html}", read), "{url}");
104 _TMP_FILE_NAMES
= ['script', 'html', 'cookies']
108 return get_exe_version('phantomjs', version_re
=r
'([0-9.]+)')
110 def __init__(self
, extractor
, required_version
=None, timeout
=10000):
113 self
.exe
= check_executable('phantomjs', ['-v'])
115 raise ExtractorError(f
'PhantomJS not found, {self.INSTALL_HINT}', expected
=True)
117 self
.extractor
= extractor
120 version
= self
._version
()
121 if is_outdated_version(version
, required_version
):
122 self
.extractor
._downloader
.report_warning(
123 'Your copy of PhantomJS is outdated, update it to version '
124 f
'{required_version} or newer if you encounter any errors.')
126 for name
in self
._TMP
_FILE
_NAMES
:
127 tmp
= tempfile
.NamedTemporaryFile(delete
=False)
129 self
._TMP
_FILES
[name
] = tmp
131 self
.options
= collections
.ChainMap({
134 x
: self
._TMP
_FILES
[x
].name
.replace('\\', '\\\\').replace('"', '\\"')
135 for x
in self
._TMP
_FILE
_NAMES
139 for name
in self
._TMP
_FILE
_NAMES
:
140 with contextlib
.suppress(OSError, KeyError):
141 os
.remove(self
._TMP
_FILES
[name
].name
)
143 def _save_cookies(self
, url
):
144 cookies
= cookie_jar_to_list(self
.extractor
.cookiejar
)
145 for cookie
in cookies
:
146 if 'path' not in cookie
:
148 if 'domain' not in cookie
:
149 cookie
['domain'] = urllib
.parse
.urlparse(url
).netloc
150 with
open(self
._TMP
_FILES
['cookies'].name
, 'wb') as f
:
151 f
.write(json
.dumps(cookies
).encode())
153 def _load_cookies(self
):
154 with
open(self
._TMP
_FILES
['cookies'].name
, 'rb') as f
:
155 cookies
= json
.loads(f
.read().decode('utf-8'))
156 for cookie
in cookies
:
157 if cookie
['httponly'] is True:
158 cookie
['rest'] = {'httpOnly': None}
159 if 'expiry' in cookie
:
160 cookie
['expire_time'] = cookie
['expiry']
161 self
.extractor
._set
_cookie
(**cookie
)
163 def get(self
, url
, html
=None, video_id
=None, note
=None, note2
='Executing JS on webpage', headers
={}, jscode
='saveAndExit();'):
165 Downloads webpage (if needed) and executes JS
169 html: optional, html code of website
171 note: optional, displayed when downloading webpage
172 note2: optional, displayed when executing JS
173 headers: custom http headers
174 jscode: code to be executed when page is loaded
177 * downloaded website (after JS execution)
178 * anything you print with `console.log` (but not inside `page.execute`!)
180 In most cases you don't need to add any `jscode`.
181 It is executed in `page.onLoadFinished`.
182 `saveAndExit();` is mandatory, use it instead of `phantom.exit()`
183 It is possible to wait for some element on the webpage, e.g.
184 var check = function() {
185 var elementFound = page.evaluate(function() {
186 return document.querySelector('#b.done') !== null;
191 window.setTimeout(check, 500);
194 page.evaluate(function(){
195 document.querySelector('#a').click();
199 if 'saveAndExit();' not in jscode
:
200 raise ExtractorError('`saveAndExit();` not found in `jscode`')
202 html
= self
.extractor
._download
_webpage
(url
, video_id
, note
=note
, headers
=headers
)
203 with
open(self
._TMP
_FILES
['html'].name
, 'wb') as f
:
204 f
.write(html
.encode())
206 self
._save
_cookies
(url
)
208 user_agent
= headers
.get('User-Agent') or self
.extractor
.get_param('http_headers')['User-Agent']
209 jscode
= self
._TEMPLATE
.format_map(self
.options
.new_child({
211 'ua': user_agent
.replace('"', '\\"'),
215 stdout
= self
.execute(jscode
, video_id
, note
=note2
)
217 with
open(self
._TMP
_FILES
['html'].name
, 'rb') as f
:
218 html
= f
.read().decode('utf-8')
223 def execute(self
, jscode
, video_id
=None, *, note
='Executing JS'):
224 """Execute JS and return stdout"""
225 if 'phantom.exit();' not in jscode
:
226 jscode
+= ';\nphantom.exit();'
227 jscode
= self
._BASE
_JS
+ jscode
229 with
open(self
._TMP
_FILES
['script'].name
, 'w', encoding
='utf-8') as f
:
231 self
.extractor
.to_screen(f
'{format_field(video_id, None, "%s: ")}{note}')
233 cmd
= [self
.exe
, '--ssl-protocol=any', self
._TMP
_FILES
['script'].name
]
234 self
.extractor
.write_debug(f
'PhantomJS command line: {shell_quote(cmd)}')
236 stdout
, stderr
, returncode
= Popen
.run(cmd
, timeout
=self
.options
['timeout'] / 1000,
237 text
=True, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
238 except Exception as e
:
239 raise ExtractorError(f
'{note} failed: Unable to run PhantomJS binary', cause
=e
)
241 raise ExtractorError(f
'{note} failed with returncode {returncode}:\n{stderr.strip()}')