safari.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. update_url_query,
  13. )
  14. class SafariBaseIE(InfoExtractor):
  15. _LOGIN_URL = 'https://www.safaribooksonline.com/accounts/login/'
  16. _SUCCESSFUL_LOGIN_REGEX = r'<a href="/accounts/logout/"[^>]*>Sign Out</a>'
  17. _NETRC_MACHINE = 'safari'
  18. _API_BASE = 'https://www.safaribooksonline.com/api/v1'
  19. _API_FORMAT = 'json'
  20. LOGGED_IN = False
  21. def _real_initialize(self):
  22. # We only need to log in once for courses or individual videos
  23. if not self.LOGGED_IN:
  24. self._login()
  25. SafariBaseIE.LOGGED_IN = True
  26. def _login(self):
  27. (username, password) = self._get_login_info()
  28. if username is None:
  29. return
  30. headers = std_headers.copy()
  31. if 'Referer' not in headers:
  32. headers['Referer'] = self._LOGIN_URL
  33. login_page_request = sanitized_Request(self._LOGIN_URL, headers=headers)
  34. login_page = self._download_webpage(
  35. login_page_request, 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 = sanitized_Request(
  48. self._LOGIN_URL, urlencode_postdata(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': 'dcc5a425e79f2564148652616af1f2a3',
  72. 'info_dict': {
  73. 'id': '0_qbqx90ic',
  74. 'ext': 'mp4',
  75. 'title': 'Introduction to Hadoop Fundamentals LiveLessons',
  76. 'timestamp': 1437758058,
  77. 'upload_date': '20150724',
  78. 'uploader_id': 'stork',
  79. },
  80. }, {
  81. 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
  82. 'only_matching': True,
  83. }, {
  84. # non-digits in course id
  85. 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
  86. 'only_matching': True,
  87. }]
  88. def _real_extract(self, url):
  89. mobj = re.match(self._VALID_URL, url)
  90. course_id = mobj.group('course_id')
  91. part = mobj.group('part')
  92. webpage = self._download_webpage(url, '%s/%s' % (course_id, part))
  93. reference_id = self._search_regex(r'data-reference-id="([^"]+)"', webpage, 'kaltura reference id')
  94. partner_id = self._search_regex(r'data-partner-id="([^"]+)"', webpage, 'kaltura widget id')
  95. ui_id = self._search_regex(r'data-ui-id="([^"]+)"', webpage, 'kaltura uiconf id')
  96. query = {
  97. 'wid': '_%s' % partner_id,
  98. 'uiconf_id': ui_id,
  99. 'flashvars[referenceId]': reference_id,
  100. }
  101. if self.LOGGED_IN:
  102. kaltura_session = self._download_json(
  103. '%s/player/kaltura_session/?reference_id=%s' % (self._API_BASE, reference_id),
  104. course_id, 'Downloading kaltura session JSON',
  105. 'Unable to download kaltura session JSON', fatal=False)
  106. if kaltura_session:
  107. session = kaltura_session.get('session')
  108. if session:
  109. query['flashvars[ks]'] = session
  110. return self.url_result(update_url_query(
  111. 'https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', query),
  112. 'Kaltura')
  113. class SafariCourseIE(SafariBaseIE):
  114. IE_NAME = 'safari:course'
  115. IE_DESC = 'safaribooksonline.com online courses'
  116. _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)/(?P<id>[^/]+)/?(?:[#?]|$)'
  117. _TESTS = [{
  118. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
  119. 'info_dict': {
  120. 'id': '9780133392838',
  121. 'title': 'Hadoop Fundamentals LiveLessons',
  122. },
  123. 'playlist_count': 22,
  124. 'skip': 'Requires safaribooksonline account credentials',
  125. }, {
  126. 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
  127. 'only_matching': True,
  128. }]
  129. def _real_extract(self, url):
  130. course_id = self._match_id(url)
  131. course_json = self._download_json(
  132. '%s/book/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
  133. course_id, 'Downloading course JSON')
  134. if 'chapters' not in course_json:
  135. raise ExtractorError(
  136. 'No chapters found for course %s' % course_id, expected=True)
  137. entries = [
  138. self.url_result(chapter, 'Safari')
  139. for chapter in course_json['chapters']]
  140. course_title = course_json['title']
  141. return self.playlist_result(entries, course_id, course_title)