1 from __future__ import unicode_literals
7 from .common import InfoExtractor
12 compat_urllib_parse_unquote,
13 compat_urllib_request,
25 class FacebookIE(InfoExtractor):
27 https?://(?:\w+\.)?facebook\.com/
30 (?:video/video\.php|photo\.php|video\.php|video/embed)\?(?:.*?)
32 [^/]+/videos/(?:[^/]+/)?
36 _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1'
37 _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1'
38 _NETRC_MACHINE = 'facebook'
41 'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf',
42 'md5': '6a40d33c0eccbb1af76cf0485a052659',
44 'id': '637842556329505',
46 'title': 're:Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam',
49 'note': 'Video without discernible title',
50 'url': 'https://www.facebook.com/video.php?v=274175099429670',
52 'id': '274175099429670',
54 'title': 'Facebook video #274175099429670',
56 'expected_warnings': [
60 'url': 'https://www.facebook.com/video.php?v=10204634152394104',
61 'only_matching': True,
63 'url': 'https://www.facebook.com/amogood/videos/1618742068337349/?fref=nf',
64 'only_matching': True,
66 'url': 'https://www.facebook.com/ChristyClarkForBC/videos/vb.22819070941/10153870694020942/?type=2&theater',
67 'only_matching': True,
71 (useremail, password) = self._get_login_info()
75 login_page_req = compat_urllib_request.Request(self._LOGIN_URL)
76 login_page_req.add_header('Cookie', 'locale=en_US')
77 login_page = self._download_webpage(login_page_req, None,
78 note='Downloading login page',
79 errnote='Unable to download login page')
80 lsd = self._search_regex(
81 r'<input type="hidden" name="lsd" value="([^"]*)"',
83 lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd')
90 'next': 'http://facebook.com/home.php',
91 'default_persistent': '0',
96 request = compat_urllib_request.Request(self._LOGIN_URL, urlencode_postdata(login_form))
97 request.add_header('Content-Type', 'application/x-www-form-urlencoded')
99 login_results = self._download_webpage(request, None,
100 note='Logging in', errnote='unable to fetch login page')
101 if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
102 self._downloader.report_warning('unable to log in: bad username/password, or exceded login rate limit (~3/min). Check credentials or wait.')
106 'fb_dtsg': self._search_regex(r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg'),
107 'h': self._search_regex(
108 r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h'),
109 'name_action_selected': 'dont_save',
111 check_req = compat_urllib_request.Request(self._CHECKPOINT_URL, urlencode_postdata(check_form))
112 check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
113 check_response = self._download_webpage(check_req, None,
114 note='Confirming login')
115 if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
116 self._downloader.report_warning('Unable to confirm login, you have to login in your brower and authorize the login.')
117 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
118 self._downloader.report_warning('unable to log in: %s' % compat_str(err))
121 def _real_initialize(self):
124 def _real_extract(self, url):
125 video_id = self._match_id(url)
126 url = 'https://www.facebook.com/video/video.php?v=%s' % video_id
127 webpage = self._download_webpage(url, video_id)
129 BEFORE = '{swf.addParam(param[0], param[1]);});\n'
130 AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
131 m = re.search(re.escape(BEFORE) + '(.*?)' + re.escape(AFTER), webpage)
133 m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
134 if m_msg is not None:
135 raise ExtractorError(
136 'The video is not available, Facebook said: "%s"' % m_msg.group(1),
139 raise ExtractorError('Cannot parse data')
140 data = dict(json.loads(m.group(1)))
141 params_raw = compat_urllib_parse_unquote(data['params'])
142 params = json.loads(params_raw)
143 video_data = params['video_data'][0]
146 for quality in ['sd', 'hd']:
147 src = video_data.get('%s_src' % quality)
150 'format_id': quality,
154 raise ExtractorError('Cannot find video formats')
156 video_title = self._html_search_regex(
157 r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>([^<]*)</h2>', webpage, 'title',
160 video_title = self._html_search_regex(
161 r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>',
162 webpage, 'alternative title', fatal=False)
163 video_title = limit_length(video_title, 80)
165 video_title = 'Facebook video #%s' % video_id
166 uploader = clean_html(get_element_by_id('fbPhotoPageAuthorName', webpage))
170 'title': video_title,
172 'duration': int_or_none(video_data.get('video_duration')),
173 'thumbnail': video_data.get('thumbnail_src'),
174 'uploader': uploader,