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