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.

132 lines
5.3 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. 'h': self._search_regex(r'name="h" value="(\w*?)"', login_results, 'h'),
  71. 'name_action_selected': 'dont_save',
  72. }
  73. check_req = compat_urllib_request.Request(self._CHECKPOINT_URL, urlencode_postdata(check_form))
  74. check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  75. check_response = self._download_webpage(check_req, None,
  76. note='Confirming login')
  77. if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
  78. self._downloader.report_warning('Unable to confirm login, you have to login in your brower and authorize the login.')
  79. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  80. self._downloader.report_warning('unable to log in: %s' % compat_str(err))
  81. return
  82. def _real_initialize(self):
  83. self._login()
  84. def _real_extract(self, url):
  85. mobj = re.match(self._VALID_URL, url)
  86. video_id = mobj.group('id')
  87. url = 'https://www.facebook.com/video/video.php?v=%s' % video_id
  88. webpage = self._download_webpage(url, video_id)
  89. BEFORE = '{swf.addParam(param[0], param[1]);});\n'
  90. AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
  91. m = re.search(re.escape(BEFORE) + '(.*?)' + re.escape(AFTER), webpage)
  92. if not m:
  93. m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
  94. if m_msg is not None:
  95. raise ExtractorError(
  96. 'The video is not available, Facebook said: "%s"' % m_msg.group(1),
  97. expected=True)
  98. else:
  99. raise ExtractorError('Cannot parse data')
  100. data = dict(json.loads(m.group(1)))
  101. params_raw = compat_urllib_parse.unquote(data['params'])
  102. params = json.loads(params_raw)
  103. video_data = params['video_data'][0]
  104. video_url = video_data.get('hd_src')
  105. if not video_url:
  106. video_url = video_data['sd_src']
  107. if not video_url:
  108. raise ExtractorError('Cannot find video URL')
  109. video_title = self._html_search_regex(
  110. r'<h2 class="uiHeaderTitle">([^<]*)</h2>', webpage, 'title')
  111. return {
  112. 'id': video_id,
  113. 'title': video_title,
  114. 'url': video_url,
  115. 'duration': int(video_data['video_duration']),
  116. 'thumbnail': video_data['thumbnail_src'],
  117. }