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