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.

161 lines
5.4 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>[^/]+)/
  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. # non-digits in course id
  86. 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
  87. 'only_matching': True,
  88. }]
  89. def _real_extract(self, url):
  90. mobj = re.match(self._VALID_URL, url)
  91. course_id = mobj.group('course_id')
  92. part = mobj.group('part')
  93. webpage = self._download_webpage(
  94. '%s/%s/chapter-content/%s.html' % (self._API_BASE, course_id, part),
  95. part)
  96. bc_url = BrightcoveIE._extract_brightcove_url(webpage)
  97. if not bc_url:
  98. raise ExtractorError('Could not extract Brightcove URL from %s' % url, expected=True)
  99. return self.url_result(smuggle_url(bc_url, {'Referer': url}), 'Brightcove')
  100. class SafariCourseIE(SafariBaseIE):
  101. IE_NAME = 'safari:course'
  102. IE_DESC = 'safaribooksonline.com online courses'
  103. _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)/(?P<id>[^/]+)/?(?:[#?]|$)'
  104. _TESTS = [{
  105. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
  106. 'info_dict': {
  107. 'id': '9780133392838',
  108. 'title': 'Hadoop Fundamentals LiveLessons',
  109. },
  110. 'playlist_count': 22,
  111. 'skip': 'Requires safaribooksonline account credentials',
  112. }, {
  113. 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
  114. 'only_matching': True,
  115. }]
  116. def _real_extract(self, url):
  117. course_id = self._match_id(url)
  118. course_json = self._download_json(
  119. '%s/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
  120. course_id, 'Downloading course JSON')
  121. if 'chapters' not in course_json:
  122. raise ExtractorError(
  123. 'No chapters found for course %s' % course_id, expected=True)
  124. entries = [
  125. self.url_result(chapter, 'Safari')
  126. for chapter in course_json['chapters']]
  127. course_title = course_json['title']
  128. return self.playlist_result(entries, course_id, course_title)