safari.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. from .common import InfoExtractor
  6. from .brightcove import BrightcoveIE
  7. from ..compat import (
  8. compat_urllib_parse,
  9. compat_urllib_request,
  10. )
  11. from ..utils import (
  12. ExtractorError,
  13. smuggle_url,
  14. std_headers,
  15. )
  16. class SafariBaseIE(InfoExtractor):
  17. _LOGIN_URL = 'https://www.safaribooksonline.com/accounts/login/'
  18. _SUCCESSFUL_LOGIN_REGEX = r'<a href="/accounts/logout/"[^>]+>Sign Out</a>'
  19. _ACCOUNT_CREDENTIALS_HINT = ('Use --username and --password options to '
  20. 'supply credentials for safaribooksonline.com ')
  21. _NETRC_MACHINE = 'safaribooksonline'
  22. LOGGED_IN = False
  23. def _real_initialize(self):
  24. # We only need to log in once for courses or individual videos
  25. if not SafariBaseIE.LOGGED_IN:
  26. self._login()
  27. SafariBaseIE.LOGGED_IN = True
  28. def _login(self):
  29. (username, password) = self._get_login_info()
  30. if username is None:
  31. raise ExtractorError(
  32. self._ACCOUNT_CREDENTIALS_HINT,
  33. expected=True)
  34. headers = std_headers
  35. if 'Referer' not in headers:
  36. headers['Referer'] = self._LOGIN_URL
  37. login_page = self._download_webpage(
  38. self._LOGIN_URL, None,
  39. 'Downloading login form')
  40. csrf = self._html_search_regex(
  41. r"<input +type='hidden' +name='csrfmiddlewaretoken' +value='([^']+)' +/>",
  42. login_page, 'csrf token')
  43. login_form = {
  44. 'csrfmiddlewaretoken': csrf,
  45. 'email': username,
  46. 'password1': password,
  47. 'login': 'Sign In',
  48. 'next': '',
  49. }
  50. request = compat_urllib_request.Request(
  51. self._LOGIN_URL, compat_urllib_parse.urlencode(login_form), headers=headers)
  52. login_page = self._download_webpage(
  53. request, None, 'Logging in as %s' % username)
  54. if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None:
  55. raise ExtractorError('Login failed; make sure your credentials are correct and '
  56. 'try again.', expected=True)
  57. self.to_screen('Login successful')
  58. class SafariIE(SafariBaseIE):
  59. IE_NAME = 'safari'
  60. IE_DESC = 'safaribooksonline.com online video'
  61. _VALID_URL = (r'https?://(?:www\.)?safaribooksonline\.com/library/view/[^/]+/'
  62. '(?P<id>\d+)/(?P<part>part\d+)\.html')
  63. _TEST = {
  64. 'url': ('https://www.safaribooksonline.com/library/view/'
  65. 'hadoop-fundamentals-livelessons/9780133392838/part00.html'),
  66. 'md5': '5b0c4cc1b3c1ba15dda7344085aa5592',
  67. 'info_dict': {
  68. 'id': '9780133392838',
  69. 'ext': 'mp4',
  70. 'title': 'Introduction',
  71. }
  72. }
  73. def _real_extract(self, url):
  74. mobj = re.match(self._VALID_URL, url)
  75. part = mobj.group('part')
  76. webpage = self._download_webpage(url, part)
  77. bc_url = BrightcoveIE._extract_brightcove_url(webpage)
  78. if not bc_url:
  79. raise ExtractorError('Could not extract Brightcove URL from %s' % url, expected=True)
  80. return {
  81. '_type': 'url',
  82. 'url': smuggle_url(bc_url, {'Referer': url}),
  83. 'ie_key': 'Brightcove'
  84. }
  85. class SafariCourseIE(SafariBaseIE):
  86. IE_NAME = 'safari:course'
  87. IE_DESC = 'safaribooksonline.com online courses'
  88. _VALID_URL = (r'https?://(?:www\.)?safaribooksonline\.com/library/view/'
  89. '(?P<course_path>[^/]+)/(?P<id>\d+)/?$')
  90. _API_BASE = 'https://www.safaribooksonline.com/api/v1/book'
  91. _API_FORMAT = 'json'
  92. def _real_extract(self, url):
  93. mobj = re.match(self._VALID_URL, url)
  94. course_path = mobj.group('course_path')
  95. course_id = mobj.group('id')
  96. webpage = self._download_webpage(
  97. '%s/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
  98. course_path, 'Downloading course JSON')
  99. course_json = json.loads(webpage)
  100. if 'chapters' not in course_json:
  101. raise ExtractorError('No chapters found for course %s' % course_id, expected=True)
  102. num_parts = len(course_json['chapters'])
  103. parts = ['%02d' % part for part in range(num_parts)]
  104. entries = [
  105. self.url_result(
  106. 'https://www.safaribooksonline.com/library/view/%s/%s/part%s.html' % (course_path,
  107. course_id,
  108. part_id),
  109. 'Safari')
  110. for part_id in parts]
  111. course_title = course_json['title']
  112. return self.playlist_result(entries, course_id, course_title)