]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/facebook.py
Update changelog.
[youtubedl] / youtube_dl / extractor / facebook.py
1 import json
2 import re
3 import socket
4
5 from .common import InfoExtractor
6 from ..utils import (
7 compat_http_client,
8 compat_str,
9 compat_urllib_error,
10 compat_urllib_parse,
11 compat_urllib_request,
12
13 ExtractorError,
14 )
15
16
17 class FacebookIE(InfoExtractor):
18 """Information Extractor for Facebook"""
19
20 _VALID_URL = r'''(?x)
21 (?:https?://)?(?:\w+\.)?facebook\.com/
22 (?:[^#?]*\#!/)?
23 (?:video/video\.php|photo\.php|video/embed)\?(?:.*?)
24 (?:v|video_id)=(?P<id>[0-9]+)
25 (?:.*)'''
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'
29 IE_NAME = u'facebook'
30 _TEST = {
31 u'url': u'https://www.facebook.com/photo.php?v=120708114770723',
32 u'file': u'120708114770723.mp4',
33 u'md5': u'48975a41ccc4b7a581abd68651c1a5a8',
34 u'info_dict': {
35 u"duration": 279,
36 u"title": u"PEOPLE ARE AWESOME 2013"
37 }
38 }
39
40 def report_login(self):
41 """Report attempt to log in."""
42 self.to_screen(u'Logging in')
43
44 def _login(self):
45 (useremail, password) = self._get_login_info()
46 if useremail is None:
47 return
48
49 login_page_req = compat_urllib_request.Request(self._LOGIN_URL)
50 login_page_req.add_header('Cookie', 'locale=en_US')
51 self.report_login()
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')
56
57 login_form = {
58 'email': useremail,
59 'pass': password,
60 'lsd': lsd,
61 'lgnrnd': lgnrnd,
62 'next': 'http://facebook.com/home.php',
63 'default_persistent': '0',
64 'legacy_return': '1',
65 'timezone': '-60',
66 'trynum': '1',
67 }
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')
70 try:
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.')
74 return
75
76 check_form = {
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'),
81 }
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))
89 return
90
91 def _real_initialize(self):
92 self._login()
93
94 def _real_extract(self, url):
95 mobj = re.match(self._VALID_URL, url)
96 if mobj is None:
97 raise ExtractorError(u'Invalid URL: %s' % url)
98 video_id = mobj.group('id')
99
100 url = 'https://www.facebook.com/video/video.php?v=%s' % video_id
101 webpage = self._download_webpage(url, video_id)
102
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)
106 if not m:
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),
111 expected=True)
112 else:
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')
119 if not video_url:
120 video_url = video_data['sd_src']
121 if not video_url:
122 raise ExtractorError(u'Cannot find video URL')
123 video_duration = int(video_data['video_duration'])
124 thumbnail = video_data['thumbnail_src']
125
126 video_title = self._html_search_regex(
127 r'<h2 class="uiHeaderTitle">([^<]*)</h2>', webpage, u'title')
128
129 info = {
130 'id': video_id,
131 'title': video_title,
132 'url': video_url,
133 'ext': 'mp4',
134 'duration': video_duration,
135 'thumbnail': thumbnail,
136 }
137 return [info]