]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/facebook.py
Update changelog.
[youtubedl] / youtube_dl / extractor / facebook.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import socket
6
7 from .common import InfoExtractor
8 from ..compat import (
9 compat_etree_fromstring,
10 compat_http_client,
11 compat_urllib_error,
12 compat_urllib_parse_unquote,
13 compat_urllib_parse_unquote_plus,
14 )
15 from ..utils import (
16 clean_html,
17 error_to_compat_str,
18 ExtractorError,
19 get_element_by_id,
20 int_or_none,
21 js_to_json,
22 limit_length,
23 parse_count,
24 sanitized_Request,
25 try_get,
26 urlencode_postdata,
27 )
28
29
30 class FacebookIE(InfoExtractor):
31 _VALID_URL = r'''(?x)
32 (?:
33 https?://
34 (?:[\w-]+\.)?(?:facebook\.com|facebookcorewwwi\.onion)/
35 (?:[^#]*?\#!/)?
36 (?:
37 (?:
38 video/video\.php|
39 photo\.php|
40 video\.php|
41 video/embed|
42 story\.php
43 )\?(?:.*?)(?:v|video_id|story_fbid)=|
44 [^/]+/videos/(?:[^/]+/)?|
45 [^/]+/posts/|
46 groups/[^/]+/permalink/
47 )|
48 facebook:
49 )
50 (?P<id>[0-9]+)
51 '''
52 _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1'
53 _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1'
54 _NETRC_MACHINE = 'facebook'
55 IE_NAME = 'facebook'
56
57 _CHROME_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36'
58
59 _VIDEO_PAGE_TEMPLATE = 'https://www.facebook.com/video/video.php?v=%s'
60 _VIDEO_PAGE_TAHOE_TEMPLATE = 'https://www.facebook.com/video/tahoe/async/%s/?chain=true&isvideo=true&payloadtype=primary'
61
62 _TESTS = [{
63 'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf',
64 'md5': '6a40d33c0eccbb1af76cf0485a052659',
65 'info_dict': {
66 'id': '637842556329505',
67 'ext': 'mp4',
68 'title': 're:Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam',
69 'uploader': 'Tennis on Facebook',
70 'upload_date': '20140908',
71 'timestamp': 1410199200,
72 },
73 'skip': 'Requires logging in',
74 }, {
75 'url': 'https://www.facebook.com/video.php?v=274175099429670',
76 'info_dict': {
77 'id': '274175099429670',
78 'ext': 'mp4',
79 'title': 're:^Asif Nawab Butt posted a video',
80 'uploader': 'Asif Nawab Butt',
81 'upload_date': '20140506',
82 'timestamp': 1399398998,
83 'thumbnail': r're:^https?://.*',
84 },
85 'expected_warnings': [
86 'title'
87 ]
88 }, {
89 'note': 'Video with DASH manifest',
90 'url': 'https://www.facebook.com/video.php?v=957955867617029',
91 'md5': 'b2c28d528273b323abe5c6ab59f0f030',
92 'info_dict': {
93 'id': '957955867617029',
94 'ext': 'mp4',
95 'title': 'When you post epic content on instagram.com/433 8 million followers, this is ...',
96 'uploader': 'Demy de Zeeuw',
97 'upload_date': '20160110',
98 'timestamp': 1452431627,
99 },
100 'skip': 'Requires logging in',
101 }, {
102 'url': 'https://www.facebook.com/maxlayn/posts/10153807558977570',
103 'md5': '037b1fa7f3c2d02b7a0d7bc16031ecc6',
104 'info_dict': {
105 'id': '544765982287235',
106 'ext': 'mp4',
107 'title': '"What are you doing running in the snow?"',
108 'uploader': 'FailArmy',
109 },
110 'skip': 'Video gone',
111 }, {
112 'url': 'https://m.facebook.com/story.php?story_fbid=1035862816472149&id=116132035111903',
113 'md5': '1deb90b6ac27f7efcf6d747c8a27f5e3',
114 'info_dict': {
115 'id': '1035862816472149',
116 'ext': 'mp4',
117 'title': 'What the Flock Is Going On In New Zealand Credit: ViralHog',
118 'uploader': 'S. Saint',
119 },
120 'skip': 'Video gone',
121 }, {
122 'note': 'swf params escaped',
123 'url': 'https://www.facebook.com/barackobama/posts/10153664894881749',
124 'md5': '97ba073838964d12c70566e0085c2b91',
125 'info_dict': {
126 'id': '10153664894881749',
127 'ext': 'mp4',
128 'title': 'Average time to confirm recent Supreme Court nominees: 67 days Longest it\'s t...',
129 'thumbnail': r're:^https?://.*',
130 'timestamp': 1456259628,
131 'upload_date': '20160223',
132 'uploader': 'Barack Obama',
133 },
134 }, {
135 # have 1080P, but only up to 720p in swf params
136 'url': 'https://www.facebook.com/cnn/videos/10155529876156509/',
137 'md5': '9571fae53d4165bbbadb17a94651dcdc',
138 'info_dict': {
139 'id': '10155529876156509',
140 'ext': 'mp4',
141 'title': 'She survived the holocaust — and years later, she’s getting her citizenship s...',
142 'timestamp': 1477818095,
143 'upload_date': '20161030',
144 'uploader': 'CNN',
145 'thumbnail': r're:^https?://.*',
146 'view_count': int,
147 },
148 }, {
149 # bigPipe.onPageletArrive ... onPageletArrive pagelet_group_mall
150 'url': 'https://www.facebook.com/yaroslav.korpan/videos/1417995061575415/',
151 'info_dict': {
152 'id': '1417995061575415',
153 'ext': 'mp4',
154 'title': 'md5:1db063d6a8c13faa8da727817339c857',
155 'timestamp': 1486648217,
156 'upload_date': '20170209',
157 'uploader': 'Yaroslav Korpan',
158 },
159 'params': {
160 'skip_download': True,
161 },
162 }, {
163 'url': 'https://www.facebook.com/LaGuiaDelVaron/posts/1072691702860471',
164 'info_dict': {
165 'id': '1072691702860471',
166 'ext': 'mp4',
167 'title': 'md5:ae2d22a93fbb12dad20dc393a869739d',
168 'timestamp': 1477305000,
169 'upload_date': '20161024',
170 'uploader': 'La Guía Del Varón',
171 'thumbnail': r're:^https?://.*',
172 },
173 'params': {
174 'skip_download': True,
175 },
176 }, {
177 'url': 'https://www.facebook.com/groups/1024490957622648/permalink/1396382447100162/',
178 'info_dict': {
179 'id': '1396382447100162',
180 'ext': 'mp4',
181 'title': 'md5:19a428bbde91364e3de815383b54a235',
182 'timestamp': 1486035494,
183 'upload_date': '20170202',
184 'uploader': 'Elisabeth Ahtn',
185 },
186 'params': {
187 'skip_download': True,
188 },
189 }, {
190 'url': 'https://www.facebook.com/video.php?v=10204634152394104',
191 'only_matching': True,
192 }, {
193 'url': 'https://www.facebook.com/amogood/videos/1618742068337349/?fref=nf',
194 'only_matching': True,
195 }, {
196 'url': 'https://www.facebook.com/ChristyClarkForBC/videos/vb.22819070941/10153870694020942/?type=2&theater',
197 'only_matching': True,
198 }, {
199 'url': 'facebook:544765982287235',
200 'only_matching': True,
201 }, {
202 'url': 'https://www.facebook.com/groups/164828000315060/permalink/764967300301124/',
203 'only_matching': True,
204 }, {
205 'url': 'https://zh-hk.facebook.com/peoplespower/videos/1135894589806027/',
206 'only_matching': True,
207 }, {
208 'url': 'https://www.facebookcorewwwi.onion/video.php?v=274175099429670',
209 'only_matching': True,
210 }, {
211 # no title
212 'url': 'https://www.facebook.com/onlycleverentertainment/videos/1947995502095005/',
213 'only_matching': True,
214 }, {
215 'url': 'https://www.facebook.com/WatchESLOne/videos/359649331226507/',
216 'info_dict': {
217 'id': '359649331226507',
218 'ext': 'mp4',
219 'title': '#ESLOne VoD - Birmingham Finals Day#1 Fnatic vs. @Evil Geniuses',
220 'uploader': 'ESL One Dota 2',
221 },
222 'params': {
223 'skip_download': True,
224 },
225 }]
226
227 @staticmethod
228 def _extract_urls(webpage):
229 urls = []
230 for mobj in re.finditer(
231 r'<iframe[^>]+?src=(["\'])(?P<url>https?://www\.facebook\.com/(?:video/embed|plugins/video\.php).+?)\1',
232 webpage):
233 urls.append(mobj.group('url'))
234 # Facebook API embed
235 # see https://developers.facebook.com/docs/plugins/embedded-video-player
236 for mobj in re.finditer(r'''(?x)<div[^>]+
237 class=(?P<q1>[\'"])[^\'"]*\bfb-(?:video|post)\b[^\'"]*(?P=q1)[^>]+
238 data-href=(?P<q2>[\'"])(?P<url>(?:https?:)?//(?:www\.)?facebook.com/.+?)(?P=q2)''', webpage):
239 urls.append(mobj.group('url'))
240 return urls
241
242 def _login(self):
243 useremail, password = self._get_login_info()
244 if useremail is None:
245 return
246
247 login_page_req = sanitized_Request(self._LOGIN_URL)
248 self._set_cookie('facebook.com', 'locale', 'en_US')
249 login_page = self._download_webpage(login_page_req, None,
250 note='Downloading login page',
251 errnote='Unable to download login page')
252 lsd = self._search_regex(
253 r'<input type="hidden" name="lsd" value="([^"]*)"',
254 login_page, 'lsd')
255 lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd')
256
257 login_form = {
258 'email': useremail,
259 'pass': password,
260 'lsd': lsd,
261 'lgnrnd': lgnrnd,
262 'next': 'http://facebook.com/home.php',
263 'default_persistent': '0',
264 'legacy_return': '1',
265 'timezone': '-60',
266 'trynum': '1',
267 }
268 request = sanitized_Request(self._LOGIN_URL, urlencode_postdata(login_form))
269 request.add_header('Content-Type', 'application/x-www-form-urlencoded')
270 try:
271 login_results = self._download_webpage(request, None,
272 note='Logging in', errnote='unable to fetch login page')
273 if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
274 error = self._html_search_regex(
275 r'(?s)<div[^>]+class=(["\']).*?login_error_box.*?\1[^>]*><div[^>]*>.*?</div><div[^>]*>(?P<error>.+?)</div>',
276 login_results, 'login error', default=None, group='error')
277 if error:
278 raise ExtractorError('Unable to login: %s' % error, expected=True)
279 self._downloader.report_warning('unable to log in: bad username/password, or exceeded login rate limit (~3/min). Check credentials or wait.')
280 return
281
282 fb_dtsg = self._search_regex(
283 r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg', default=None)
284 h = self._search_regex(
285 r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h', default=None)
286
287 if not fb_dtsg or not h:
288 return
289
290 check_form = {
291 'fb_dtsg': fb_dtsg,
292 'h': h,
293 'name_action_selected': 'dont_save',
294 }
295 check_req = sanitized_Request(self._CHECKPOINT_URL, urlencode_postdata(check_form))
296 check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
297 check_response = self._download_webpage(check_req, None,
298 note='Confirming login')
299 if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
300 self._downloader.report_warning('Unable to confirm login, you have to login in your browser and authorize the login.')
301 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
302 self._downloader.report_warning('unable to log in: %s' % error_to_compat_str(err))
303 return
304
305 def _real_initialize(self):
306 self._login()
307
308 def _extract_from_url(self, url, video_id, fatal_if_no_video=True):
309 req = sanitized_Request(url)
310 req.add_header('User-Agent', self._CHROME_USER_AGENT)
311 webpage = self._download_webpage(req, video_id)
312
313 video_data = None
314
315 def extract_video_data(instances):
316 for item in instances:
317 if item[1][0] == 'VideoConfig':
318 video_item = item[2][0]
319 if video_item.get('video_id'):
320 return video_item['videoData']
321
322 server_js_data = self._parse_json(self._search_regex(
323 r'handleServerJS\(({.+})(?:\);|,")', webpage,
324 'server js data', default='{}'), video_id, fatal=False)
325
326 if server_js_data:
327 video_data = extract_video_data(server_js_data.get('instances', []))
328
329 def extract_from_jsmods_instances(js_data):
330 if js_data:
331 return extract_video_data(try_get(
332 js_data, lambda x: x['jsmods']['instances'], list) or [])
333
334 if not video_data:
335 server_js_data = self._parse_json(
336 self._search_regex(
337 r'bigPipe\.onPageletArrive\(({.+?})\)\s*;\s*}\s*\)\s*,\s*["\']onPageletArrive\s+(?:pagelet_group_mall|permalink_video_pagelet|hyperfeed_story_id_\d+)',
338 webpage, 'js data', default='{}'),
339 video_id, transform_source=js_to_json, fatal=False)
340 video_data = extract_from_jsmods_instances(server_js_data)
341
342 if not video_data:
343 if not fatal_if_no_video:
344 return webpage, False
345 m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
346 if m_msg is not None:
347 raise ExtractorError(
348 'The video is not available, Facebook said: "%s"' % m_msg.group(1),
349 expected=True)
350 elif '>You must log in to continue' in webpage:
351 self.raise_login_required()
352
353 # Video info not in first request, do a secondary request using
354 # tahoe player specific URL
355 tahoe_data = self._download_webpage(
356 self._VIDEO_PAGE_TAHOE_TEMPLATE % video_id, video_id,
357 data=urlencode_postdata({
358 '__a': 1,
359 '__pc': self._search_regex(
360 r'pkg_cohort["\']\s*:\s*["\'](.+?)["\']', webpage,
361 'pkg cohort', default='PHASED:DEFAULT'),
362 '__rev': self._search_regex(
363 r'client_revision["\']\s*:\s*(\d+),', webpage,
364 'client revision', default='3944515'),
365 'fb_dtsg': self._search_regex(
366 r'"DTSGInitialData"\s*,\s*\[\]\s*,\s*{\s*"token"\s*:\s*"([^"]+)"',
367 webpage, 'dtsg token', default=''),
368 }),
369 headers={
370 'Content-Type': 'application/x-www-form-urlencoded',
371 })
372 tahoe_js_data = self._parse_json(
373 self._search_regex(
374 r'for\s+\(\s*;\s*;\s*\)\s*;(.+)', tahoe_data,
375 'tahoe js data', default='{}'),
376 video_id, fatal=False)
377 video_data = extract_from_jsmods_instances(tahoe_js_data)
378
379 if not video_data:
380 raise ExtractorError('Cannot parse data')
381
382 subtitles = {}
383 formats = []
384 for f in video_data:
385 format_id = f['stream_type']
386 if f and isinstance(f, dict):
387 f = [f]
388 if not f or not isinstance(f, list):
389 continue
390 for quality in ('sd', 'hd'):
391 for src_type in ('src', 'src_no_ratelimit'):
392 src = f[0].get('%s_%s' % (quality, src_type))
393 if src:
394 preference = -10 if format_id == 'progressive' else 0
395 if quality == 'hd':
396 preference += 5
397 formats.append({
398 'format_id': '%s_%s_%s' % (format_id, quality, src_type),
399 'url': src,
400 'preference': preference,
401 })
402 dash_manifest = f[0].get('dash_manifest')
403 if dash_manifest:
404 formats.extend(self._parse_mpd_formats(
405 compat_etree_fromstring(compat_urllib_parse_unquote_plus(dash_manifest))))
406 subtitles_src = f[0].get('subtitles_src')
407 if subtitles_src:
408 subtitles.setdefault('en', []).append({'url': subtitles_src})
409 if not formats:
410 raise ExtractorError('Cannot find video formats')
411
412 # Downloads with browser's User-Agent are rate limited. Working around
413 # with non-browser User-Agent.
414 for f in formats:
415 f.setdefault('http_headers', {})['User-Agent'] = 'facebookexternalhit/1.1'
416
417 self._sort_formats(formats)
418
419 video_title = self._html_search_regex(
420 r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>([^<]*)</h2>', webpage,
421 'title', default=None)
422 if not video_title:
423 video_title = self._html_search_regex(
424 r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>',
425 webpage, 'alternative title', default=None)
426 if not video_title:
427 video_title = self._html_search_meta(
428 'description', webpage, 'title', default=None)
429 if video_title:
430 video_title = limit_length(video_title, 80)
431 else:
432 video_title = 'Facebook video #%s' % video_id
433 uploader = clean_html(get_element_by_id(
434 'fbPhotoPageAuthorName', webpage)) or self._search_regex(
435 r'ownerName\s*:\s*"([^"]+)"', webpage, 'uploader',
436 default=None) or self._og_search_title(webpage, fatal=False)
437 timestamp = int_or_none(self._search_regex(
438 r'<abbr[^>]+data-utime=["\'](\d+)', webpage,
439 'timestamp', default=None))
440 thumbnail = self._html_search_meta(['og:image', 'twitter:image'], webpage)
441
442 view_count = parse_count(self._search_regex(
443 r'\bviewCount\s*:\s*["\']([\d,.]+)', webpage, 'view count',
444 default=None))
445
446 info_dict = {
447 'id': video_id,
448 'title': video_title,
449 'formats': formats,
450 'uploader': uploader,
451 'timestamp': timestamp,
452 'thumbnail': thumbnail,
453 'view_count': view_count,
454 'subtitles': subtitles,
455 }
456
457 return webpage, info_dict
458
459 def _real_extract(self, url):
460 video_id = self._match_id(url)
461
462 real_url = self._VIDEO_PAGE_TEMPLATE % video_id if url.startswith('facebook:') else url
463 webpage, info_dict = self._extract_from_url(real_url, video_id, fatal_if_no_video=False)
464
465 if info_dict:
466 return info_dict
467
468 if '/posts/' in url:
469 video_id_json = self._search_regex(
470 r'(["\'])video_ids\1\s*:\s*(?P<ids>\[.+?\])', webpage, 'video ids', group='ids',
471 default='')
472 if video_id_json:
473 entries = [
474 self.url_result('facebook:%s' % vid, FacebookIE.ie_key())
475 for vid in self._parse_json(video_id_json, video_id)]
476 return self.playlist_result(entries, video_id)
477
478 # Single Video?
479 video_id = self._search_regex(r'video_id:\s*"([0-9]+)"', webpage, 'single video id')
480 return self.url_result('facebook:%s' % video_id, FacebookIE.ie_key())
481 else:
482 _, info_dict = self._extract_from_url(
483 self._VIDEO_PAGE_TEMPLATE % video_id,
484 video_id, fatal_if_no_video=True)
485 return info_dict
486
487
488 class FacebookPluginsVideoIE(InfoExtractor):
489 _VALID_URL = r'https?://(?:[\w-]+\.)?facebook\.com/plugins/video\.php\?.*?\bhref=(?P<id>https.+)'
490
491 _TESTS = [{
492 'url': 'https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fgov.sg%2Fvideos%2F10154383743583686%2F&show_text=0&width=560',
493 'md5': '5954e92cdfe51fe5782ae9bda7058a07',
494 'info_dict': {
495 'id': '10154383743583686',
496 'ext': 'mp4',
497 'title': 'What to do during the haze?',
498 'uploader': 'Gov.sg',
499 'upload_date': '20160826',
500 'timestamp': 1472184808,
501 },
502 'add_ie': [FacebookIE.ie_key()],
503 }, {
504 'url': 'https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fvideo.php%3Fv%3D10204634152394104',
505 'only_matching': True,
506 }, {
507 'url': 'https://www.facebook.com/plugins/video.php?href=https://www.facebook.com/gov.sg/videos/10154383743583686/&show_text=0&width=560',
508 'only_matching': True,
509 }]
510
511 def _real_extract(self, url):
512 return self.url_result(
513 compat_urllib_parse_unquote(self._match_id(url)),
514 FacebookIE.ie_key())