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