]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/facebook.py
   6 from .common 
import InfoExtractor
 
  12     compat_urllib_request
, 
  18 class FacebookIE(InfoExtractor
): 
  19     """Information Extractor for Facebook""" 
  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' 
  26     def report_login(self
): 
  27         """Report attempt to log in.""" 
  28         self
.to_screen(u
'Logging in') 
  30     def _real_initialize(self
): 
  31         if self
._downloader 
is None: 
  36         downloader_params 
= self
._downloader
.params
 
  38         # Attempt to use provided username and password or .netrc data 
  39         if downloader_params
.get('username', None) is not None: 
  40             useremail 
= downloader_params
['username'] 
  41             password 
= downloader_params
['password'] 
  42         elif downloader_params
.get('usenetrc', False): 
  44                 info 
= netrc
.netrc().authenticators(self
._NETRC
_MACHINE
) 
  49                     raise netrc
.NetrcParseError('No authenticators for %s' % self
._NETRC
_MACHINE
) 
  50             except (IOError, netrc
.NetrcParseError
) as err
: 
  51                 self
._downloader
.report_warning(u
'parsing .netrc: %s' % compat_str(err
)) 
  63         request 
= compat_urllib_request
.Request(self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(login_form
)) 
  66             login_results 
= compat_urllib_request
.urlopen(request
).read() 
  67             if re
.search(r
'<form(.*)name="login"(.*)</form>', login_results
) is not None: 
  68                 self
._downloader
.report_warning(u
'unable to log in: bad username/password, or exceded login rate limit (~3/min). Check credentials or wait.') 
  70         except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
: 
  71             self
._downloader
.report_warning(u
'unable to log in: %s' % compat_str(err
)) 
  74     def _real_extract(self
, url
): 
  75         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  77             raise ExtractorError(u
'Invalid URL: %s' % url
) 
  78         video_id 
= mobj
.group('ID') 
  80         url 
= 'https://www.facebook.com/video/video.php?v=%s' % video_id
 
  81         webpage 
= self
._download
_webpage
(url
, video_id
) 
  83         BEFORE 
= '{swf.addParam(param[0], param[1]);});\n' 
  84         AFTER 
= '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});' 
  85         m 
= re
.search(re
.escape(BEFORE
) + '(.*?)' + re
.escape(AFTER
), webpage
) 
  87             raise ExtractorError(u
'Cannot parse data') 
  88         data 
= dict(json
.loads(m
.group(1))) 
  89         params_raw 
= compat_urllib_parse
.unquote(data
['params']) 
  90         params 
= json
.loads(params_raw
) 
  91         video_data 
= params
['video_data'][0] 
  92         video_url 
= video_data
.get('hd_src') 
  94             video_url 
= video_data
['sd_src'] 
  96             raise ExtractorError(u
'Cannot find video URL') 
  97         video_duration 
= int(video_data
['video_duration']) 
  98         thumbnail 
= video_data
['thumbnail_src'] 
 100         video_title 
= self
._html
_search
_regex
('<h2 class="uiHeaderTitle">([^<]+)</h2>', 
 105             'title': video_title
, 
 108             'duration': video_duration
, 
 109             'thumbnail': thumbnail
,