You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
5.4 KiB

  1. from __future__ import unicode_literals
  2. import json
  3. import re
  4. import socket
  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. urlencode_postdata,
  13. ExtractorError,
  14. )
  15. class FacebookIE(InfoExtractor):
  16. _VALID_URL = r'''(?x)
  17. https?://(?:\w+\.)?facebook\.com/
  18. (?:[^#?]*\#!/)?
  19. (?:video/video\.php|photo\.php|video/embed)\?(?:.*?)
  20. (?:v|video_id)=(?P<id>[0-9]+)
  21. (?:.*)'''
  22. _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1'
  23. _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1'
  24. _NETRC_MACHINE = 'facebook'
  25. IE_NAME = 'facebook'
  26. _TEST = {
  27. 'url': 'https://www.facebook.com/photo.php?v=120708114770723',
  28. 'md5': '48975a41ccc4b7a581abd68651c1a5a8',
  29. 'info_dict': {
  30. 'id': '120708114770723',
  31. 'ext': 'mp4',
  32. 'duration': 279,
  33. 'title': 'PEOPLE ARE AWESOME 2013',
  34. }
  35. }
  36. def _login(self):
  37. (useremail, password) = self._get_login_info()
  38. if useremail is None:
  39. return
  40. login_page_req = compat_urllib_request.Request(self._LOGIN_URL)
  41. login_page_req.add_header('Cookie', 'locale=en_US')
  42. login_page = self._download_webpage(login_page_req, None,
  43. note='Downloading login page',
  44. errnote='Unable to download login page')
  45. lsd = self._search_regex(
  46. r'<input type="hidden" name="lsd" value="([^"]*)"',
  47. login_page, 'lsd')
  48. lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd')
  49. login_form = {
  50. 'email': useremail,
  51. 'pass': password,
  52. 'lsd': lsd,
  53. 'lgnrnd': lgnrnd,
  54. 'next': 'http://facebook.com/home.php',
  55. 'default_persistent': '0',
  56. 'legacy_return': '1',
  57. 'timezone': '-60',
  58. 'trynum': '1',
  59. }
  60. request = compat_urllib_request.Request(self._LOGIN_URL, urlencode_postdata(login_form))
  61. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  62. try:
  63. login_results = self._download_webpage(request, None,
  64. note='Logging in', errnote='unable to fetch login page')
  65. if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
  66. self._downloader.report_warning('unable to log in: bad username/password, or exceded login rate limit (~3/min). Check credentials or wait.')
  67. return
  68. check_form = {
  69. 'fb_dtsg': self._search_regex(r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg'),
  70. 'nh': self._search_regex(r'name="nh" value="(\w*?)"', login_results, 'nh'),
  71. 'name_action_selected': 'dont_save',
  72. 'submit[Continue]': self._search_regex(r'<button[^>]+value="(.*?)"[^>]+name="submit\[Continue\]"', login_results, 'continue'),
  73. }
  74. check_req = compat_urllib_request.Request(self._CHECKPOINT_URL, urlencode_postdata(check_form))
  75. check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  76. check_response = self._download_webpage(check_req, None,
  77. note='Confirming login')
  78. if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
  79. self._downloader.report_warning('Unable to confirm login, you have to login in your brower and authorize the login.')
  80. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  81. self._downloader.report_warning('unable to log in: %s' % compat_str(err))
  82. return
  83. def _real_initialize(self):
  84. self._login()
  85. def _real_extract(self, url):
  86. mobj = re.match(self._VALID_URL, url)
  87. video_id = mobj.group('id')
  88. url = 'https://www.facebook.com/video/video.php?v=%s' % video_id
  89. webpage = self._download_webpage(url, video_id)
  90. BEFORE = '{swf.addParam(param[0], param[1]);});\n'
  91. AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
  92. m = re.search(re.escape(BEFORE) + '(.*?)' + re.escape(AFTER), webpage)
  93. if not m:
  94. m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
  95. if m_msg is not None:
  96. raise ExtractorError(
  97. 'The video is not available, Facebook said: "%s"' % m_msg.group(1),
  98. expected=True)
  99. else:
  100. raise ExtractorError('Cannot parse data')
  101. data = dict(json.loads(m.group(1)))
  102. params_raw = compat_urllib_parse.unquote(data['params'])
  103. params = json.loads(params_raw)
  104. video_data = params['video_data'][0]
  105. video_url = video_data.get('hd_src')
  106. if not video_url:
  107. video_url = video_data['sd_src']
  108. if not video_url:
  109. raise ExtractorError('Cannot find video URL')
  110. video_title = self._html_search_regex(
  111. r'<h2 class="uiHeaderTitle">([^<]*)</h2>', webpage, 'title')
  112. return {
  113. 'id': video_id,
  114. 'title': video_title,
  115. 'url': video_url,
  116. 'duration': int(video_data['video_duration']),
  117. 'thumbnail': video_data['thumbnail_src'],
  118. }