1 from __future__ import unicode_literals
7 from .common import InfoExtractor
13 compat_urllib_request,
20 class FacebookIE(InfoExtractor):
22 https?://(?:\w+\.)?facebook\.com/
24 (?:video/video\.php|photo\.php|video\.php|video/embed)\?(?:.*?)
25 (?:v|video_id)=(?P<id>[0-9]+)
27 _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1'
28 _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1'
29 _NETRC_MACHINE = 'facebook'
32 'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf',
33 'md5': '6a40d33c0eccbb1af76cf0485a052659',
35 'id': '637842556329505',
38 'title': 'Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam fin...',
41 'url': 'https://www.facebook.com/video.php?v=10204634152394104',
42 'only_matching': True,
46 (useremail, password) = self._get_login_info()
50 login_page_req = compat_urllib_request.Request(self._LOGIN_URL)
51 login_page_req.add_header('Cookie', 'locale=en_US')
52 login_page = self._download_webpage(login_page_req, None,
53 note='Downloading login page',
54 errnote='Unable to download login page')
55 lsd = self._search_regex(
56 r'<input type="hidden" name="lsd" value="([^"]*)"',
58 lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd')
65 'next': 'http://facebook.com/home.php',
66 'default_persistent': '0',
71 request = compat_urllib_request.Request(self._LOGIN_URL, urlencode_postdata(login_form))
72 request.add_header('Content-Type', 'application/x-www-form-urlencoded')
74 login_results = self._download_webpage(request, None,
75 note='Logging in', errnote='unable to fetch login page')
76 if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
77 self._downloader.report_warning('unable to log in: bad username/password, or exceded login rate limit (~3/min). Check credentials or wait.')
81 'fb_dtsg': self._search_regex(r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg'),
82 'h': self._search_regex(
83 r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h'),
84 'name_action_selected': 'dont_save',
86 check_req = compat_urllib_request.Request(self._CHECKPOINT_URL, urlencode_postdata(check_form))
87 check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
88 check_response = self._download_webpage(check_req, None,
89 note='Confirming login')
90 if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
91 self._downloader.report_warning('Unable to confirm login, you have to login in your brower and authorize the login.')
92 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
93 self._downloader.report_warning('unable to log in: %s' % compat_str(err))
96 def _real_initialize(self):
99 def _real_extract(self, url):
100 mobj = re.match(self._VALID_URL, url)
101 video_id = mobj.group('id')
103 url = 'https://www.facebook.com/video/video.php?v=%s' % video_id
104 webpage = self._download_webpage(url, video_id)
106 BEFORE = '{swf.addParam(param[0], param[1]);});\n'
107 AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
108 m = re.search(re.escape(BEFORE) + '(.*?)' + re.escape(AFTER), webpage)
110 m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
111 if m_msg is not None:
112 raise ExtractorError(
113 'The video is not available, Facebook said: "%s"' % m_msg.group(1),
116 raise ExtractorError('Cannot parse data')
117 data = dict(json.loads(m.group(1)))
118 params_raw = compat_urllib_parse.unquote(data['params'])
119 params = json.loads(params_raw)
120 video_data = params['video_data'][0]
121 video_url = video_data.get('hd_src')
123 video_url = video_data['sd_src']
125 raise ExtractorError('Cannot find video URL')
127 video_title = self._html_search_regex(
128 r'<h2 class="uiHeaderTitle">([^<]*)</h2>', webpage, 'title',
131 video_title = self._html_search_regex(
132 r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>',
133 webpage, 'alternative title', default=None)
134 if len(video_title) > 80 + 3:
135 video_title = video_title[:80] + '...'
137 video_title = 'Facebook video #%s' % video_id
141 'title': video_title,
143 'duration': int(video_data['video_duration']),
144 'thumbnail': video_data['thumbnail_src'],