]>
Commit | Line | Data |
---|---|---|
29f7c58a | 1 | import json |
9eae41dd | 2 | import re |
ac668111 | 3 | import urllib.parse |
9eae41dd PH |
4 | |
5 | from .common import InfoExtractor | |
8c25f81b | 6 | from ..compat import ( |
0803753f | 7 | compat_etree_fromstring, |
29f7c58a | 8 | compat_str, |
d7011316 | 9 | compat_urllib_parse_unquote, |
8c25f81b PH |
10 | ) |
11 | from ..utils import ( | |
ac668111 | 12 | ExtractorError, |
b83ef507 | 13 | clean_html, |
b8e976a4 | 14 | determine_ext, |
9b9c5355 | 15 | error_to_compat_str, |
29f7c58a | 16 | float_or_none, |
b83ef507 | 17 | get_element_by_id, |
ff91cf74 | 18 | get_first, |
196c6ba0 | 19 | int_or_none, |
b83ef507 | 20 | js_to_json, |
a181cd0c | 21 | merge_dicts, |
3158150c | 22 | network_exceptions, |
11330f51 | 23 | parse_count, |
5f549d49 | 24 | parse_qs, |
29f7c58a | 25 | qualities, |
5c2266df | 26 | sanitized_Request, |
80fa6e53 | 27 | traverse_obj, |
b83ef507 | 28 | try_get, |
5f549d49 | 29 | url_or_none, |
2fc9f2b4 | 30 | urlencode_postdata, |
29f7c58a | 31 | urljoin, |
b2db8102 | 32 | variadic, |
9eae41dd PH |
33 | ) |
34 | ||
35 | ||
36 | class FacebookIE(InfoExtractor): | |
9834872b | 37 | _VALID_URL = r'''(?x) |
2e6e742c S |
38 | (?: |
39 | https?:// | |
61bfacb2 | 40 | (?:[\w-]+\.)?(?:facebook\.com|facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd\.onion)/ |
2e6e742c S |
41 | (?:[^#]*?\#!/)? |
42 | (?: | |
43 | (?: | |
44 | video/video\.php| | |
45 | photo\.php| | |
46 | video\.php| | |
efbeddea | 47 | video/embed| |
29f7c58a | 48 | story\.php| |
49 | watch(?:/live)?/? | |
efbeddea | 50 | )\?(?:.*?)(?:v|video_id|story_fbid)=| |
9c7b509b | 51 | [^/]+/videos/(?:[^/]+/)?| |
e738e433 | 52 | [^/]+/posts/| |
29f7c58a | 53 | groups/[^/]+/permalink/| |
54 | watchparty/ | |
2e6e742c S |
55 | )| |
56 | facebook: | |
57 | ) | |
58 | (?P<id>[0-9]+) | |
59 | ''' | |
bfd973ec | 60 | _EMBED_REGEX = [ |
61 | r'<iframe[^>]+?src=(["\'])(?P<url>https?://www\.facebook\.com/(?:video/embed|plugins/video\.php).+?)\1', | |
62 | # Facebook API embed https://developers.facebook.com/docs/plugins/embedded-video-player | |
63 | r'''(?x)<div[^>]+ | |
64 | class=(?P<q1>[\'"])[^\'"]*\bfb-(?:video|post)\b[^\'"]*(?P=q1)[^>]+ | |
65 | data-href=(?P<q2>[\'"])(?P<url>(?:https?:)?//(?:www\.)?facebook.com/.+?)(?P=q2)''', | |
66 | ] | |
67874aef JMF |
67 | _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1' |
68 | _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1' | |
9eae41dd | 69 | _NETRC_MACHINE = 'facebook' |
cb3bb2cf | 70 | IE_NAME = 'facebook' |
0803753f | 71 | |
9c7b509b | 72 | _VIDEO_PAGE_TEMPLATE = 'https://www.facebook.com/video/video.php?v=%s' |
0df514f0 | 73 | _VIDEO_PAGE_TAHOE_TEMPLATE = 'https://www.facebook.com/video/tahoe/async/%s/?chain=true&isvideo=true&payloadtype=primary' |
9c7b509b | 74 | |
60ac04e5 | 75 | _TESTS = [{ |
50317dbb PH |
76 | 'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf', |
77 | 'md5': '6a40d33c0eccbb1af76cf0485a052659', | |
cb3bb2cf | 78 | 'info_dict': { |
50317dbb | 79 | 'id': '637842556329505', |
cb3bb2cf | 80 | 'ext': 'mp4', |
5e4f0619 | 81 | 'title': 're:Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam', |
67b8a28a | 82 | 'uploader': 'Tennis on Facebook', |
196c6ba0 S |
83 | 'upload_date': '20140908', |
84 | 'timestamp': 1410199200, | |
01c742ec YCH |
85 | }, |
86 | 'skip': 'Requires logging in', | |
a020a0dc | 87 | }, { |
29f7c58a | 88 | # data.video |
a020a0dc PH |
89 | 'url': 'https://www.facebook.com/video.php?v=274175099429670', |
90 | 'info_dict': { | |
91 | 'id': '274175099429670', | |
92 | 'ext': 'mp4', | |
a181cd0c | 93 | 'title': 'Asif Nawab Butt', |
94 | 'description': 'Asif Nawab Butt', | |
67b8a28a | 95 | 'uploader': 'Asif Nawab Butt', |
196c6ba0 S |
96 | 'upload_date': '20140506', |
97 | 'timestamp': 1399398998, | |
01c742ec | 98 | 'thumbnail': r're:^https?://.*', |
db3ca364 YCH |
99 | }, |
100 | 'expected_warnings': [ | |
101 | 'title' | |
102 | ] | |
0803753f YCH |
103 | }, { |
104 | 'note': 'Video with DASH manifest', | |
105 | 'url': 'https://www.facebook.com/video.php?v=957955867617029', | |
196c6ba0 | 106 | 'md5': 'b2c28d528273b323abe5c6ab59f0f030', |
0803753f YCH |
107 | 'info_dict': { |
108 | 'id': '957955867617029', | |
109 | 'ext': 'mp4', | |
110 | 'title': 'When you post epic content on instagram.com/433 8 million followers, this is ...', | |
111 | 'uploader': 'Demy de Zeeuw', | |
196c6ba0 S |
112 | 'upload_date': '20160110', |
113 | 'timestamp': 1452431627, | |
0803753f | 114 | }, |
01c742ec | 115 | 'skip': 'Requires logging in', |
9c7b509b YCH |
116 | }, { |
117 | 'url': 'https://www.facebook.com/maxlayn/posts/10153807558977570', | |
118 | 'md5': '037b1fa7f3c2d02b7a0d7bc16031ecc6', | |
119 | 'info_dict': { | |
120 | 'id': '544765982287235', | |
121 | 'ext': 'mp4', | |
122 | 'title': '"What are you doing running in the snow?"', | |
123 | 'uploader': 'FailArmy', | |
d9ee2e5c YCH |
124 | }, |
125 | 'skip': 'Video gone', | |
98801241 YCH |
126 | }, { |
127 | 'url': 'https://m.facebook.com/story.php?story_fbid=1035862816472149&id=116132035111903', | |
128 | 'md5': '1deb90b6ac27f7efcf6d747c8a27f5e3', | |
129 | 'info_dict': { | |
130 | 'id': '1035862816472149', | |
131 | 'ext': 'mp4', | |
132 | 'title': 'What the Flock Is Going On In New Zealand Credit: ViralHog', | |
133 | 'uploader': 'S. Saint', | |
134 | }, | |
d9ee2e5c | 135 | 'skip': 'Video gone', |
5080cbf9 YCH |
136 | }, { |
137 | 'note': 'swf params escaped', | |
138 | 'url': 'https://www.facebook.com/barackobama/posts/10153664894881749', | |
139 | 'md5': '97ba073838964d12c70566e0085c2b91', | |
140 | 'info_dict': { | |
141 | 'id': '10153664894881749', | |
142 | 'ext': 'mp4', | |
01c742ec YCH |
143 | 'title': 'Average time to confirm recent Supreme Court nominees: 67 days Longest it\'s t...', |
144 | 'thumbnail': r're:^https?://.*', | |
145 | 'timestamp': 1456259628, | |
146 | 'upload_date': '20160223', | |
147 | 'uploader': 'Barack Obama', | |
5080cbf9 | 148 | }, |
a181cd0c | 149 | 'skip': 'Gif on giphy.com gone', |
d9ee2e5c YCH |
150 | }, { |
151 | # have 1080P, but only up to 720p in swf params | |
29f7c58a | 152 | # data.video.story.attachments[].media |
d9ee2e5c | 153 | 'url': 'https://www.facebook.com/cnn/videos/10155529876156509/', |
a181cd0c | 154 | 'md5': '3f3798adb2b73423263e59376f1f5eb7', |
d9ee2e5c YCH |
155 | 'info_dict': { |
156 | 'id': '10155529876156509', | |
157 | 'ext': 'mp4', | |
a181cd0c | 158 | 'title': 'Holocaust survivor becomes US citizen', |
159 | 'description': 'She survived the holocaust — and years later, she’s getting her citizenship so she can vote for Hillary Clinton http://cnn.it/2eERh5f', | |
d9ee2e5c YCH |
160 | 'timestamp': 1477818095, |
161 | 'upload_date': '20161030', | |
162 | 'uploader': 'CNN', | |
01c742ec | 163 | 'thumbnail': r're:^https?://.*', |
11330f51 | 164 | 'view_count': int, |
d9ee2e5c | 165 | }, |
78ef214d S |
166 | }, { |
167 | # bigPipe.onPageletArrive ... onPageletArrive pagelet_group_mall | |
29f7c58a | 168 | # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media |
78ef214d S |
169 | 'url': 'https://www.facebook.com/yaroslav.korpan/videos/1417995061575415/', |
170 | 'info_dict': { | |
171 | 'id': '1417995061575415', | |
172 | 'ext': 'mp4', | |
b2db8102 | 173 | 'title': 'Ukrainian Scientists Worldwide | Довгоочікуване відео', |
a181cd0c | 174 | 'description': 'Довгоочікуване відео', |
175 | 'timestamp': 1486648771, | |
78ef214d S |
176 | 'upload_date': '20170209', |
177 | 'uploader': 'Yaroslav Korpan', | |
a181cd0c | 178 | 'uploader_id': '100000948048708', |
78ef214d S |
179 | }, |
180 | 'params': { | |
181 | 'skip_download': True, | |
182 | }, | |
71cdd756 | 183 | }, { |
a181cd0c | 184 | # FIXME |
71cdd756 S |
185 | 'url': 'https://www.facebook.com/LaGuiaDelVaron/posts/1072691702860471', |
186 | 'info_dict': { | |
187 | 'id': '1072691702860471', | |
188 | 'ext': 'mp4', | |
189 | 'title': 'md5:ae2d22a93fbb12dad20dc393a869739d', | |
190 | 'timestamp': 1477305000, | |
191 | 'upload_date': '20161024', | |
192 | 'uploader': 'La Guía Del Varón', | |
01c742ec | 193 | 'thumbnail': r're:^https?://.*', |
71cdd756 S |
194 | }, |
195 | 'params': { | |
196 | 'skip_download': True, | |
197 | }, | |
198 | }, { | |
29f7c58a | 199 | # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media |
71cdd756 S |
200 | 'url': 'https://www.facebook.com/groups/1024490957622648/permalink/1396382447100162/', |
201 | 'info_dict': { | |
a181cd0c | 202 | 'id': '202882990186699', |
71cdd756 | 203 | 'ext': 'mp4', |
b2db8102 | 204 | 'title': 'birb (O v O") | Hello? Yes your uber ride is here', |
205 | 'description': 'Hello? Yes your uber ride is here * Jukin Media Verified * Find this video and others like it by visiting...', | |
a181cd0c | 206 | 'timestamp': 1486035513, |
71cdd756 S |
207 | 'upload_date': '20170202', |
208 | 'uploader': 'Elisabeth Ahtn', | |
a181cd0c | 209 | 'uploader_id': '100013949973717', |
71cdd756 S |
210 | }, |
211 | 'params': { | |
212 | 'skip_download': True, | |
213 | }, | |
60ac04e5 PH |
214 | }, { |
215 | 'url': 'https://www.facebook.com/video.php?v=10204634152394104', | |
216 | 'only_matching': True, | |
c6256697 YCH |
217 | }, { |
218 | 'url': 'https://www.facebook.com/amogood/videos/1618742068337349/?fref=nf', | |
219 | 'only_matching': True, | |
53faa3ca | 220 | }, { |
29f7c58a | 221 | # data.mediaset.currMedia.edges |
53faa3ca YCH |
222 | 'url': 'https://www.facebook.com/ChristyClarkForBC/videos/vb.22819070941/10153870694020942/?type=2&theater', |
223 | 'only_matching': True, | |
2e6e742c | 224 | }, { |
29f7c58a | 225 | # data.video.story.attachments[].media |
2e6e742c S |
226 | 'url': 'facebook:544765982287235', |
227 | 'only_matching': True, | |
e738e433 | 228 | }, { |
29f7c58a | 229 | # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media |
e738e433 YCH |
230 | 'url': 'https://www.facebook.com/groups/164828000315060/permalink/764967300301124/', |
231 | 'only_matching': True, | |
b4a131e1 | 232 | }, { |
29f7c58a | 233 | # data.video.creation_story.attachments[].media |
b4a131e1 S |
234 | 'url': 'https://zh-hk.facebook.com/peoplespower/videos/1135894589806027/', |
235 | 'only_matching': True, | |
30918999 | 236 | }, { |
29f7c58a | 237 | # data.video |
61bfacb2 | 238 | 'url': 'https://www.facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion/video.php?v=274175099429670', |
30918999 | 239 | 'only_matching': True, |
66bf351f S |
240 | }, { |
241 | # no title | |
242 | 'url': 'https://www.facebook.com/onlycleverentertainment/videos/1947995502095005/', | |
243 | 'only_matching': True, | |
9d082e7c | 244 | }, { |
29f7c58a | 245 | # data.video |
9d082e7c NR |
246 | 'url': 'https://www.facebook.com/WatchESLOne/videos/359649331226507/', |
247 | 'info_dict': { | |
248 | 'id': '359649331226507', | |
249 | 'ext': 'mp4', | |
a181cd0c | 250 | 'title': 'Fnatic vs. EG - Group A - Opening Match - ESL One Birmingham Day 1', |
251 | 'description': '#ESLOne VoD - Birmingham Finals Day#1 Fnatic vs. @Evil Geniuses', | |
252 | 'timestamp': 1527084179, | |
253 | 'upload_date': '20180523', | |
9d082e7c | 254 | 'uploader': 'ESL One Dota 2', |
a181cd0c | 255 | 'uploader_id': '234218833769558', |
9d082e7c NR |
256 | }, |
257 | 'params': { | |
258 | 'skip_download': True, | |
259 | }, | |
29f7c58a | 260 | }, { |
261 | # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.all_subattachments.nodes[].media | |
262 | 'url': 'https://www.facebook.com/100033620354545/videos/106560053808006/', | |
263 | 'info_dict': { | |
264 | 'id': '106560053808006', | |
265 | }, | |
266 | 'playlist_count': 2, | |
267 | }, { | |
268 | # data.video.story.attachments[].media | |
269 | 'url': 'https://www.facebook.com/watch/?v=647537299265662', | |
270 | 'only_matching': True, | |
271 | }, { | |
a181cd0c | 272 | # FIXME: https://github.com/yt-dlp/yt-dlp/issues/542 |
29f7c58a | 273 | # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.all_subattachments.nodes[].media |
274 | 'url': 'https://www.facebook.com/PankajShahLondon/posts/10157667649866271', | |
275 | 'info_dict': { | |
276 | 'id': '10157667649866271', | |
277 | }, | |
278 | 'playlist_count': 3, | |
279 | }, { | |
280 | # data.nodes[].comet_sections.content.story.attachments[].style_type_renderer.attachment.media | |
281 | 'url': 'https://m.facebook.com/Alliance.Police.Department/posts/4048563708499330', | |
282 | 'info_dict': { | |
283 | 'id': '117576630041613', | |
284 | 'ext': 'mp4', | |
285 | # TODO: title can be extracted from video page | |
286 | 'title': 'Facebook video #117576630041613', | |
287 | 'uploader_id': '189393014416438', | |
288 | 'upload_date': '20201123', | |
289 | 'timestamp': 1606162592, | |
290 | }, | |
291 | 'skip': 'Requires logging in', | |
292 | }, { | |
293 | # node.comet_sections.content.story.attached_story.attachments.style_type_renderer.attachment.media | |
294 | 'url': 'https://www.facebook.com/groups/ateistiskselskab/permalink/10154930137678856/', | |
295 | 'info_dict': { | |
296 | 'id': '211567722618337', | |
297 | 'ext': 'mp4', | |
298 | 'title': 'Facebook video #211567722618337', | |
299 | 'uploader_id': '127875227654254', | |
300 | 'upload_date': '20161122', | |
301 | 'timestamp': 1479793574, | |
302 | }, | |
a181cd0c | 303 | 'skip': 'No video', |
29f7c58a | 304 | }, { |
305 | # data.video.creation_story.attachments[].media | |
306 | 'url': 'https://www.facebook.com/watch/live/?v=1823658634322275', | |
307 | 'only_matching': True, | |
308 | }, { | |
309 | 'url': 'https://www.facebook.com/watchparty/211641140192478', | |
310 | 'info_dict': { | |
311 | 'id': '211641140192478', | |
312 | }, | |
313 | 'playlist_count': 1, | |
314 | 'skip': 'Requires logging in', | |
60ac04e5 | 315 | }] |
29f7c58a | 316 | _SUPPORTED_PAGLETS_REGEX = r'(?:pagelet_group_mall|permalink_video_pagelet|hyperfeed_story_id_[0-9a-f]+)' |
317 | _api_config = { | |
318 | 'graphURI': '/api/graphql/' | |
319 | } | |
9eae41dd | 320 | |
52efa4b3 | 321 | def _perform_login(self, username, password): |
5c2266df | 322 | login_page_req = sanitized_Request(self._LOGIN_URL) |
c1f49e16 | 323 | self._set_cookie('facebook.com', 'locale', 'en_US') |
b74fa8cd | 324 | login_page = self._download_webpage(login_page_req, None, |
9e1a5b84 JW |
325 | note='Downloading login page', |
326 | errnote='Unable to download login page') | |
b37b9450 | 327 | lsd = self._search_regex( |
d0ff8384 | 328 | r'<input type="hidden" name="lsd" value="([^"]*)"', |
b37b9450 | 329 | login_page, 'lsd') |
cb3bb2cf | 330 | lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd') |
67874aef | 331 | |
9eae41dd | 332 | login_form = { |
52efa4b3 | 333 | 'email': username, |
9eae41dd | 334 | 'pass': password, |
67874aef JMF |
335 | 'lsd': lsd, |
336 | 'lgnrnd': lgnrnd, | |
337 | 'next': 'http://facebook.com/home.php', | |
338 | 'default_persistent': '0', | |
339 | 'legacy_return': '1', | |
340 | 'timezone': '-60', | |
341 | 'trynum': '1', | |
b74e86f4 | 342 | } |
5c2266df | 343 | request = sanitized_Request(self._LOGIN_URL, urlencode_postdata(login_form)) |
67874aef | 344 | request.add_header('Content-Type', 'application/x-www-form-urlencoded') |
9eae41dd | 345 | try: |
b74fa8cd | 346 | login_results = self._download_webpage(request, None, |
9e1a5b84 | 347 | note='Logging in', errnote='unable to fetch login page') |
9eae41dd | 348 | if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None: |
9f66931e S |
349 | error = self._html_search_regex( |
350 | r'(?s)<div[^>]+class=(["\']).*?login_error_box.*?\1[^>]*><div[^>]*>.*?</div><div[^>]*>(?P<error>.+?)</div>', | |
351 | login_results, 'login error', default=None, group='error') | |
352 | if error: | |
353 | raise ExtractorError('Unable to login: %s' % error, expected=True) | |
6a39ee13 | 354 | self.report_warning('unable to log in: bad username/password, or exceeded login rate limit (~3/min). Check credentials or wait.') |
9eae41dd | 355 | return |
67874aef | 356 | |
c1f49e16 S |
357 | fb_dtsg = self._search_regex( |
358 | r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg', default=None) | |
359 | h = self._search_regex( | |
360 | r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h', default=None) | |
361 | ||
362 | if not fb_dtsg or not h: | |
363 | return | |
364 | ||
67874aef | 365 | check_form = { |
c1f49e16 S |
366 | 'fb_dtsg': fb_dtsg, |
367 | 'h': h, | |
67874aef | 368 | 'name_action_selected': 'dont_save', |
67874aef | 369 | } |
5c2266df | 370 | check_req = sanitized_Request(self._CHECKPOINT_URL, urlencode_postdata(check_form)) |
67874aef | 371 | check_req.add_header('Content-Type', 'application/x-www-form-urlencoded') |
b74fa8cd | 372 | check_response = self._download_webpage(check_req, None, |
9e1a5b84 | 373 | note='Confirming login') |
67874aef | 374 | if re.search(r'id="checkpointSubmitButton"', check_response) is not None: |
6a39ee13 | 375 | self.report_warning('Unable to confirm login, you have to login in your browser and authorize the login.') |
3158150c | 376 | except network_exceptions as err: |
6a39ee13 | 377 | self.report_warning('unable to log in: %s' % error_to_compat_str(err)) |
9eae41dd PH |
378 | return |
379 | ||
29f7c58a | 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) | |
9eae41dd | 383 | |
a181cd0c | 384 | def extract_metadata(webpage): |
b2db8102 | 385 | post_data = [self._parse_json(j, video_id, fatal=False) for j in re.findall( |
80fa6e53 | 386 | r'handleWithCustomApplyEach\(\s*ScheduledApplyEach\s*,\s*(\{.+?\})\s*\);', webpage)] |
b2db8102 | 387 | post = traverse_obj(post_data, ( |
388 | ..., 'require', ..., ..., ..., '__bbox', 'result', 'data'), expected_type=dict) or [] | |
1e9969f4 | 389 | media = traverse_obj(post, (..., 'attachments', ..., lambda k, v: ( |
390 | k == 'media' and str(v['id']) == video_id and v['__typename'] == 'Video')), expected_type=dict) | |
ff91cf74 | 391 | title = get_first(media, ('title', 'text')) |
392 | description = get_first(media, ('creation_story', 'comet_sections', 'message', 'story', 'message', 'text')) | |
393 | uploader_data = get_first(media, 'owner') or get_first(post, ('node', 'actors', ...)) or {} | |
80fa6e53 | 394 | |
b2db8102 | 395 | page_title = title or self._html_search_regex(( |
80fa6e53 | 396 | r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>(?P<content>[^<]*)</h2>', |
397 | r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(?P<content>.*?)</span>', | |
b2db8102 | 398 | self._meta_regex('og:title'), self._meta_regex('twitter:title'), r'<title>(?P<content>.+?)</title>' |
80fa6e53 | 399 | ), webpage, 'title', default=None, group='content') |
400 | description = description or self._html_search_meta( | |
a181cd0c | 401 | ['description', 'og:description', 'twitter:description'], |
402 | webpage, 'description', default=None) | |
b2db8102 | 403 | uploader = uploader_data.get('name') or ( |
80fa6e53 | 404 | clean_html(get_element_by_id('fbPhotoPageAuthorName', webpage)) |
405 | or self._search_regex( | |
406 | (r'ownerName\s*:\s*"([^"]+)"', *self._og_regexes('title')), webpage, 'uploader', fatal=False)) | |
407 | ||
a181cd0c | 408 | timestamp = int_or_none(self._search_regex( |
409 | r'<abbr[^>]+data-utime=["\'](\d+)', webpage, | |
410 | 'timestamp', default=None)) | |
411 | thumbnail = self._html_search_meta( | |
412 | ['og:image', 'twitter:image'], webpage, 'thumbnail', default=None) | |
413 | # some webpages contain unretrievable thumbnail urls | |
414 | # like https://lookaside.fbsbx.com/lookaside/crawler/media/?media_id=10155168902769113&get_thumbnail=1 | |
415 | # in https://www.facebook.com/yaroslav.korpan/videos/1417995061575415/ | |
416 | if thumbnail and not re.search(r'\.(?:jpg|png)', thumbnail): | |
417 | thumbnail = None | |
418 | view_count = parse_count(self._search_regex( | |
419 | r'\bviewCount\s*:\s*["\']([\d,.]+)', webpage, 'view count', | |
420 | default=None)) | |
421 | info_dict = { | |
a181cd0c | 422 | 'description': description, |
423 | 'uploader': uploader, | |
b2db8102 | 424 | 'uploader_id': uploader_data.get('id'), |
a181cd0c | 425 | 'timestamp': timestamp, |
426 | 'thumbnail': thumbnail, | |
427 | 'view_count': view_count, | |
428 | } | |
b2db8102 | 429 | |
a181cd0c | 430 | info_json_ld = self._search_json_ld(webpage, video_id, default={}) |
b2db8102 | 431 | info_json_ld['title'] = (re.sub(r'\s*\|\s*Facebook$', '', title or info_json_ld.get('title') or page_title or '') |
432 | or (description or '').replace('\n', ' ') or f'Facebook video #{video_id}') | |
a181cd0c | 433 | return merge_dicts(info_json_ld, info_dict) |
434 | ||
c1406299 YCH |
435 | video_data = None |
436 | ||
b83ef507 | 437 | def extract_video_data(instances): |
29f7c58a | 438 | video_data = [] |
b83ef507 | 439 | for item in instances: |
29f7c58a | 440 | if try_get(item, lambda x: x[1][0]) == 'VideoConfig': |
b83ef507 | 441 | video_item = item[2][0] |
71cdd756 | 442 | if video_item.get('video_id'): |
29f7c58a | 443 | video_data.append(video_item['videoData']) |
444 | return video_data | |
b83ef507 | 445 | |
d9ee2e5c | 446 | server_js_data = self._parse_json(self._search_regex( |
29f7c58a | 447 | [r'handleServerJS\(({.+})(?:\);|,")', r'\bs\.handle\(({.+?})\);'], |
448 | webpage, 'server js data', default='{}'), video_id, fatal=False) | |
b83ef507 S |
449 | |
450 | if server_js_data: | |
451 | video_data = extract_video_data(server_js_data.get('instances', [])) | |
452 | ||
9b89daef S |
453 | def extract_from_jsmods_instances(js_data): |
454 | if js_data: | |
455 | return extract_video_data(try_get( | |
456 | js_data, lambda x: x['jsmods']['instances'], list) or []) | |
457 | ||
29f7c58a | 458 | def extract_dash_manifest(video, formats): |
459 | dash_manifest = video.get('dash_manifest') | |
460 | if dash_manifest: | |
461 | formats.extend(self._parse_mpd_formats( | |
ac668111 | 462 | compat_etree_fromstring(urllib.parse.unquote_plus(dash_manifest)))) |
29f7c58a | 463 | |
9f14daf2 | 464 | def process_formats(info): |
29f7c58a | 465 | # Downloads with browser's User-Agent are rate limited. Working around |
466 | # with non-browser User-Agent. | |
9f14daf2 | 467 | for f in info['formats']: |
29f7c58a | 468 | f.setdefault('http_headers', {})['User-Agent'] = 'facebookexternalhit/1.1' |
9f14daf2 | 469 | info['_format_sort_fields'] = ('res', 'quality') |
29f7c58a | 470 | |
471 | def extract_relay_data(_filter): | |
472 | return self._parse_json(self._search_regex( | |
473 | r'handleWithCustomApplyEach\([^,]+,\s*({.*?%s.*?})\);' % _filter, | |
474 | webpage, 'replay data', default='{}'), video_id, fatal=False) or {} | |
475 | ||
476 | def extract_relay_prefetched_data(_filter): | |
477 | replay_data = extract_relay_data(_filter) | |
478 | for require in (replay_data.get('require') or []): | |
479 | if require[0] == 'RelayPrefetchedStreamCache': | |
480 | return try_get(require, lambda x: x[3][1]['__bbox']['result']['data'], dict) or {} | |
481 | ||
b83ef507 | 482 | if not video_data: |
29f7c58a | 483 | server_js_data = self._parse_json(self._search_regex([ |
484 | r'bigPipe\.onPageletArrive\(({.+?})\)\s*;\s*}\s*\)\s*,\s*["\']onPageletArrive\s+' + self._SUPPORTED_PAGLETS_REGEX, | |
485 | r'bigPipe\.onPageletArrive\(({.*?id\s*:\s*"%s".*?})\);' % self._SUPPORTED_PAGLETS_REGEX | |
486 | ], webpage, 'js data', default='{}'), video_id, js_to_json, False) | |
9b89daef | 487 | video_data = extract_from_jsmods_instances(server_js_data) |
c1406299 | 488 | |
9d082e7c | 489 | if not video_data: |
29f7c58a | 490 | data = extract_relay_prefetched_data( |
491 | r'"(?:dash_manifest|playable_url(?:_quality_hd)?)"\s*:\s*"[^"]+"') | |
492 | if data: | |
493 | entries = [] | |
494 | ||
495 | def parse_graphql_video(video): | |
496 | formats = [] | |
497 | q = qualities(['sd', 'hd']) | |
b8e976a4 | 498 | for key, format_id in (('playable_url', 'sd'), ('playable_url_quality_hd', 'hd'), |
499 | ('playable_url_dash', '')): | |
500 | playable_url = video.get(key) | |
29f7c58a | 501 | if not playable_url: |
502 | continue | |
b8e976a4 | 503 | if determine_ext(playable_url) == 'mpd': |
504 | formats.extend(self._extract_mpd_formats(playable_url, video_id)) | |
505 | else: | |
506 | formats.append({ | |
507 | 'format_id': format_id, | |
508 | 'quality': q(format_id), | |
509 | 'url': playable_url, | |
510 | }) | |
29f7c58a | 511 | extract_dash_manifest(video, formats) |
29f7c58a | 512 | v_id = video.get('videoId') or video.get('id') or video_id |
513 | info = { | |
514 | 'id': v_id, | |
515 | 'formats': formats, | |
cda1bc51 A |
516 | 'thumbnail': traverse_obj( |
517 | video, ('thumbnailImage', 'uri'), ('preferred_thumbnail', 'image', 'uri')), | |
29f7c58a | 518 | 'uploader_id': try_get(video, lambda x: x['owner']['id']), |
519 | 'timestamp': int_or_none(video.get('publish_time')), | |
520 | 'duration': float_or_none(video.get('playable_duration_in_ms'), 1000), | |
521 | } | |
9f14daf2 | 522 | process_formats(info) |
29f7c58a | 523 | description = try_get(video, lambda x: x['savable_description']['text']) |
524 | title = video.get('name') | |
525 | if title: | |
526 | info.update({ | |
527 | 'title': title, | |
528 | 'description': description, | |
529 | }) | |
530 | else: | |
531 | info['title'] = description or 'Facebook video #%s' % v_id | |
532 | entries.append(info) | |
533 | ||
534 | def parse_attachment(attachment, key='media'): | |
535 | media = attachment.get(key) or {} | |
536 | if media.get('__typename') == 'Video': | |
537 | return parse_graphql_video(media) | |
538 | ||
b2db8102 | 539 | nodes = variadic(traverse_obj(data, 'nodes', 'node') or []) |
540 | attachments = traverse_obj(nodes, ( | |
541 | ..., 'comet_sections', 'content', 'story', (None, 'attached_story'), 'attachments', | |
542 | ..., ('styles', 'style_type_renderer'), 'attachment'), expected_type=dict) or [] | |
543 | for attachment in attachments: | |
544 | ns = try_get(attachment, lambda x: x['all_subattachments']['nodes'], list) or [] | |
545 | for n in ns: | |
546 | parse_attachment(n) | |
547 | parse_attachment(attachment) | |
29f7c58a | 548 | |
549 | edges = try_get(data, lambda x: x['mediaset']['currMedia']['edges'], list) or [] | |
550 | for edge in edges: | |
551 | parse_attachment(edge, key='node') | |
552 | ||
553 | video = data.get('video') or {} | |
554 | if video: | |
555 | attachments = try_get(video, [ | |
556 | lambda x: x['story']['attachments'], | |
557 | lambda x: x['creation_story']['attachments'] | |
558 | ], list) or [] | |
559 | for attachment in attachments: | |
560 | parse_attachment(attachment) | |
561 | if not entries: | |
562 | parse_graphql_video(video) | |
563 | ||
a181cd0c | 564 | if len(entries) > 1: |
565 | return self.playlist_result(entries, video_id) | |
566 | ||
567 | video_info = entries[0] | |
568 | webpage_info = extract_metadata(webpage) | |
569 | # honor precise duration in video info | |
570 | if video_info.get('duration'): | |
571 | webpage_info['duration'] = video_info['duration'] | |
572 | return merge_dicts(webpage_info, video_info) | |
29f7c58a | 573 | |
574 | if not video_data: | |
9b89daef S |
575 | m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage) |
576 | if m_msg is not None: | |
577 | raise ExtractorError( | |
578 | 'The video is not available, Facebook said: "%s"' % m_msg.group(1), | |
579 | expected=True) | |
46358f64 | 580 | elif any(p in webpage for p in ( |
581 | '>You must log in to continue', | |
582 | 'id="login_form"', | |
583 | 'id="loginbutton"')): | |
9b89daef S |
584 | self.raise_login_required() |
585 | ||
29f7c58a | 586 | if not video_data and '/watchparty/' in url: |
587 | post_data = { | |
588 | 'doc_id': 3731964053542869, | |
589 | 'variables': json.dumps({ | |
590 | 'livingRoomID': video_id, | |
591 | }), | |
592 | } | |
593 | ||
594 | prefetched_data = extract_relay_prefetched_data(r'"login_data"\s*:\s*{') | |
595 | if prefetched_data: | |
596 | lsd = try_get(prefetched_data, lambda x: x['login_data']['lsd'], dict) | |
597 | if lsd: | |
598 | post_data[lsd['name']] = lsd['value'] | |
599 | ||
600 | relay_data = extract_relay_data(r'\[\s*"RelayAPIConfigDefaults"\s*,') | |
601 | for define in (relay_data.get('define') or []): | |
602 | if define[0] == 'RelayAPIConfigDefaults': | |
603 | self._api_config = define[2] | |
604 | ||
605 | living_room = self._download_json( | |
606 | urljoin(url, self._api_config['graphURI']), video_id, | |
607 | data=urlencode_postdata(post_data))['data']['living_room'] | |
608 | ||
609 | entries = [] | |
610 | for edge in (try_get(living_room, lambda x: x['recap']['watched_content']['edges']) or []): | |
611 | video = try_get(edge, lambda x: x['node']['video']) or {} | |
612 | v_id = video.get('id') | |
613 | if not v_id: | |
614 | continue | |
615 | v_id = compat_str(v_id) | |
616 | entries.append(self.url_result( | |
617 | self._VIDEO_PAGE_TEMPLATE % v_id, | |
618 | self.ie_key(), v_id, video.get('name'))) | |
619 | ||
620 | return self.playlist_result(entries, video_id) | |
621 | ||
622 | if not video_data: | |
9b89daef S |
623 | # Video info not in first request, do a secondary request using |
624 | # tahoe player specific URL | |
9d082e7c NR |
625 | tahoe_data = self._download_webpage( |
626 | self._VIDEO_PAGE_TAHOE_TEMPLATE % video_id, video_id, | |
627 | data=urlencode_postdata({ | |
9d082e7c | 628 | '__a': 1, |
9b89daef S |
629 | '__pc': self._search_regex( |
630 | r'pkg_cohort["\']\s*:\s*["\'](.+?)["\']', webpage, | |
631 | 'pkg cohort', default='PHASED:DEFAULT'), | |
632 | '__rev': self._search_regex( | |
633 | r'client_revision["\']\s*:\s*(\d+),', webpage, | |
634 | 'client revision', default='3944515'), | |
631f93ee RA |
635 | 'fb_dtsg': self._search_regex( |
636 | r'"DTSGInitialData"\s*,\s*\[\]\s*,\s*{\s*"token"\s*:\s*"([^"]+)"', | |
637 | webpage, 'dtsg token', default=''), | |
9d082e7c NR |
638 | }), |
639 | headers={ | |
640 | 'Content-Type': 'application/x-www-form-urlencoded', | |
641 | }) | |
9b89daef S |
642 | tahoe_js_data = self._parse_json( |
643 | self._search_regex( | |
644 | r'for\s+\(\s*;\s*;\s*\)\s*;(.+)', tahoe_data, | |
645 | 'tahoe js data', default='{}'), | |
646 | video_id, fatal=False) | |
647 | video_data = extract_from_jsmods_instances(tahoe_js_data) | |
9d082e7c | 648 | |
c1406299 | 649 | if not video_data: |
9b89daef | 650 | raise ExtractorError('Cannot parse data') |
ffdf972b | 651 | |
29f7c58a | 652 | if len(video_data) > 1: |
653 | entries = [] | |
654 | for v in video_data: | |
655 | video_url = v[0].get('video_url') | |
656 | if not video_url: | |
657 | continue | |
658 | entries.append(self.url_result(urljoin( | |
659 | url, video_url), self.ie_key(), v[0].get('video_id'))) | |
660 | return self.playlist_result(entries, video_id) | |
661 | video_data = video_data[0] | |
662 | ||
ffdf972b | 663 | formats = [] |
29f7c58a | 664 | subtitles = {} |
d9ee2e5c YCH |
665 | for f in video_data: |
666 | format_id = f['stream_type'] | |
ca74c90b SC |
667 | if f and isinstance(f, dict): |
668 | f = [f] | |
44d6dd08 S |
669 | if not f or not isinstance(f, list): |
670 | continue | |
671 | for quality in ('sd', 'hd'): | |
672 | for src_type in ('src', 'src_no_ratelimit'): | |
673 | src = f[0].get('%s_%s' % (quality, src_type)) | |
674 | if src: | |
8a2d9923 | 675 | preference = -10 if format_id == 'progressive' else -1 |
c24883a1 YCH |
676 | if quality == 'hd': |
677 | preference += 5 | |
44d6dd08 S |
678 | formats.append({ |
679 | 'format_id': '%s_%s_%s' % (format_id, quality, src_type), | |
680 | 'url': src, | |
f983b875 | 681 | 'quality': preference, |
8a2d9923 | 682 | 'height': 720 if quality == 'hd' else None |
44d6dd08 | 683 | }) |
29f7c58a | 684 | extract_dash_manifest(f[0], formats) |
07154c79 RA |
685 | subtitles_src = f[0].get('subtitles_src') |
686 | if subtitles_src: | |
687 | subtitles.setdefault('en', []).append({'url': subtitles_src}) | |
9eae41dd | 688 | |
9c7b509b | 689 | info_dict = { |
9eae41dd | 690 | 'id': video_id, |
ffdf972b | 691 | 'formats': formats, |
07154c79 | 692 | 'subtitles': subtitles, |
9eae41dd | 693 | } |
9f14daf2 | 694 | process_formats(info_dict) |
a181cd0c | 695 | info_dict.update(extract_metadata(webpage)) |
de691a49 | 696 | |
29f7c58a | 697 | return info_dict |
de691a49 S |
698 | |
699 | def _real_extract(self, url): | |
9c7b509b YCH |
700 | video_id = self._match_id(url) |
701 | ||
702 | real_url = self._VIDEO_PAGE_TEMPLATE % video_id if url.startswith('facebook:') else url | |
29f7c58a | 703 | return self._extract_from_url(real_url, video_id) |
349fc5c7 S |
704 | |
705 | ||
706 | class FacebookPluginsVideoIE(InfoExtractor): | |
707 | _VALID_URL = r'https?://(?:[\w-]+\.)?facebook\.com/plugins/video\.php\?.*?\bhref=(?P<id>https.+)' | |
708 | ||
709 | _TESTS = [{ | |
710 | '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', | |
711 | 'md5': '5954e92cdfe51fe5782ae9bda7058a07', | |
712 | 'info_dict': { | |
713 | 'id': '10154383743583686', | |
714 | 'ext': 'mp4', | |
b2db8102 | 715 | # TODO: Fix title, uploader |
349fc5c7 S |
716 | 'title': 'What to do during the haze?', |
717 | 'uploader': 'Gov.sg', | |
718 | 'upload_date': '20160826', | |
719 | 'timestamp': 1472184808, | |
720 | }, | |
721 | 'add_ie': [FacebookIE.ie_key()], | |
722 | }, { | |
723 | 'url': 'https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fvideo.php%3Fv%3D10204634152394104', | |
724 | 'only_matching': True, | |
725 | }, { | |
726 | 'url': 'https://www.facebook.com/plugins/video.php?href=https://www.facebook.com/gov.sg/videos/10154383743583686/&show_text=0&width=560', | |
727 | 'only_matching': True, | |
728 | }] | |
729 | ||
730 | def _real_extract(self, url): | |
731 | return self.url_result( | |
732 | compat_urllib_parse_unquote(self._match_id(url)), | |
733 | FacebookIE.ie_key()) | |
5f549d49 | 734 | |
735 | ||
736 | class FacebookRedirectURLIE(InfoExtractor): | |
737 | IE_DESC = False # Do not list | |
738 | _VALID_URL = r'https?://(?:[\w-]+\.)?facebook\.com/flx/warn[/?]' | |
739 | _TESTS = [{ | |
740 | 'url': 'https://www.facebook.com/flx/warn/?h=TAQHsoToz&u=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DpO8h3EaFRdo&s=1', | |
741 | 'info_dict': { | |
742 | 'id': 'pO8h3EaFRdo', | |
743 | 'ext': 'mp4', | |
744 | 'title': 'Tripeo Boiler Room x Dekmantel Festival DJ Set', | |
745 | 'description': 'md5:2d713ccbb45b686a1888397b2c77ca6b', | |
746 | 'channel_id': 'UCGBpxWJr9FNOcFYA5GkKrMg', | |
747 | 'playable_in_embed': True, | |
748 | 'categories': ['Music'], | |
749 | 'channel': 'Boiler Room', | |
750 | 'uploader_id': 'brtvofficial', | |
751 | 'uploader': 'Boiler Room', | |
752 | 'tags': 'count:11', | |
753 | 'duration': 3332, | |
754 | 'live_status': 'not_live', | |
755 | 'thumbnail': 'https://i.ytimg.com/vi/pO8h3EaFRdo/maxresdefault.jpg', | |
756 | 'channel_url': 'https://www.youtube.com/channel/UCGBpxWJr9FNOcFYA5GkKrMg', | |
757 | 'availability': 'public', | |
758 | 'uploader_url': 'http://www.youtube.com/user/brtvofficial', | |
759 | 'upload_date': '20150917', | |
760 | 'age_limit': 0, | |
761 | 'view_count': int, | |
762 | 'like_count': int, | |
763 | }, | |
764 | 'add_ie': ['Youtube'], | |
765 | 'params': {'skip_download': 'Youtube'}, | |
766 | }] | |
767 | ||
768 | def _real_extract(self, url): | |
769 | redirect_url = url_or_none(parse_qs(url).get('u', [None])[-1]) | |
770 | if not redirect_url: | |
771 | raise ExtractorError('Invalid facebook redirect URL', expected=True) | |
772 | return self.url_result(redirect_url) | |
63be30e3 | 773 | |
774 | ||
775 | class FacebookReelIE(InfoExtractor): | |
776 | _VALID_URL = r'https?://(?:[\w-]+\.)?facebook\.com/reel/(?P<id>\d+)' | |
777 | IE_NAME = 'facebook:reel' | |
778 | ||
779 | _TESTS = [{ | |
780 | 'url': 'https://www.facebook.com/reel/1195289147628387', | |
781 | 'md5': 'c4ff9a7182ff9ff7d6f7a83603bae831', | |
782 | 'info_dict': { | |
783 | 'id': '1195289147628387', | |
784 | 'ext': 'mp4', | |
785 | 'title': 'md5:9f5b142921b2dc57004fa13f76005f87', | |
786 | 'description': 'md5:24ea7ef062215d295bdde64e778f5474', | |
787 | 'uploader': 'Beast Camp Training', | |
788 | 'uploader_id': '1738535909799870', | |
789 | 'duration': 9.536, | |
790 | 'thumbnail': r're:^https?://.*', | |
791 | 'upload_date': '20211121', | |
792 | 'timestamp': 1637502604, | |
793 | } | |
794 | }] | |
795 | ||
796 | def _real_extract(self, url): | |
797 | video_id = self._match_id(url) | |
798 | return self.url_result( | |
799 | f'https://m.facebook.com/watch/?v={video_id}&_rdr', FacebookIE, video_id) |