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.

157 lines
5.2 KiB

  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from .brightcove import BrightcoveIE
  6. from ..compat import (
  7. compat_urllib_parse,
  8. compat_urllib_request,
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. smuggle_url,
  13. std_headers,
  14. )
  15. class SafariBaseIE(InfoExtractor):
  16. _LOGIN_URL = 'https://www.safaribooksonline.com/accounts/login/'
  17. _SUCCESSFUL_LOGIN_REGEX = r'<a href="/accounts/logout/"[^>]*>Sign Out</a>'
  18. _ACCOUNT_CREDENTIALS_HINT = 'Use --username and --password options to supply credentials for safaribooksonline.com'
  19. _NETRC_MACHINE = 'safari'
  20. _API_BASE = 'https://www.safaribooksonline.com/api/v1/book'
  21. _API_FORMAT = 'json'
  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 self.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"name='csrfmiddlewaretoken'\s+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(
  56. 'Login failed; make sure your credentials are correct and try again.',
  57. expected=True)
  58. self.to_screen('Login successful')
  59. class SafariIE(SafariBaseIE):
  60. IE_NAME = 'safari'
  61. IE_DESC = 'safaribooksonline.com online video'
  62. _VALID_URL = r'''(?x)https?://
  63. (?:www\.)?safaribooksonline\.com/
  64. (?:
  65. library/view/[^/]+|
  66. api/v1/book
  67. )/
  68. (?P<course_id>\d+)/
  69. (?:chapter(?:-content)?/)?
  70. (?P<part>part\d+)\.html
  71. '''
  72. _TESTS = [{
  73. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
  74. 'md5': '5b0c4cc1b3c1ba15dda7344085aa5592',
  75. 'info_dict': {
  76. 'id': '2842601850001',
  77. 'ext': 'mp4',
  78. 'title': 'Introduction',
  79. },
  80. 'skip': 'Requires safaribooksonline account credentials',
  81. }, {
  82. 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
  83. 'only_matching': True,
  84. }]
  85. def _real_extract(self, url):
  86. mobj = re.match(self._VALID_URL, url)
  87. course_id = mobj.group('course_id')
  88. part = mobj.group('part')
  89. webpage = self._download_webpage(
  90. '%s/%s/chapter-content/%s.html' % (self._API_BASE, course_id, part),
  91. part)
  92. bc_url = BrightcoveIE._extract_brightcove_url(webpage)
  93. if not bc_url:
  94. raise ExtractorError('Could not extract Brightcove URL from %s' % url, expected=True)
  95. return self.url_result(smuggle_url(bc_url, {'Referer': url}), 'Brightcove')
  96. class SafariCourseIE(SafariBaseIE):
  97. IE_NAME = 'safari:course'
  98. IE_DESC = 'safaribooksonline.com online courses'
  99. _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)/(?P<id>\d+)/?(?:[#?]|$)'
  100. _TESTS = [{
  101. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
  102. 'info_dict': {
  103. 'id': '9780133392838',
  104. 'title': 'Hadoop Fundamentals LiveLessons',
  105. },
  106. 'playlist_count': 22,
  107. 'skip': 'Requires safaribooksonline account credentials',
  108. }, {
  109. 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
  110. 'only_matching': True,
  111. }]
  112. def _real_extract(self, url):
  113. course_id = self._match_id(url)
  114. course_json = self._download_json(
  115. '%s/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
  116. course_id, 'Downloading course JSON')
  117. if 'chapters' not in course_json:
  118. raise ExtractorError(
  119. 'No chapters found for course %s' % course_id, expected=True)
  120. entries = [
  121. self.url_result(chapter, 'Safari')
  122. for chapter in course_json['chapters']]
  123. course_title = course_json['title']
  124. return self.playlist_result(entries, course_id, course_title)