5 from .common import InfoExtractor
11 compat_urllib_request,
17 class FacebookIE(InfoExtractor):
18 """Information Extractor for Facebook"""
21 (?:https?://)?(?:\w+\.)?facebook\.com/
23 (?:video/video\.php|photo\.php|video/embed)\?(?:.*?)
24 (?:v|video_id)=(?P<id>[0-9]+)
26 _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1'
27 _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1'
28 _NETRC_MACHINE = 'facebook'
31 u'url': u'https://www.facebook.com/photo.php?v=120708114770723',
32 u'file': u'120708114770723.mp4',
33 u'md5': u'48975a41ccc4b7a581abd68651c1a5a8',
36 u"title": u"PEOPLE ARE AWESOME 2013"
40 def report_login(self):
41 """Report attempt to log in."""
42 self.to_screen(u'Logging in')
45 (useremail, password) = self._get_login_info()
49 login_page_req = compat_urllib_request.Request(self._LOGIN_URL)
50 login_page_req.add_header('Cookie', 'locale=en_US')
52 login_page = self._download_webpage(login_page_req, None, note=False,
53 errnote=u'Unable to download login page')
54 lsd = self._search_regex(r'"lsd":"(\w*?)"', login_page, u'lsd')
55 lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, u'lgnrnd')
62 'next': 'http://facebook.com/home.php',
63 'default_persistent': '0',
68 request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(login_form))
69 request.add_header('Content-Type', 'application/x-www-form-urlencoded')
71 login_results = compat_urllib_request.urlopen(request).read()
72 if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
73 self._downloader.report_warning(u'unable to log in: bad username/password, or exceded login rate limit (~3/min). Check credentials or wait.')
77 'fb_dtsg': self._search_regex(r'"fb_dtsg":"(.*?)"', login_results, u'fb_dtsg'),
78 'nh': self._search_regex(r'name="nh" value="(\w*?)"', login_results, u'nh'),
79 'name_action_selected': 'dont_save',
80 'submit[Continue]': self._search_regex(r'<input value="(.*?)" name="submit\[Continue\]"', login_results, u'continue'),
82 check_req = compat_urllib_request.Request(self._CHECKPOINT_URL, compat_urllib_parse.urlencode(check_form))
83 check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
84 check_response = compat_urllib_request.urlopen(check_req).read()
85 if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
86 self._downloader.report_warning(u'Unable to confirm login, you have to login in your brower and authorize the login.')
87 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
88 self._downloader.report_warning(u'unable to log in: %s' % compat_str(err))
91 def _real_initialize(self):
94 def _real_extract(self, url):
95 mobj = re.match(self._VALID_URL, url)
97 raise ExtractorError(u'Invalid URL: %s' % url)
98 video_id = mobj.group('id')
100 url = 'https://www.facebook.com/video/video.php?v=%s' % video_id
101 webpage = self._download_webpage(url, video_id)
103 BEFORE = '{swf.addParam(param[0], param[1]);});\n'
104 AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
105 m = re.search(re.escape(BEFORE) + '(.*?)' + re.escape(AFTER), webpage)
107 m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
108 if m_msg is not None:
109 raise ExtractorError(
110 u'The video is not available, Facebook said: "%s"' % m_msg.group(1),
113 raise ExtractorError(u'Cannot parse data')
114 data = dict(json.loads(m.group(1)))
115 params_raw = compat_urllib_parse.unquote(data['params'])
116 params = json.loads(params_raw)
117 video_data = params['video_data'][0]
118 video_url = video_data.get('hd_src')
120 video_url = video_data['sd_src']
122 raise ExtractorError(u'Cannot find video URL')
123 video_duration = int(video_data['video_duration'])
124 thumbnail = video_data['thumbnail_src']
126 video_title = self._html_search_regex(
127 r'<h2 class="uiHeaderTitle">([^<]*)</h2>', webpage, u'title')
131 'title': video_title,
134 'duration': video_duration,
135 'thumbnail': thumbnail,