]>
Commit | Line | Data |
---|---|---|
cb3bb2cf PH |
1 | from __future__ import unicode_literals |
2 | ||
9eae41dd | 3 | import json |
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 ( | |
9b9c5355 | 16 | error_to_compat_str, |
9eae41dd | 17 | ExtractorError, |
a020a0dc | 18 | limit_length, |
5c2266df | 19 | sanitized_Request, |
2fc9f2b4 | 20 | urlencode_postdata, |
51a57515 | 21 | get_element_by_id, |
22 | clean_html, | |
9eae41dd PH |
23 | ) |
24 | ||
25 | ||
26 | class FacebookIE(InfoExtractor): | |
9834872b | 27 | _VALID_URL = r'''(?x) |
2e6e742c S |
28 | (?: |
29 | https?:// | |
30 | (?:\w+\.)?facebook\.com/ | |
31 | (?:[^#]*?\#!/)? | |
32 | (?: | |
33 | (?: | |
34 | video/video\.php| | |
35 | photo\.php| | |
36 | video\.php| | |
efbeddea YCH |
37 | video/embed| |
38 | story\.php | |
39 | )\?(?:.*?)(?:v|video_id|story_fbid)=| | |
9c7b509b | 40 | [^/]+/videos/(?:[^/]+/)?| |
e738e433 YCH |
41 | [^/]+/posts/| |
42 | groups/[^/]+/permalink/ | |
2e6e742c S |
43 | )| |
44 | facebook: | |
45 | ) | |
46 | (?P<id>[0-9]+) | |
47 | ''' | |
67874aef JMF |
48 | _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1' |
49 | _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1' | |
9eae41dd | 50 | _NETRC_MACHINE = 'facebook' |
cb3bb2cf | 51 | IE_NAME = 'facebook' |
0803753f YCH |
52 | |
53 | _CHROME_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36' | |
54 | ||
9c7b509b YCH |
55 | _VIDEO_PAGE_TEMPLATE = 'https://www.facebook.com/video/video.php?v=%s' |
56 | ||
60ac04e5 | 57 | _TESTS = [{ |
50317dbb PH |
58 | 'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf', |
59 | 'md5': '6a40d33c0eccbb1af76cf0485a052659', | |
cb3bb2cf | 60 | 'info_dict': { |
50317dbb | 61 | 'id': '637842556329505', |
cb3bb2cf | 62 | 'ext': 'mp4', |
5e4f0619 | 63 | 'title': 're:Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam', |
67b8a28a | 64 | 'uploader': 'Tennis on Facebook', |
6f5ac90c | 65 | } |
a020a0dc PH |
66 | }, { |
67 | 'note': 'Video without discernible title', | |
68 | 'url': 'https://www.facebook.com/video.php?v=274175099429670', | |
69 | 'info_dict': { | |
70 | 'id': '274175099429670', | |
71 | 'ext': 'mp4', | |
72 | 'title': 'Facebook video #274175099429670', | |
67b8a28a | 73 | 'uploader': 'Asif Nawab Butt', |
db3ca364 YCH |
74 | }, |
75 | 'expected_warnings': [ | |
76 | 'title' | |
77 | ] | |
0803753f YCH |
78 | }, { |
79 | 'note': 'Video with DASH manifest', | |
80 | 'url': 'https://www.facebook.com/video.php?v=957955867617029', | |
248ae880 | 81 | 'md5': '54706e4db4f5ad58fbad82dde1f1213f', |
0803753f YCH |
82 | 'info_dict': { |
83 | 'id': '957955867617029', | |
84 | 'ext': 'mp4', | |
85 | 'title': 'When you post epic content on instagram.com/433 8 million followers, this is ...', | |
86 | 'uploader': 'Demy de Zeeuw', | |
87 | }, | |
9c7b509b YCH |
88 | }, { |
89 | 'url': 'https://www.facebook.com/maxlayn/posts/10153807558977570', | |
90 | 'md5': '037b1fa7f3c2d02b7a0d7bc16031ecc6', | |
91 | 'info_dict': { | |
92 | 'id': '544765982287235', | |
93 | 'ext': 'mp4', | |
94 | 'title': '"What are you doing running in the snow?"', | |
95 | 'uploader': 'FailArmy', | |
96 | } | |
98801241 YCH |
97 | }, { |
98 | 'url': 'https://m.facebook.com/story.php?story_fbid=1035862816472149&id=116132035111903', | |
99 | 'md5': '1deb90b6ac27f7efcf6d747c8a27f5e3', | |
100 | 'info_dict': { | |
101 | 'id': '1035862816472149', | |
102 | 'ext': 'mp4', | |
103 | 'title': 'What the Flock Is Going On In New Zealand Credit: ViralHog', | |
104 | 'uploader': 'S. Saint', | |
105 | }, | |
5080cbf9 YCH |
106 | }, { |
107 | 'note': 'swf params escaped', | |
108 | 'url': 'https://www.facebook.com/barackobama/posts/10153664894881749', | |
109 | 'md5': '97ba073838964d12c70566e0085c2b91', | |
110 | 'info_dict': { | |
111 | 'id': '10153664894881749', | |
112 | 'ext': 'mp4', | |
113 | 'title': 'Facebook video #10153664894881749', | |
114 | }, | |
60ac04e5 PH |
115 | }, { |
116 | 'url': 'https://www.facebook.com/video.php?v=10204634152394104', | |
117 | 'only_matching': True, | |
c6256697 YCH |
118 | }, { |
119 | 'url': 'https://www.facebook.com/amogood/videos/1618742068337349/?fref=nf', | |
120 | 'only_matching': True, | |
53faa3ca YCH |
121 | }, { |
122 | 'url': 'https://www.facebook.com/ChristyClarkForBC/videos/vb.22819070941/10153870694020942/?type=2&theater', | |
123 | 'only_matching': True, | |
2e6e742c S |
124 | }, { |
125 | 'url': 'facebook:544765982287235', | |
126 | 'only_matching': True, | |
e738e433 YCH |
127 | }, { |
128 | 'url': 'https://www.facebook.com/groups/164828000315060/permalink/764967300301124/', | |
129 | 'only_matching': True, | |
60ac04e5 | 130 | }] |
9eae41dd | 131 | |
67874aef JMF |
132 | def _login(self): |
133 | (useremail, password) = self._get_login_info() | |
9eae41dd PH |
134 | if useremail is None: |
135 | return | |
136 | ||
5c2266df | 137 | login_page_req = sanitized_Request(self._LOGIN_URL) |
c1f49e16 | 138 | self._set_cookie('facebook.com', 'locale', 'en_US') |
b74fa8cd | 139 | login_page = self._download_webpage(login_page_req, None, |
9e1a5b84 JW |
140 | note='Downloading login page', |
141 | errnote='Unable to download login page') | |
b37b9450 | 142 | lsd = self._search_regex( |
d0ff8384 | 143 | r'<input type="hidden" name="lsd" value="([^"]*)"', |
b37b9450 | 144 | login_page, 'lsd') |
cb3bb2cf | 145 | lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd') |
67874aef | 146 | |
9eae41dd PH |
147 | login_form = { |
148 | 'email': useremail, | |
149 | 'pass': password, | |
67874aef JMF |
150 | 'lsd': lsd, |
151 | 'lgnrnd': lgnrnd, | |
152 | 'next': 'http://facebook.com/home.php', | |
153 | 'default_persistent': '0', | |
154 | 'legacy_return': '1', | |
155 | 'timezone': '-60', | |
156 | 'trynum': '1', | |
b74e86f4 | 157 | } |
5c2266df | 158 | request = sanitized_Request(self._LOGIN_URL, urlencode_postdata(login_form)) |
67874aef | 159 | request.add_header('Content-Type', 'application/x-www-form-urlencoded') |
9eae41dd | 160 | try: |
b74fa8cd | 161 | login_results = self._download_webpage(request, None, |
9e1a5b84 | 162 | note='Logging in', errnote='unable to fetch login page') |
9eae41dd | 163 | if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None: |
9f66931e S |
164 | error = self._html_search_regex( |
165 | r'(?s)<div[^>]+class=(["\']).*?login_error_box.*?\1[^>]*><div[^>]*>.*?</div><div[^>]*>(?P<error>.+?)</div>', | |
166 | login_results, 'login error', default=None, group='error') | |
167 | if error: | |
168 | raise ExtractorError('Unable to login: %s' % error, expected=True) | |
dfb1b146 | 169 | self._downloader.report_warning('unable to log in: bad username/password, or exceeded login rate limit (~3/min). Check credentials or wait.') |
9eae41dd | 170 | return |
67874aef | 171 | |
c1f49e16 S |
172 | fb_dtsg = self._search_regex( |
173 | r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg', default=None) | |
174 | h = self._search_regex( | |
175 | r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h', default=None) | |
176 | ||
177 | if not fb_dtsg or not h: | |
178 | return | |
179 | ||
67874aef | 180 | check_form = { |
c1f49e16 S |
181 | 'fb_dtsg': fb_dtsg, |
182 | 'h': h, | |
67874aef | 183 | 'name_action_selected': 'dont_save', |
67874aef | 184 | } |
5c2266df | 185 | check_req = sanitized_Request(self._CHECKPOINT_URL, urlencode_postdata(check_form)) |
67874aef | 186 | check_req.add_header('Content-Type', 'application/x-www-form-urlencoded') |
b74fa8cd | 187 | check_response = self._download_webpage(check_req, None, |
9e1a5b84 | 188 | note='Confirming login') |
67874aef | 189 | if re.search(r'id="checkpointSubmitButton"', check_response) is not None: |
dfb1b146 | 190 | self._downloader.report_warning('Unable to confirm login, you have to login in your browser and authorize the login.') |
9eae41dd | 191 | except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err: |
9b9c5355 | 192 | self._downloader.report_warning('unable to log in: %s' % error_to_compat_str(err)) |
9eae41dd PH |
193 | return |
194 | ||
67874aef JMF |
195 | def _real_initialize(self): |
196 | self._login() | |
197 | ||
9c7b509b YCH |
198 | def _extract_from_url(self, url, video_id, fatal_if_no_video=True): |
199 | req = sanitized_Request(url) | |
0803753f YCH |
200 | req.add_header('User-Agent', self._CHROME_USER_AGENT) |
201 | webpage = self._download_webpage(req, video_id) | |
9eae41dd | 202 | |
c1406299 YCH |
203 | video_data = None |
204 | ||
5080cbf9 | 205 | BEFORE = '{swf.addParam(param[0], param[1]);});' |
9eae41dd | 206 | AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});' |
5080cbf9 | 207 | m = re.search(re.escape(BEFORE) + '(?:\n|\\\\n)(.*?)' + re.escape(AFTER), webpage) |
c1406299 | 208 | if m: |
5080cbf9 YCH |
209 | swf_params = m.group(1).replace('\\\\', '\\').replace('\\"', '"') |
210 | data = dict(json.loads(swf_params)) | |
c1406299 YCH |
211 | params_raw = compat_urllib_parse_unquote(data['params']) |
212 | video_data = json.loads(params_raw)['video_data'] | |
213 | ||
214 | def video_data_list2dict(video_data): | |
215 | ret = {} | |
216 | for item in video_data: | |
217 | format_id = item['stream_type'] | |
218 | ret.setdefault(format_id, []).append(item) | |
219 | return ret | |
220 | ||
221 | if not video_data: | |
222 | server_js_data = self._parse_json(self._search_regex( | |
98801241 | 223 | r'handleServerJS\(({.+})\);', webpage, 'server js data', default='{}'), video_id) |
cb64ccc7 | 224 | for item in server_js_data.get('instances', []): |
c1406299 YCH |
225 | if item[1][0] == 'VideoConfig': |
226 | video_data = video_data_list2dict(item[2][0]['videoData']) | |
227 | break | |
228 | ||
229 | if not video_data: | |
9c7b509b YCH |
230 | if not fatal_if_no_video: |
231 | return webpage, False | |
6f71ef58 JMF |
232 | m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage) |
233 | if m_msg is not None: | |
749a4fd2 | 234 | raise ExtractorError( |
cb3bb2cf | 235 | 'The video is not available, Facebook said: "%s"' % m_msg.group(1), |
749a4fd2 | 236 | expected=True) |
6f71ef58 | 237 | else: |
cb3bb2cf | 238 | raise ExtractorError('Cannot parse data') |
ffdf972b JMF |
239 | |
240 | formats = [] | |
c1406299 | 241 | for format_id, f in video_data.items(): |
44d6dd08 S |
242 | if not f or not isinstance(f, list): |
243 | continue | |
244 | for quality in ('sd', 'hd'): | |
245 | for src_type in ('src', 'src_no_ratelimit'): | |
246 | src = f[0].get('%s_%s' % (quality, src_type)) | |
247 | if src: | |
c24883a1 YCH |
248 | preference = -10 if format_id == 'progressive' else 0 |
249 | if quality == 'hd': | |
250 | preference += 5 | |
44d6dd08 S |
251 | formats.append({ |
252 | 'format_id': '%s_%s_%s' % (format_id, quality, src_type), | |
253 | 'url': src, | |
c24883a1 | 254 | 'preference': preference, |
44d6dd08 | 255 | }) |
0803753f YCH |
256 | dash_manifest = f[0].get('dash_manifest') |
257 | if dash_manifest: | |
91cb6b50 | 258 | formats.extend(self._parse_mpd_formats( |
c11875b3 | 259 | compat_etree_fromstring(compat_urllib_parse_unquote_plus(dash_manifest)))) |
ffdf972b JMF |
260 | if not formats: |
261 | raise ExtractorError('Cannot find video formats') | |
9eae41dd | 262 | |
0803753f YCH |
263 | self._sort_formats(formats) |
264 | ||
81ec7c79 | 265 | video_title = self._html_search_regex( |
bf24c3d0 | 266 | r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>([^<]*)</h2>', webpage, 'title', |
db3ca364 | 267 | default=None) |
50317dbb PH |
268 | if not video_title: |
269 | video_title = self._html_search_regex( | |
270 | r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>', | |
59ee8a86 | 271 | webpage, 'alternative title', default=None) |
a020a0dc | 272 | video_title = limit_length(video_title, 80) |
50317dbb PH |
273 | if not video_title: |
274 | video_title = 'Facebook video #%s' % video_id | |
51a57515 | 275 | uploader = clean_html(get_element_by_id('fbPhotoPageAuthorName', webpage)) |
9eae41dd | 276 | |
9c7b509b | 277 | info_dict = { |
9eae41dd PH |
278 | 'id': video_id, |
279 | 'title': video_title, | |
ffdf972b | 280 | 'formats': formats, |
51a57515 | 281 | 'uploader': uploader, |
9eae41dd | 282 | } |
de691a49 | 283 | |
9c7b509b | 284 | return webpage, info_dict |
de691a49 S |
285 | |
286 | def _real_extract(self, url): | |
9c7b509b YCH |
287 | video_id = self._match_id(url) |
288 | ||
289 | real_url = self._VIDEO_PAGE_TEMPLATE % video_id if url.startswith('facebook:') else url | |
290 | webpage, info_dict = self._extract_from_url(real_url, video_id, fatal_if_no_video=False) | |
de691a49 | 291 | |
9c7b509b YCH |
292 | if info_dict: |
293 | return info_dict | |
de691a49 | 294 | |
9c7b509b YCH |
295 | if '/posts/' in url: |
296 | entries = [ | |
f6f6217a JMF |
297 | self.url_result('facebook:%s' % vid, FacebookIE.ie_key()) |
298 | for vid in self._parse_json( | |
9c7b509b YCH |
299 | self._search_regex( |
300 | r'(["\'])video_ids\1\s*:\s*(?P<ids>\[.+?\])', | |
301 | webpage, 'video ids', group='ids'), | |
302 | video_id)] | |
de691a49 | 303 | |
9c7b509b YCH |
304 | return self.playlist_result(entries, video_id) |
305 | else: | |
306 | _, info_dict = self._extract_from_url( | |
307 | self._VIDEO_PAGE_TEMPLATE % video_id, | |
308 | video_id, fatal_if_no_video=True) | |
309 | return info_dict |