5 from .common import InfoExtractor
11 compat_urllib_request,
17 class FacebookIE(InfoExtractor):
18 """Information Extractor for Facebook"""
20 _VALID_URL = r'^(?:https?://)?(?:\w+\.)?facebook\.com/(?:[^#?]*#!/)?(?:video/video|photo)\.php\?(?:.*?)v=(?P<ID>\d+)(?:.*)'
21 _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1'
22 _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1'
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')
40 (useremail, password) = self._get_login_info()
44 login_page_req = compat_urllib_request.Request(self._LOGIN_URL)
45 login_page_req.add_header('Cookie', 'locale=en_US')
47 login_page = self._download_webpage(login_page_req, None, note=False,
48 errnote=u'Unable to download login page')
49 lsd = self._search_regex(r'"lsd":"(\w*?)"', login_page, u'lsd')
50 lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, u'lgnrnd')
57 'next': 'http://facebook.com/home.php',
58 'default_persistent': '0',
63 request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(login_form))
64 request.add_header('Content-Type', 'application/x-www-form-urlencoded')
66 login_results = compat_urllib_request.urlopen(request).read()
67 if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
68 self._downloader.report_warning(u'unable to log in: bad username/password, or exceded login rate limit (~3/min). Check credentials or wait.')
72 'fb_dtsg': self._search_regex(r'"fb_dtsg":"(.*?)"', login_results, u'fb_dtsg'),
73 'nh': self._search_regex(r'name="nh" value="(\w*?)"', login_results, u'nh'),
74 'name_action_selected': 'dont_save',
75 'submit[Continue]': self._search_regex(r'<input value="(.*?)" name="submit\[Continue\]"', login_results, u'continue'),
77 check_req = compat_urllib_request.Request(self._CHECKPOINT_URL, compat_urllib_parse.urlencode(check_form))
78 check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
79 check_response = compat_urllib_request.urlopen(check_req).read()
80 if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
81 self._downloader.report_warning(u'Unable to confirm login, you have to login in your brower and authorize the login.')
82 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
83 self._downloader.report_warning(u'unable to log in: %s' % compat_str(err))
86 def _real_initialize(self):
89 def _real_extract(self, url):
90 mobj = re.match(self._VALID_URL, url)
92 raise ExtractorError(u'Invalid URL: %s' % url)
93 video_id = mobj.group('ID')
95 url = 'https://www.facebook.com/video/video.php?v=%s' % video_id
96 webpage = self._download_webpage(url, video_id)
98 BEFORE = '{swf.addParam(param[0], param[1]);});\n'
99 AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
100 m = re.search(re.escape(BEFORE) + '(.*?)' + re.escape(AFTER), webpage)
102 m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
103 if m_msg is not None:
104 raise ExtractorError(
105 u'The video is not available, Facebook said: "%s"' % m_msg.group(1),
108 raise ExtractorError(u'Cannot parse data')
109 data = dict(json.loads(m.group(1)))
110 params_raw = compat_urllib_parse.unquote(data['params'])
111 params = json.loads(params_raw)
112 video_data = params['video_data'][0]
113 video_url = video_data.get('hd_src')
115 video_url = video_data['sd_src']
117 raise ExtractorError(u'Cannot find video URL')
118 video_duration = int(video_data['video_duration'])
119 thumbnail = video_data['thumbnail_src']
121 video_title = self._html_search_regex(
122 r'<h2 class="uiHeaderTitle">([^<]*)</h2>', webpage, u'title')
126 'title': video_title,
129 'duration': video_duration,
130 'thumbnail': thumbnail,