2 from __future__
import unicode_literals
8 from .common
import InfoExtractor
10 compat_etree_fromstring
,
14 compat_urllib_parse_unquote
,
15 compat_urllib_parse_unquote_plus
,
35 class FacebookIE(InfoExtractor
):
39 (?:[\w-]+\.)?(?:facebook\.com|facebookcorewwwi\.onion)/
49 )\?(?:.*?)(?:v|video_id|story_fbid)=|
50 [^/]+/videos/(?:[^/]+/)?|
52 groups/[^/]+/permalink/|
59 _LOGIN_URL
= 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1'
60 _CHECKPOINT_URL
= 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1'
61 _NETRC_MACHINE
= 'facebook'
64 _VIDEO_PAGE_TEMPLATE
= 'https://www.facebook.com/video/video.php?v=%s'
65 _VIDEO_PAGE_TAHOE_TEMPLATE
= 'https://www.facebook.com/video/tahoe/async/%s/?chain=true&isvideo=true&payloadtype=primary'
68 'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf',
69 'md5': '6a40d33c0eccbb1af76cf0485a052659',
71 'id': '637842556329505',
73 'title': 're:Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam',
74 'uploader': 'Tennis on Facebook',
75 'upload_date': '20140908',
76 'timestamp': 1410199200,
78 'skip': 'Requires logging in',
81 'url': 'https://www.facebook.com/video.php?v=274175099429670',
83 'id': '274175099429670',
85 'title': 're:^Asif Nawab Butt posted a video',
86 'uploader': 'Asif Nawab Butt',
87 'upload_date': '20140506',
88 'timestamp': 1399398998,
89 'thumbnail': r
're:^https?://.*',
91 'expected_warnings': [
95 'note': 'Video with DASH manifest',
96 'url': 'https://www.facebook.com/video.php?v=957955867617029',
97 'md5': 'b2c28d528273b323abe5c6ab59f0f030',
99 'id': '957955867617029',
101 'title': 'When you post epic content on instagram.com/433 8 million followers, this is ...',
102 'uploader': 'Demy de Zeeuw',
103 'upload_date': '20160110',
104 'timestamp': 1452431627,
106 'skip': 'Requires logging in',
108 'url': 'https://www.facebook.com/maxlayn/posts/10153807558977570',
109 'md5': '037b1fa7f3c2d02b7a0d7bc16031ecc6',
111 'id': '544765982287235',
113 'title': '"What are you doing running in the snow?"',
114 'uploader': 'FailArmy',
116 'skip': 'Video gone',
118 'url': 'https://m.facebook.com/story.php?story_fbid=1035862816472149&id=116132035111903',
119 'md5': '1deb90b6ac27f7efcf6d747c8a27f5e3',
121 'id': '1035862816472149',
123 'title': 'What the Flock Is Going On In New Zealand Credit: ViralHog',
124 'uploader': 'S. Saint',
126 'skip': 'Video gone',
128 'note': 'swf params escaped',
129 'url': 'https://www.facebook.com/barackobama/posts/10153664894881749',
130 'md5': '97ba073838964d12c70566e0085c2b91',
132 'id': '10153664894881749',
134 'title': 'Average time to confirm recent Supreme Court nominees: 67 days Longest it\'s t...',
135 'thumbnail': r
're:^https?://.*',
136 'timestamp': 1456259628,
137 'upload_date': '20160223',
138 'uploader': 'Barack Obama',
141 # have 1080P, but only up to 720p in swf params
142 # data.video.story.attachments[].media
143 'url': 'https://www.facebook.com/cnn/videos/10155529876156509/',
144 'md5': '9571fae53d4165bbbadb17a94651dcdc',
146 'id': '10155529876156509',
148 'title': 'She survived the holocaust — and years later, she’s getting her citizenship s...',
149 'timestamp': 1477818095,
150 'upload_date': '20161030',
152 'thumbnail': r
're:^https?://.*',
156 # bigPipe.onPageletArrive ... onPageletArrive pagelet_group_mall
157 # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media
158 'url': 'https://www.facebook.com/yaroslav.korpan/videos/1417995061575415/',
160 'id': '1417995061575415',
162 'title': 'md5:1db063d6a8c13faa8da727817339c857',
163 'timestamp': 1486648217,
164 'upload_date': '20170209',
165 'uploader': 'Yaroslav Korpan',
168 'skip_download': True,
171 'url': 'https://www.facebook.com/LaGuiaDelVaron/posts/1072691702860471',
173 'id': '1072691702860471',
175 'title': 'md5:ae2d22a93fbb12dad20dc393a869739d',
176 'timestamp': 1477305000,
177 'upload_date': '20161024',
178 'uploader': 'La Guía Del Varón',
179 'thumbnail': r
're:^https?://.*',
182 'skip_download': True,
185 # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media
186 'url': 'https://www.facebook.com/groups/1024490957622648/permalink/1396382447100162/',
188 'id': '1396382447100162',
190 'title': 'md5:19a428bbde91364e3de815383b54a235',
191 'timestamp': 1486035494,
192 'upload_date': '20170202',
193 'uploader': 'Elisabeth Ahtn',
196 'skip_download': True,
199 'url': 'https://www.facebook.com/video.php?v=10204634152394104',
200 'only_matching': True,
202 'url': 'https://www.facebook.com/amogood/videos/1618742068337349/?fref=nf',
203 'only_matching': True,
205 # data.mediaset.currMedia.edges
206 'url': 'https://www.facebook.com/ChristyClarkForBC/videos/vb.22819070941/10153870694020942/?type=2&theater',
207 'only_matching': True,
209 # data.video.story.attachments[].media
210 'url': 'facebook:544765982287235',
211 'only_matching': True,
213 # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media
214 'url': 'https://www.facebook.com/groups/164828000315060/permalink/764967300301124/',
215 'only_matching': True,
217 # data.video.creation_story.attachments[].media
218 'url': 'https://zh-hk.facebook.com/peoplespower/videos/1135894589806027/',
219 'only_matching': True,
222 'url': 'https://www.facebookcorewwwi.onion/video.php?v=274175099429670',
223 'only_matching': True,
226 'url': 'https://www.facebook.com/onlycleverentertainment/videos/1947995502095005/',
227 'only_matching': True,
230 'url': 'https://www.facebook.com/WatchESLOne/videos/359649331226507/',
232 'id': '359649331226507',
234 'title': '#ESLOne VoD - Birmingham Finals Day#1 Fnatic vs. @Evil Geniuses',
235 'uploader': 'ESL One Dota 2',
238 'skip_download': True,
241 # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.all_subattachments.nodes[].media
242 'url': 'https://www.facebook.com/100033620354545/videos/106560053808006/',
244 'id': '106560053808006',
248 # data.video.story.attachments[].media
249 'url': 'https://www.facebook.com/watch/?v=647537299265662',
250 'only_matching': True,
252 # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.all_subattachments.nodes[].media
253 'url': 'https://www.facebook.com/PankajShahLondon/posts/10157667649866271',
255 'id': '10157667649866271',
259 # data.nodes[].comet_sections.content.story.attachments[].style_type_renderer.attachment.media
260 'url': 'https://m.facebook.com/Alliance.Police.Department/posts/4048563708499330',
262 'id': '117576630041613',
264 # TODO: title can be extracted from video page
265 'title': 'Facebook video #117576630041613',
266 'uploader_id': '189393014416438',
267 'upload_date': '20201123',
268 'timestamp': 1606162592,
270 'skip': 'Requires logging in',
272 # node.comet_sections.content.story.attached_story.attachments.style_type_renderer.attachment.media
273 'url': 'https://www.facebook.com/groups/ateistiskselskab/permalink/10154930137678856/',
275 'id': '211567722618337',
277 'title': 'Facebook video #211567722618337',
278 'uploader_id': '127875227654254',
279 'upload_date': '20161122',
280 'timestamp': 1479793574,
283 # data.video.creation_story.attachments[].media
284 'url': 'https://www.facebook.com/watch/live/?v=1823658634322275',
285 'only_matching': True,
287 'url': 'https://www.facebook.com/watchparty/211641140192478',
289 'id': '211641140192478',
292 'skip': 'Requires logging in',
294 _SUPPORTED_PAGLETS_REGEX
= r
'(?:pagelet_group_mall|permalink_video_pagelet|hyperfeed_story_id_[0-9a-f]+)'
296 'graphURI': '/api/graphql/'
300 def _extract_urls(webpage
):
302 for mobj
in re
.finditer(
303 r
'<iframe[^>]+?src=(["\'])(?P
<url
>https?
://www\
.facebook\
.com
/(?
:video
/embed|plugins
/video\
.php
).+?
)\
1',
305 urls.append(mobj.group('url
'))
307 # see https://developers.facebook.com/docs/plugins/embedded-video-player
308 for mobj in re.finditer(r'''(?x)<div[^>]+
309 class=(?P<q1>[\'"])[^\'"]*\bfb-(?:video|post)\b[^\'"]*(?P=q1)[^>]+
310 data-href=(?P<q2>[\'"])(?P<url>(?:https?:)?//(?:www\.)?facebook.com/.+?)(?P=q2)''', webpage):
311 urls.append(mobj.group('url
'))
315 useremail, password = self._get_login_info()
316 if useremail is None:
319 login_page_req = sanitized_Request(self._LOGIN_URL)
320 self._set_cookie('facebook
.com
', 'locale
', 'en_US
')
321 login_page = self._download_webpage(login_page_req, None,
322 note='Downloading login page
',
323 errnote='Unable to download login page
')
324 lsd = self._search_regex(
325 r'<input type="hidden" name
="lsd" value
="([^"]*)"',
327 lgnrnd = self._search_regex(r'name="lgnrnd
" value="([^
"]*?)"', login_page, 'lgnrnd
')
334 'next
': 'http
://facebook
.com
/home
.php
',
335 'default_persistent
': '0',
336 'legacy_return
': '1',
340 request = sanitized_Request(self._LOGIN_URL, urlencode_postdata(login_form))
341 request.add_header('Content
-Type
', 'application
/x
-www
-form
-urlencoded
')
343 login_results = self._download_webpage(request, None,
344 note='Logging
in', errnote='unable to fetch login page
')
345 if re.search(r'<form(.*)name
="login"(.*)</form
>', login_results) is not None:
346 error = self._html_search_regex(
347 r'(?s
)<div
[^
>]+class=(["\']).*?login_error_box.*?\1[^>]*><div[^>]*>.*?</div><div[^>]*>(?P<error>.+?)</div>',
348 login_results, 'login error', default=None, group='error')
350 raise ExtractorError('Unable to login: %s' % error, expected=True)
351 self._downloader.report_warning('unable to log in: bad username/password, or exceeded login rate limit (~3/min). Check credentials or wait.')
354 fb_dtsg = self._search_regex(
355 r'name="fb_dtsg
" value="(.+?
)"', login_results, 'fb_dtsg', default=None)
356 h = self._search_regex(
357 r'name="h
"\s+(?:\w+="[^
"]+"\s
+)*?value
="([^"]+)"', login_results, 'h', default=None)
359 if not fb_dtsg or not h:
365 'name_action_selected': 'dont_save',
367 check_req = sanitized_Request(self._CHECKPOINT_URL, urlencode_postdata(check_form))
368 check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
369 check_response = self._download_webpage(check_req, None,
370 note='Confirming login')
371 if re.search(r'id="checkpointSubmitButton
"', check_response) is not None:
372 self._downloader.report_warning('Unable to confirm login, you have to login in your browser and authorize the login.')
373 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
374 self._downloader.report_warning('unable to log in: %s' % error_to_compat_str(err))
377 def _real_initialize(self):
380 def _extract_from_url(self, url, video_id):
381 webpage = self._download_webpage(
382 url.replace('://m.facebook.com/', '://www.facebook.com/'), video_id)
386 def extract_video_data(instances):
388 for item in instances:
389 if try_get(item, lambda x: x[1][0]) == 'VideoConfig':
390 video_item = item[2][0]
391 if video_item.get('video_id'):
392 video_data.append(video_item['videoData'])
395 server_js_data = self._parse_json(self._search_regex(
396 [r'handleServerJS\(({.+})(?:\);|,")', r'\bs\
.handle\
(({.+?}
)\
);'],
397 webpage, 'server js data
', default='{}'), video_id, fatal=False)
400 video_data = extract_video_data(server_js_data.get('instances
', []))
402 def extract_from_jsmods_instances(js_data):
404 return extract_video_data(try_get(
405 js_data, lambda x: x['jsmods
']['instances
'], list) or [])
407 def extract_dash_manifest(video, formats):
408 dash_manifest = video.get('dash_manifest
')
410 formats.extend(self._parse_mpd_formats(
411 compat_etree_fromstring(compat_urllib_parse_unquote_plus(dash_manifest))))
413 def process_formats(formats):
414 # Downloads with browser's User
-Agent are rate limited
. Working around
415 # with non-browser User-Agent.
417 f
.setdefault('http_headers', {})['User-Agent'] = 'facebookexternalhit/1.1'
419 self
._sort
_formats
(formats
)
421 def extract_relay_data(_filter
):
422 return self
._parse
_json
(self
._search
_regex
(
423 r
'handleWithCustomApplyEach\([^,]+,\s*({.*?%s.*?})\);' % _filter
,
424 webpage
, 'replay data', default
='{}'), video_id, fatal=False) or {}
426 def extract_relay_prefetched_data(_filter):
427 replay_data = extract_relay_data(_filter)
428 for require in (replay_data.get('require
') or []):
429 if require[0] == 'RelayPrefetchedStreamCache
':
430 return try_get(require, lambda x: x[3][1]['__bbox
']['result
']['data
'], dict) or {}
433 server_js_data = self._parse_json(self._search_regex([
434 r'bigPipe\
.onPageletArrive\
(({.+?}
)\
)\s
*;\s
*}\s
*\
)\s
*,\s
*["\']onPageletArrive\s+' + self._SUPPORTED_PAGLETS_REGEX,
435 r'bigPipe\.onPageletArrive\(({.*?id\s*:\s*"%s".*?})\);' % self._SUPPORTED_PAGLETS_REGEX
436 ], webpage, 'js data', default='{}'), video_id, js_to_json, False)
437 video_data = extract_from_jsmods_instances(server_js_data)
440 data = extract_relay_prefetched_data(
441 r'"(?
:dash_manifest|
playable_url(?
:_quality_hd
)?
)"\s*:\s*"[^
"]+"')
445 def parse_graphql_video(video):
447 q = qualities(['sd
', 'hd
'])
448 for (suffix, format_id) in [('', 'sd
'), ('_quality_hd
', 'hd
')]:
449 playable_url = video.get('playable_url
' + suffix)
453 'format_id
': format_id,
454 'quality
': q(format_id),
457 extract_dash_manifest(video, formats)
458 process_formats(formats)
459 v_id = video.get('videoId
') or video.get('id') or video_id
463 'thumbnail
': try_get(video, lambda x: x['thumbnailImage
']['uri
']),
464 'uploader_id
': try_get(video, lambda x: x['owner
']['id']),
465 'timestamp
': int_or_none(video.get('publish_time
')),
466 'duration
': float_or_none(video.get('playable_duration_in_ms
'), 1000),
468 description = try_get(video, lambda x: x['savable_description
']['text
'])
469 title = video.get('name
')
473 'description
': description,
476 info['title
'] = description or 'Facebook video
#%s' % v_id
479 def parse_attachment(attachment
, key
='media'):
480 media
= attachment
.get(key
) or {}
481 if media
.get('__typename') == 'Video':
482 return parse_graphql_video(media
)
484 nodes
= data
.get('nodes') or []
485 node
= data
.get('node') or {}
486 if not nodes
and node
:
489 story
= try_get(node
, lambda x
: x
['comet_sections']['content']['story'], dict) or {}
490 attachments
= try_get(story
, [
491 lambda x
: x
['attached_story']['attachments'],
492 lambda x
: x
['attachments']
494 for attachment
in attachments
:
495 attachment
= try_get(attachment
, lambda x
: x
['style_type_renderer']['attachment'], dict)
496 ns
= try_get(attachment
, lambda x
: x
['all_subattachments']['nodes'], list) or []
499 parse_attachment(attachment
)
501 edges
= try_get(data
, lambda x
: x
['mediaset']['currMedia']['edges'], list) or []
503 parse_attachment(edge
, key
='node')
505 video
= data
.get('video') or {}
507 attachments
= try_get(video
, [
508 lambda x
: x
['story']['attachments'],
509 lambda x
: x
['creation_story']['attachments']
511 for attachment
in attachments
:
512 parse_attachment(attachment
)
514 parse_graphql_video(video
)
516 return self
.playlist_result(entries
, video_id
)
519 m_msg
= re
.search(r
'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage
)
520 if m_msg
is not None:
521 raise ExtractorError(
522 'The video is not available, Facebook said: "%s"' % m_msg
.group(1),
524 elif '>You must log in to continue' in webpage
:
525 self
.raise_login_required()
527 if not video_data
and '/watchparty/' in url
:
529 'doc_id': 3731964053542869,
530 'variables': json
.dumps({
531 'livingRoomID': video_id
,
535 prefetched_data
= extract_relay_prefetched_data(r
'"login_data"\s*:\s*{')
537 lsd
= try_get(prefetched_data
, lambda x
: x
['login_data']['lsd'], dict)
539 post_data
[lsd
['name']] = lsd
['value']
541 relay_data
= extract_relay_data(r
'\[\s*"RelayAPIConfigDefaults"\s*,')
542 for define
in (relay_data
.get('define') or []):
543 if define
[0] == 'RelayAPIConfigDefaults':
544 self
._api
_config
= define
[2]
546 living_room
= self
._download
_json
(
547 urljoin(url
, self
._api
_config
['graphURI']), video_id
,
548 data
=urlencode_postdata(post_data
))['data']['living_room']
551 for edge
in (try_get(living_room
, lambda x
: x
['recap']['watched_content']['edges']) or []):
552 video
= try_get(edge
, lambda x
: x
['node']['video']) or {}
553 v_id
= video
.get('id')
556 v_id
= compat_str(v_id
)
557 entries
.append(self
.url_result(
558 self
._VIDEO
_PAGE
_TEMPLATE
% v_id
,
559 self
.ie_key(), v_id
, video
.get('name')))
561 return self
.playlist_result(entries
, video_id
)
564 # Video info not in first request, do a secondary request using
565 # tahoe player specific URL
566 tahoe_data
= self
._download
_webpage
(
567 self
._VIDEO
_PAGE
_TAHOE
_TEMPLATE
% video_id
, video_id
,
568 data
=urlencode_postdata({
570 '__pc': self
._search
_regex
(
571 r
'pkg_cohort["\']\s
*:\s
*["\'](.+?)["\']', webpage,
572 'pkg cohort
', default='PHASED
:DEFAULT
'),
573 '__rev
': self._search_regex(
574 r'client_revision
["\']\s*:\s*(\d+),', webpage,
575 'client revision', default='3944515'),
576 'fb_dtsg': self._search_regex(
577 r'"DTSGInitialData
"\s*,\s*\[\]\s*,\s*{\s*"token
"\s*:\s*"([^
"]+)"',
578 webpage, 'dtsg token
', default=''),
581 'Content
-Type
': 'application
/x
-www
-form
-urlencoded
',
583 tahoe_js_data = self._parse_json(
585 r'for\s
+\
(\s
*;\s
*;\s
*\
)\s
*;(.+)', tahoe_data,
586 'tahoe js data
', default='{}'),
587 video_id, fatal=False)
588 video_data = extract_from_jsmods_instances(tahoe_js_data)
591 raise ExtractorError('Cannot parse data
')
593 if len(video_data) > 1:
596 video_url = v[0].get('video_url
')
599 entries.append(self.url_result(urljoin(
600 url, video_url), self.ie_key(), v[0].get('video_id
')))
601 return self.playlist_result(entries, video_id)
602 video_data = video_data[0]
607 format_id = f['stream_type
']
608 if f and isinstance(f, dict):
610 if not f or not isinstance(f, list):
612 for quality in ('sd
', 'hd
'):
613 for src_type in ('src
', 'src_no_ratelimit
'):
614 src = f[0].get('%s_%s' % (quality, src_type))
616 preference = -10 if format_id == 'progressive
' else 0
620 'format_id
': '%s_%s_%s' % (format_id, quality, src_type),
622 'preference
': preference,
624 extract_dash_manifest(f[0], formats)
625 subtitles_src = f[0].get('subtitles_src
')
627 subtitles.setdefault('en
', []).append({'url': subtitles_src})
629 raise ExtractorError('Cannot find video formats
')
631 process_formats(formats)
633 video_title = self._html_search_regex(
634 r'<h2\s
+[^
>]*class="uiHeaderTitle"[^
>]*>([^
<]*)</h2
>', webpage,
635 'title
', default=None)
637 video_title = self._html_search_regex(
638 r'(?s
)<span
class="fbPhotosPhotoCaption".*?
id="fbPhotoPageCaption"><span
class="hasCaption">(.*?
)</span
>',
639 webpage, 'alternative title
', default=None)
641 video_title = self._html_search_meta(
642 'description
', webpage, 'title
', default=None)
644 video_title = limit_length(video_title, 80)
646 video_title = 'Facebook video
#%s' % video_id
647 uploader
= clean_html(get_element_by_id(
648 'fbPhotoPageAuthorName', webpage
)) or self
._search
_regex
(
649 r
'ownerName\s*:\s*"([^"]+)"', webpage
, 'uploader',
650 default
=None) or self
._og
_search
_title
(webpage
, fatal
=False)
651 timestamp
= int_or_none(self
._search
_regex
(
652 r
'<abbr[^>]+data-utime=["\'](\d
+)', webpage,
653 'timestamp
', default=None))
654 thumbnail = self._html_search_meta(['og
:image
', 'twitter
:image
'], webpage)
656 view_count = parse_count(self._search_regex(
657 r'\bviewCount\s
*:\s
*["\']([\d,.]+)', webpage, 'view count',
662 'title': video_title,
664 'uploader': uploader,
665 'timestamp': timestamp,
666 'thumbnail': thumbnail,
667 'view_count': view_count,
668 'subtitles': subtitles,
673 def _real_extract(self, url):
674 video_id = self._match_id(url)
676 real_url = self._VIDEO_PAGE_TEMPLATE % video_id if url.startswith('facebook:') else url
677 return self._extract_from_url(real_url, video_id)
680 class FacebookPluginsVideoIE(InfoExtractor):
681 _VALID_URL = r'https?://(?:[\w-]+\.)?facebook\.com/plugins/video\.php\?.*?\bhref=(?P<id>https.+)'
684 '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',
685 'md5': '5954e92cdfe51fe5782ae9bda7058a07',
687 'id': '10154383743583686',
689 'title': 'What to do during the haze?',
690 'uploader': 'Gov.sg',
691 'upload_date': '20160826',
692 'timestamp': 1472184808,
694 'add_ie': [FacebookIE.ie_key()],
696 'url': 'https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fvideo.php%3Fv%3D10204634152394104',
697 'only_matching': True,
699 'url': 'https://www.facebook.com/plugins/video.php?href=https://www.facebook.com/gov.sg/videos/10154383743583686/&show_text=0&width=560',
700 'only_matching': True,
703 def _real_extract(self, url):
704 return self.url_result(
705 compat_urllib_parse_unquote(self._match_id(url)),