safari.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from .brightcove import BrightcoveLegacyIE
  6. from ..utils import (
  7. ExtractorError,
  8. sanitized_Request,
  9. smuggle_url,
  10. std_headers,
  11. urlencode_postdata,
  12. )
  13. class SafariBaseIE(InfoExtractor):
  14. _LOGIN_URL = 'https://www.safaribooksonline.com/accounts/login/'
  15. _SUCCESSFUL_LOGIN_REGEX = r'<a href="/accounts/logout/"[^>]*>Sign Out</a>'
  16. _NETRC_MACHINE = 'safari'
  17. _API_BASE = 'https://www.safaribooksonline.com/api/v1/book'
  18. _API_FORMAT = 'json'
  19. LOGGED_IN = False
  20. def _real_initialize(self):
  21. # We only need to log in once for courses or individual videos
  22. if not self.LOGGED_IN:
  23. self._login()
  24. SafariBaseIE.LOGGED_IN = True
  25. def _login(self):
  26. (username, password) = self._get_login_info()
  27. if username is None:
  28. self.raise_login_required('safaribooksonline.com account is required')
  29. headers = std_headers.copy()
  30. if 'Referer' not in headers:
  31. headers['Referer'] = self._LOGIN_URL
  32. login_page_request = sanitized_Request(self._LOGIN_URL, headers=headers)
  33. login_page = self._download_webpage(
  34. login_page_request, None,
  35. 'Downloading login form')
  36. csrf = self._html_search_regex(
  37. r"name='csrfmiddlewaretoken'\s+value='([^']+)'",
  38. login_page, 'csrf token')
  39. login_form = {
  40. 'csrfmiddlewaretoken': csrf,
  41. 'email': username,
  42. 'password1': password,
  43. 'login': 'Sign In',
  44. 'next': '',
  45. }
  46. request = sanitized_Request(
  47. self._LOGIN_URL, urlencode_postdata(login_form), headers=headers)
  48. login_page = self._download_webpage(
  49. request, None, 'Logging in as %s' % username)
  50. if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None:
  51. raise ExtractorError(
  52. 'Login failed; make sure your credentials are correct and try again.',
  53. expected=True)
  54. self.to_screen('Login successful')
  55. class SafariIE(SafariBaseIE):
  56. IE_NAME = 'safari'
  57. IE_DESC = 'safaribooksonline.com online video'
  58. _VALID_URL = r'''(?x)https?://
  59. (?:www\.)?safaribooksonline\.com/
  60. (?:
  61. library/view/[^/]+|
  62. api/v1/book
  63. )/
  64. (?P<course_id>[^/]+)/
  65. (?:chapter(?:-content)?/)?
  66. (?P<part>part\d+)\.html
  67. '''
  68. _TESTS = [{
  69. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
  70. 'md5': '5b0c4cc1b3c1ba15dda7344085aa5592',
  71. 'info_dict': {
  72. 'id': '2842601850001',
  73. 'ext': 'mp4',
  74. 'title': 'Introduction',
  75. },
  76. 'skip': 'Requires safaribooksonline account credentials',
  77. }, {
  78. 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
  79. 'only_matching': True,
  80. }, {
  81. # non-digits in course id
  82. 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/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 = BrightcoveLegacyIE._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}), 'BrightcoveLegacy')
  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>[^/]+)/?(?:[#?]|$)'
  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)