]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/facebook.py
debian/NEWS: Update with notes about split audio/video downloads.
[youtubedl] / youtube_dl / extractor / facebook.py
1 import json
2 import netrc
3 import re
4 import socket
5
6 from .common import InfoExtractor
7 from ..utils import (
8 compat_http_client,
9 compat_str,
10 compat_urllib_error,
11 compat_urllib_parse,
12 compat_urllib_request,
13
14 ExtractorError,
15 )
16
17
18 class FacebookIE(InfoExtractor):
19 """Information Extractor for Facebook"""
20
21 _VALID_URL = r'^(?:https?://)?(?:\w+\.)?facebook\.com/(?:video/video|photo)\.php\?(?:.*?)v=(?P<ID>\d+)(?:.*)'
22 _LOGIN_URL = 'https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php&'
23 _NETRC_MACHINE = 'facebook'
24 IE_NAME = u'facebook'
25 _TEST = {
26 u'url': u'https://www.facebook.com/photo.php?v=120708114770723',
27 u'file': u'120708114770723.mp4',
28 u'md5': u'48975a41ccc4b7a581abd68651c1a5a8',
29 u'info_dict': {
30 u"duration": 279,
31 u"title": u"PEOPLE ARE AWESOME 2013"
32 }
33 }
34
35 def report_login(self):
36 """Report attempt to log in."""
37 self.to_screen(u'Logging in')
38
39 def _real_initialize(self):
40 if self._downloader is None:
41 return
42
43 useremail = None
44 password = None
45 downloader_params = self._downloader.params
46
47 # Attempt to use provided username and password or .netrc data
48 if downloader_params.get('username', None) is not None:
49 useremail = downloader_params['username']
50 password = downloader_params['password']
51 elif downloader_params.get('usenetrc', False):
52 try:
53 info = netrc.netrc().authenticators(self._NETRC_MACHINE)
54 if info is not None:
55 useremail = info[0]
56 password = info[2]
57 else:
58 raise netrc.NetrcParseError('No authenticators for %s' % self._NETRC_MACHINE)
59 except (IOError, netrc.NetrcParseError) as err:
60 self._downloader.report_warning(u'parsing .netrc: %s' % compat_str(err))
61 return
62
63 if useremail is None:
64 return
65
66 # Log in
67 login_form = {
68 'email': useremail,
69 'pass': password,
70 'login': 'Log+In'
71 }
72 request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(login_form))
73 try:
74 self.report_login()
75 login_results = compat_urllib_request.urlopen(request).read()
76 if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
77 self._downloader.report_warning(u'unable to log in: bad username/password, or exceded login rate limit (~3/min). Check credentials or wait.')
78 return
79 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
80 self._downloader.report_warning(u'unable to log in: %s' % compat_str(err))
81 return
82
83 def _real_extract(self, url):
84 mobj = re.match(self._VALID_URL, url)
85 if mobj is None:
86 raise ExtractorError(u'Invalid URL: %s' % url)
87 video_id = mobj.group('ID')
88
89 url = 'https://www.facebook.com/video/video.php?v=%s' % video_id
90 webpage = self._download_webpage(url, video_id)
91
92 BEFORE = '{swf.addParam(param[0], param[1]);});\n'
93 AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
94 m = re.search(re.escape(BEFORE) + '(.*?)' + re.escape(AFTER), webpage)
95 if not m:
96 raise ExtractorError(u'Cannot parse data')
97 data = dict(json.loads(m.group(1)))
98 params_raw = compat_urllib_parse.unquote(data['params'])
99 params = json.loads(params_raw)
100 video_data = params['video_data'][0]
101 video_url = video_data.get('hd_src')
102 if not video_url:
103 video_url = video_data['sd_src']
104 if not video_url:
105 raise ExtractorError(u'Cannot find video URL')
106 video_duration = int(video_data['video_duration'])
107 thumbnail = video_data['thumbnail_src']
108
109 video_title = self._html_search_regex(
110 r'<h2 class="uiHeaderTitle">([^<]*)</h2>', webpage, u'title')
111
112 info = {
113 'id': video_id,
114 'title': video_title,
115 'url': video_url,
116 'ext': 'mp4',
117 'duration': video_duration,
118 'thumbnail': thumbnail,
119 }
120 return [info]