Merge branch 'shahid' of https://github.com/remitamine/youtube-dl into remitamine...
[youtube-dl] / youtube_dl / extractor / facebook.py
1 from __future__ import unicode_literals
2
3 import json
4 import re
5 import socket
6
7 from .common import InfoExtractor
8 from ..compat import (
9     compat_http_client,
10     compat_str,
11     compat_urllib_error,
12     compat_urllib_parse_unquote,
13     compat_urllib_request,
14 )
15 from ..utils import (
16     ExtractorError,
17     int_or_none,
18     limit_length,
19     urlencode_postdata,
20     get_element_by_id,
21     clean_html,
22 )
23
24
25 class FacebookIE(InfoExtractor):
26     _VALID_URL = r'''(?x)
27         https?://(?:\w+\.)?facebook\.com/
28         (?:[^#]*?\#!/)?
29         (?:
30             (?:video/video\.php|photo\.php|video\.php|video/embed)\?(?:.*?)
31             (?:v|video_id)=|
32             [^/]+/videos/(?:[^/]+/)?
33         )
34         (?P<id>[0-9]+)
35         (?:.*)'''
36     _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1'
37     _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1'
38     _NETRC_MACHINE = 'facebook'
39     IE_NAME = 'facebook'
40     _TESTS = [{
41         'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf',
42         'md5': '6a40d33c0eccbb1af76cf0485a052659',
43         'info_dict': {
44             'id': '637842556329505',
45             'ext': 'mp4',
46             'title': 're:Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam',
47             'uploader': 'Tennis on Facebook',
48         }
49     }, {
50         'note': 'Video without discernible title',
51         'url': 'https://www.facebook.com/video.php?v=274175099429670',
52         'info_dict': {
53             'id': '274175099429670',
54             'ext': 'mp4',
55             'title': 'Facebook video #274175099429670',
56             'uploader': 'Asif Nawab Butt',
57         },
58         'expected_warnings': [
59             'title'
60         ]
61     }, {
62         'url': 'https://www.facebook.com/video.php?v=10204634152394104',
63         'only_matching': True,
64     }, {
65         'url': 'https://www.facebook.com/amogood/videos/1618742068337349/?fref=nf',
66         'only_matching': True,
67     }, {
68         'url': 'https://www.facebook.com/ChristyClarkForBC/videos/vb.22819070941/10153870694020942/?type=2&theater',
69         'only_matching': True,
70     }]
71
72     def _login(self):
73         (useremail, password) = self._get_login_info()
74         if useremail is None:
75             return
76
77         login_page_req = compat_urllib_request.Request(self._LOGIN_URL)
78         login_page_req.add_header('Cookie', 'locale=en_US')
79         login_page = self._download_webpage(login_page_req, None,
80                                             note='Downloading login page',
81                                             errnote='Unable to download login page')
82         lsd = self._search_regex(
83             r'<input type="hidden" name="lsd" value="([^"]*)"',
84             login_page, 'lsd')
85         lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd')
86
87         login_form = {
88             'email': useremail,
89             'pass': password,
90             'lsd': lsd,
91             'lgnrnd': lgnrnd,
92             'next': 'http://facebook.com/home.php',
93             'default_persistent': '0',
94             'legacy_return': '1',
95             'timezone': '-60',
96             'trynum': '1',
97         }
98         request = compat_urllib_request.Request(self._LOGIN_URL, urlencode_postdata(login_form))
99         request.add_header('Content-Type', 'application/x-www-form-urlencoded')
100         try:
101             login_results = self._download_webpage(request, None,
102                                                    note='Logging in', errnote='unable to fetch login page')
103             if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
104                 self._downloader.report_warning('unable to log in: bad username/password, or exceded login rate limit (~3/min). Check credentials or wait.')
105                 return
106
107             check_form = {
108                 'fb_dtsg': self._search_regex(r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg'),
109                 'h': self._search_regex(
110                     r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h'),
111                 'name_action_selected': 'dont_save',
112             }
113             check_req = compat_urllib_request.Request(self._CHECKPOINT_URL, urlencode_postdata(check_form))
114             check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
115             check_response = self._download_webpage(check_req, None,
116                                                     note='Confirming login')
117             if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
118                 self._downloader.report_warning('Unable to confirm login, you have to login in your brower and authorize the login.')
119         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
120             self._downloader.report_warning('unable to log in: %s' % compat_str(err))
121             return
122
123     def _real_initialize(self):
124         self._login()
125
126     def _real_extract(self, url):
127         video_id = self._match_id(url)
128         url = 'https://www.facebook.com/video/video.php?v=%s' % video_id
129         webpage = self._download_webpage(url, video_id)
130
131         BEFORE = '{swf.addParam(param[0], param[1]);});\n'
132         AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
133         m = re.search(re.escape(BEFORE) + '(.*?)' + re.escape(AFTER), webpage)
134         if not m:
135             m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
136             if m_msg is not None:
137                 raise ExtractorError(
138                     'The video is not available, Facebook said: "%s"' % m_msg.group(1),
139                     expected=True)
140             else:
141                 raise ExtractorError('Cannot parse data')
142         data = dict(json.loads(m.group(1)))
143         params_raw = compat_urllib_parse_unquote(data['params'])
144         params = json.loads(params_raw)
145         video_data = params['video_data'][0]
146
147         formats = []
148         for quality in ['sd', 'hd']:
149             src = video_data.get('%s_src' % quality)
150             if src is not None:
151                 formats.append({
152                     'format_id': quality,
153                     'url': src,
154                 })
155         if not formats:
156             raise ExtractorError('Cannot find video formats')
157
158         video_title = self._html_search_regex(
159             r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>([^<]*)</h2>', webpage, 'title',
160             default=None)
161         if not video_title:
162             video_title = self._html_search_regex(
163                 r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>',
164                 webpage, 'alternative title', fatal=False)
165             video_title = limit_length(video_title, 80)
166         if not video_title:
167             video_title = 'Facebook video #%s' % video_id
168         uploader = clean_html(get_element_by_id('fbPhotoPageAuthorName', webpage))
169
170         return {
171             'id': video_id,
172             'title': video_title,
173             'formats': formats,
174             'duration': int_or_none(video_data.get('video_duration')),
175             'thumbnail': video_data.get('thumbnail_src'),
176             'uploader': uploader,
177         }