6 from .common import InfoExtractor
12 compat_urllib_request,
18 class FacebookIE(InfoExtractor):
19 """Information Extractor for Facebook"""
21 _VALID_URL = r'^(?:https?://)?(?:\w+\.)?facebook\.com/(?:video/video|photo)\.php\?(?:.*?)v=(?P<ID>\d+)(?:.*)'
22 _LOGIN_URL = 'https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php&'
23 _NETRC_MACHINE = 'facebook'
26 u'url': u'https://www.facebook.com/photo.php?v=120708114770723',
27 u'file': u'120708114770723.mp4',
28 u'md5': u'48975a41ccc4b7a581abd68651c1a5a8',
31 u"title": u"PEOPLE ARE AWESOME 2013"
35 def report_login(self):
36 """Report attempt to log in."""
37 self.to_screen(u'Logging in')
39 def _real_initialize(self):
40 if self._downloader is None:
45 downloader_params = self._downloader.params
47 # Attempt to use provided username and password or .netrc data
48 if downloader_params.get('username', None) is not None:
49 useremail = downloader_params['username']
50 password = downloader_params['password']
51 elif downloader_params.get('usenetrc', False):
53 info = netrc.netrc().authenticators(self._NETRC_MACHINE)
58 raise netrc.NetrcParseError('No authenticators for %s' % self._NETRC_MACHINE)
59 except (IOError, netrc.NetrcParseError) as err:
60 self._downloader.report_warning(u'parsing .netrc: %s' % compat_str(err))
72 request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(login_form))
75 login_results = compat_urllib_request.urlopen(request).read()
76 if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
77 self._downloader.report_warning(u'unable to log in: bad username/password, or exceded login rate limit (~3/min). Check credentials or wait.')
79 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
80 self._downloader.report_warning(u'unable to log in: %s' % compat_str(err))
83 def _real_extract(self, url):
84 mobj = re.match(self._VALID_URL, url)
86 raise ExtractorError(u'Invalid URL: %s' % url)
87 video_id = mobj.group('ID')
89 url = 'https://www.facebook.com/video/video.php?v=%s' % video_id
90 webpage = self._download_webpage(url, video_id)
92 BEFORE = '{swf.addParam(param[0], param[1]);});\n'
93 AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
94 m = re.search(re.escape(BEFORE) + '(.*?)' + re.escape(AFTER), webpage)
96 raise ExtractorError(u'Cannot parse data')
97 data = dict(json.loads(m.group(1)))
98 params_raw = compat_urllib_parse.unquote(data['params'])
99 params = json.loads(params_raw)
100 video_data = params['video_data'][0]
101 video_url = video_data.get('hd_src')
103 video_url = video_data['sd_src']
105 raise ExtractorError(u'Cannot find video URL')
106 video_duration = int(video_data['video_duration'])
107 thumbnail = video_data['thumbnail_src']
109 video_title = self._html_search_regex('<h2 class="uiHeaderTitle">([^<]+)</h2>',
114 'title': video_title,
117 'duration': video_duration,
118 'thumbnail': thumbnail,