]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/facebook.py
1 from __future__
import unicode_literals
7 from .common
import InfoExtractor
13 compat_urllib_request
,
23 class FacebookIE(InfoExtractor
):
25 https?://(?:\w+\.)?facebook\.com/
28 (?:video/video\.php|photo\.php|video\.php|video/embed)\?(?:.*?)
30 [^/]+/videos/(?:[^/]+/)?
34 _LOGIN_URL
= 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1'
35 _CHECKPOINT_URL
= 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1'
36 _NETRC_MACHINE
= 'facebook'
39 'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf',
40 'md5': '6a40d33c0eccbb1af76cf0485a052659',
42 'id': '637842556329505',
44 'title': 're:Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam',
47 'note': 'Video without discernible title',
48 'url': 'https://www.facebook.com/video.php?v=274175099429670',
50 'id': '274175099429670',
52 'title': 'Facebook video #274175099429670',
55 'url': 'https://www.facebook.com/video.php?v=10204634152394104',
56 'only_matching': True,
58 'url': 'https://www.facebook.com/amogood/videos/1618742068337349/?fref=nf',
59 'only_matching': True,
61 'url': 'https://www.facebook.com/ChristyClarkForBC/videos/vb.22819070941/10153870694020942/?type=2&theater',
62 'only_matching': True,
66 (useremail
, password
) = self
._get
_login
_info
()
70 login_page_req
= compat_urllib_request
.Request(self
._LOGIN
_URL
)
71 login_page_req
.add_header('Cookie', 'locale=en_US')
72 login_page
= self
._download
_webpage
(login_page_req
, None,
73 note
='Downloading login page',
74 errnote
='Unable to download login page')
75 lsd
= self
._search
_regex
(
76 r
'<input type="hidden" name="lsd" value="([^"]*)"',
78 lgnrnd
= self
._search
_regex
(r
'name="lgnrnd" value="([^"]*?)"', login_page
, 'lgnrnd')
85 'next': 'http://facebook.com/home.php',
86 'default_persistent': '0',
91 request
= compat_urllib_request
.Request(self
._LOGIN
_URL
, urlencode_postdata(login_form
))
92 request
.add_header('Content-Type', 'application/x-www-form-urlencoded')
94 login_results
= self
._download
_webpage
(request
, None,
95 note
='Logging in', errnote
='unable to fetch login page')
96 if re
.search(r
'<form(.*)name="login"(.*)</form>', login_results
) is not None:
97 self
._downloader
.report_warning('unable to log in: bad username/password, or exceded login rate limit (~3/min). Check credentials or wait.')
101 'fb_dtsg': self
._search
_regex
(r
'name="fb_dtsg" value="(.+?)"', login_results
, 'fb_dtsg'),
102 'h': self
._search
_regex
(
103 r
'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results
, 'h'),
104 'name_action_selected': 'dont_save',
106 check_req
= compat_urllib_request
.Request(self
._CHECKPOINT
_URL
, urlencode_postdata(check_form
))
107 check_req
.add_header('Content-Type', 'application/x-www-form-urlencoded')
108 check_response
= self
._download
_webpage
(check_req
, None,
109 note
='Confirming login')
110 if re
.search(r
'id="checkpointSubmitButton"', check_response
) is not None:
111 self
._downloader
.report_warning('Unable to confirm login, you have to login in your brower and authorize the login.')
112 except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
:
113 self
._downloader
.report_warning('unable to log in: %s' % compat_str(err
))
116 def _real_initialize(self
):
119 def _real_extract(self
, url
):
120 video_id
= self
._match
_id
(url
)
121 url
= 'https://www.facebook.com/video/video.php?v=%s' % video_id
122 webpage
= self
._download
_webpage
(url
, video_id
)
124 BEFORE
= '{swf.addParam(param[0], param[1]);});\n'
125 AFTER
= '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
126 m
= re
.search(re
.escape(BEFORE
) + '(.*?)' + re
.escape(AFTER
), webpage
)
128 m_msg
= re
.search(r
'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage
)
129 if m_msg
is not None:
130 raise ExtractorError(
131 'The video is not available, Facebook said: "%s"' % m_msg
.group(1),
134 raise ExtractorError('Cannot parse data')
135 data
= dict(json
.loads(m
.group(1)))
136 params_raw
= compat_urllib_parse
.unquote(data
['params'])
137 params
= json
.loads(params_raw
)
138 video_data
= params
['video_data'][0]
141 for quality
in ['sd', 'hd']:
142 src
= video_data
.get('%s_src' % quality
)
145 'format_id': quality
,
149 raise ExtractorError('Cannot find video formats')
151 video_title
= self
._html
_search
_regex
(
152 r
'<h2 class="uiHeaderTitle">([^<]*)</h2>', webpage
, 'title',
155 video_title
= self
._html
_search
_regex
(
156 r
'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>',
157 webpage
, 'alternative title', default
=None)
158 video_title
= limit_length(video_title
, 80)
160 video_title
= 'Facebook video #%s' % video_id
164 'title': video_title
,
166 'duration': int_or_none(video_data
.get('video_duration')),
167 'thumbnail': video_data
.get('thumbnail_src'),