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