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