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.

158 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. _NETRC_MACHINE = 'safari'
  19. _API_BASE = 'https://www.safaribooksonline.com/api/v1/book'
  20. _API_FORMAT = 'json'
  21. LOGGED_IN = False
  22. def _real_initialize(self):
  23. # We only need to log in once for courses or individual videos
  24. if not self.LOGGED_IN:
  25. self._login()
  26. SafariBaseIE.LOGGED_IN = True
  27. def _login(self):
  28. (username, password) = self._get_login_info()
  29. if username is None:
  30. self.raise_login_required('safaribooksonline.com account is required')
  31. headers = std_headers
  32. if 'Referer' not in headers:
  33. headers['Referer'] = self._LOGIN_URL
  34. login_page = self._download_webpage(
  35. self._LOGIN_URL, None,
  36. 'Downloading login form')
  37. csrf = self._html_search_regex(
  38. r"name='csrfmiddlewaretoken'\s+value='([^']+)'",
  39. login_page, 'csrf token')
  40. login_form = {
  41. 'csrfmiddlewaretoken': csrf,
  42. 'email': username,
  43. 'password1': password,
  44. 'login': 'Sign In',
  45. 'next': '',
  46. }
  47. request = compat_urllib_request.Request(
  48. self._LOGIN_URL, compat_urllib_parse.urlencode(login_form), headers=headers)
  49. login_page = self._download_webpage(
  50. request, None, 'Logging in as %s' % username)
  51. if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None:
  52. raise ExtractorError(
  53. 'Login failed; make sure your credentials are correct and try again.',
  54. expected=True)
  55. self.to_screen('Login successful')
  56. class SafariIE(SafariBaseIE):
  57. IE_NAME = 'safari'
  58. IE_DESC = 'safaribooksonline.com online video'
  59. _VALID_URL = r'''(?x)https?://
  60. (?:www\.)?safaribooksonline\.com/
  61. (?:
  62. library/view/[^/]+|
  63. api/v1/book
  64. )/
  65. (?P<course_id>[^/]+)/
  66. (?:chapter(?:-content)?/)?
  67. (?P<part>part\d+)\.html
  68. '''
  69. _TESTS = [{
  70. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
  71. 'md5': '5b0c4cc1b3c1ba15dda7344085aa5592',
  72. 'info_dict': {
  73. 'id': '2842601850001',
  74. 'ext': 'mp4',
  75. 'title': 'Introduction',
  76. },
  77. 'skip': 'Requires safaribooksonline account credentials',
  78. }, {
  79. 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
  80. 'only_matching': True,
  81. }, {
  82. # non-digits in course id
  83. 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
  84. 'only_matching': True,
  85. }]
  86. def _real_extract(self, url):
  87. mobj = re.match(self._VALID_URL, url)
  88. course_id = mobj.group('course_id')
  89. part = mobj.group('part')
  90. webpage = self._download_webpage(
  91. '%s/%s/chapter-content/%s.html' % (self._API_BASE, course_id, part),
  92. part)
  93. bc_url = BrightcoveIE._extract_brightcove_url(webpage)
  94. if not bc_url:
  95. raise ExtractorError('Could not extract Brightcove URL from %s' % url, expected=True)
  96. return self.url_result(smuggle_url(bc_url, {'Referer': url}), 'Brightcove')
  97. class SafariCourseIE(SafariBaseIE):
  98. IE_NAME = 'safari:course'
  99. IE_DESC = 'safaribooksonline.com online courses'
  100. _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)/(?P<id>[^/]+)/?(?:[#?]|$)'
  101. _TESTS = [{
  102. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
  103. 'info_dict': {
  104. 'id': '9780133392838',
  105. 'title': 'Hadoop Fundamentals LiveLessons',
  106. },
  107. 'playlist_count': 22,
  108. 'skip': 'Requires safaribooksonline account credentials',
  109. }, {
  110. 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
  111. 'only_matching': True,
  112. }]
  113. def _real_extract(self, url):
  114. course_id = self._match_id(url)
  115. course_json = self._download_json(
  116. '%s/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
  117. course_id, 'Downloading course JSON')
  118. if 'chapters' not in course_json:
  119. raise ExtractorError(
  120. 'No chapters found for course %s' % course_id, expected=True)
  121. entries = [
  122. self.url_result(chapter, 'Safari')
  123. for chapter in course_json['chapters']]
  124. course_title = course_json['title']
  125. return self.playlist_result(entries, course_id, course_title)