]>
Commit | Line | Data |
---|---|---|
cb3bb2cf PH |
1 | from __future__ import unicode_literals |
2 | ||
9eae41dd | 3 | import json |
9eae41dd PH |
4 | import re |
5 | import socket | |
6 | ||
7 | from .common import InfoExtractor | |
8c25f81b | 8 | from ..compat import ( |
9eae41dd PH |
9 | compat_http_client, |
10 | compat_str, | |
11 | compat_urllib_error, | |
12 | compat_urllib_parse, | |
13 | compat_urllib_request, | |
8c25f81b PH |
14 | ) |
15 | from ..utils import ( | |
9eae41dd | 16 | ExtractorError, |
2fc9f2b4 | 17 | int_or_none, |
a020a0dc | 18 | limit_length, |
2fc9f2b4 | 19 | urlencode_postdata, |
9eae41dd PH |
20 | ) |
21 | ||
22 | ||
23 | class FacebookIE(InfoExtractor): | |
9834872b | 24 | _VALID_URL = r'''(?x) |
9d9d70c4 | 25 | https?://(?:\w+\.)?facebook\.com/ |
a6da7b6b | 26 | (?:[^#]*?\#!/)? |
60ac04e5 | 27 | (?:video/video\.php|photo\.php|video\.php|video/embed)\?(?:.*?) |
9834872b PH |
28 | (?:v|video_id)=(?P<id>[0-9]+) |
29 | (?:.*)''' | |
67874aef JMF |
30 | _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1' |
31 | _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1' | |
9eae41dd | 32 | _NETRC_MACHINE = 'facebook' |
cb3bb2cf | 33 | IE_NAME = 'facebook' |
60ac04e5 | 34 | _TESTS = [{ |
50317dbb PH |
35 | 'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf', |
36 | 'md5': '6a40d33c0eccbb1af76cf0485a052659', | |
cb3bb2cf | 37 | 'info_dict': { |
50317dbb | 38 | 'id': '637842556329505', |
cb3bb2cf | 39 | 'ext': 'mp4', |
5e4f0619 | 40 | 'title': 're:Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam', |
6f5ac90c | 41 | } |
a020a0dc PH |
42 | }, { |
43 | 'note': 'Video without discernible title', | |
44 | 'url': 'https://www.facebook.com/video.php?v=274175099429670', | |
45 | 'info_dict': { | |
46 | 'id': '274175099429670', | |
47 | 'ext': 'mp4', | |
48 | 'title': 'Facebook video #274175099429670', | |
49 | } | |
60ac04e5 PH |
50 | }, { |
51 | 'url': 'https://www.facebook.com/video.php?v=10204634152394104', | |
52 | 'only_matching': True, | |
53 | }] | |
9eae41dd | 54 | |
67874aef JMF |
55 | def _login(self): |
56 | (useremail, password) = self._get_login_info() | |
9eae41dd PH |
57 | if useremail is None: |
58 | return | |
59 | ||
67874aef JMF |
60 | login_page_req = compat_urllib_request.Request(self._LOGIN_URL) |
61 | login_page_req.add_header('Cookie', 'locale=en_US') | |
b74fa8cd | 62 | login_page = self._download_webpage(login_page_req, None, |
9e1a5b84 JW |
63 | note='Downloading login page', |
64 | errnote='Unable to download login page') | |
b37b9450 | 65 | lsd = self._search_regex( |
d0ff8384 | 66 | r'<input type="hidden" name="lsd" value="([^"]*)"', |
b37b9450 | 67 | login_page, 'lsd') |
cb3bb2cf | 68 | lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd') |
67874aef | 69 | |
9eae41dd PH |
70 | login_form = { |
71 | 'email': useremail, | |
72 | 'pass': password, | |
67874aef JMF |
73 | 'lsd': lsd, |
74 | 'lgnrnd': lgnrnd, | |
75 | 'next': 'http://facebook.com/home.php', | |
76 | 'default_persistent': '0', | |
77 | 'legacy_return': '1', | |
78 | 'timezone': '-60', | |
79 | 'trynum': '1', | |
b74e86f4 | 80 | } |
b74fa8cd | 81 | request = compat_urllib_request.Request(self._LOGIN_URL, urlencode_postdata(login_form)) |
67874aef | 82 | request.add_header('Content-Type', 'application/x-www-form-urlencoded') |
9eae41dd | 83 | try: |
b74fa8cd | 84 | login_results = self._download_webpage(request, None, |
9e1a5b84 | 85 | note='Logging in', errnote='unable to fetch login page') |
9eae41dd | 86 | if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None: |
cb3bb2cf | 87 | self._downloader.report_warning('unable to log in: bad username/password, or exceded login rate limit (~3/min). Check credentials or wait.') |
9eae41dd | 88 | return |
67874aef JMF |
89 | |
90 | check_form = { | |
b74fa8cd | 91 | 'fb_dtsg': self._search_regex(r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg'), |
7d4d5f25 PH |
92 | 'h': self._search_regex( |
93 | r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h'), | |
67874aef | 94 | 'name_action_selected': 'dont_save', |
67874aef | 95 | } |
b74fa8cd | 96 | check_req = compat_urllib_request.Request(self._CHECKPOINT_URL, urlencode_postdata(check_form)) |
67874aef | 97 | check_req.add_header('Content-Type', 'application/x-www-form-urlencoded') |
b74fa8cd | 98 | check_response = self._download_webpage(check_req, None, |
9e1a5b84 | 99 | note='Confirming login') |
67874aef | 100 | if re.search(r'id="checkpointSubmitButton"', check_response) is not None: |
cb3bb2cf | 101 | self._downloader.report_warning('Unable to confirm login, you have to login in your brower and authorize the login.') |
9eae41dd | 102 | except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err: |
cb3bb2cf | 103 | self._downloader.report_warning('unable to log in: %s' % compat_str(err)) |
9eae41dd PH |
104 | return |
105 | ||
67874aef JMF |
106 | def _real_initialize(self): |
107 | self._login() | |
108 | ||
9eae41dd | 109 | def _real_extract(self, url): |
2fc9f2b4 | 110 | video_id = self._match_id(url) |
9eae41dd PH |
111 | url = 'https://www.facebook.com/video/video.php?v=%s' % video_id |
112 | webpage = self._download_webpage(url, video_id) | |
113 | ||
114 | BEFORE = '{swf.addParam(param[0], param[1]);});\n' | |
115 | AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});' | |
116 | m = re.search(re.escape(BEFORE) + '(.*?)' + re.escape(AFTER), webpage) | |
117 | if not m: | |
6f71ef58 JMF |
118 | m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage) |
119 | if m_msg is not None: | |
749a4fd2 | 120 | raise ExtractorError( |
cb3bb2cf | 121 | 'The video is not available, Facebook said: "%s"' % m_msg.group(1), |
749a4fd2 | 122 | expected=True) |
6f71ef58 | 123 | else: |
cb3bb2cf | 124 | raise ExtractorError('Cannot parse data') |
9eae41dd PH |
125 | data = dict(json.loads(m.group(1))) |
126 | params_raw = compat_urllib_parse.unquote(data['params']) | |
127 | params = json.loads(params_raw) | |
128 | video_data = params['video_data'][0] | |
129 | video_url = video_data.get('hd_src') | |
130 | if not video_url: | |
131 | video_url = video_data['sd_src'] | |
132 | if not video_url: | |
cb3bb2cf | 133 | raise ExtractorError('Cannot find video URL') |
9eae41dd | 134 | |
81ec7c79 | 135 | video_title = self._html_search_regex( |
50317dbb PH |
136 | r'<h2 class="uiHeaderTitle">([^<]*)</h2>', webpage, 'title', |
137 | fatal=False) | |
138 | if not video_title: | |
139 | video_title = self._html_search_regex( | |
140 | r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>', | |
141 | webpage, 'alternative title', default=None) | |
a020a0dc | 142 | video_title = limit_length(video_title, 80) |
50317dbb PH |
143 | if not video_title: |
144 | video_title = 'Facebook video #%s' % video_id | |
9eae41dd | 145 | |
9d9d70c4 | 146 | return { |
9eae41dd PH |
147 | 'id': video_id, |
148 | 'title': video_title, | |
149 | 'url': video_url, | |
2fc9f2b4 PH |
150 | 'duration': int_or_none(video_data.get('video_duration')), |
151 | 'thumbnail': video_data.get('thumbnail_src'), | |
9eae41dd | 152 | } |