safari.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. self._login()
  23. def _login(self):
  24. # We only need to log in once for courses or individual videos
  25. if self.LOGGED_IN:
  26. return
  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. SafariBaseIE.LOGGED_IN = True
  56. self.to_screen('Login successful')
  57. class SafariIE(SafariBaseIE):
  58. IE_NAME = 'safari'
  59. IE_DESC = 'safaribooksonline.com online video'
  60. _VALID_URL = r'''(?x)https?://
  61. (?:www\.)?safaribooksonline\.com/
  62. (?:
  63. library/view/[^/]+|
  64. api/v1/book
  65. )/
  66. (?P<course_id>[^/]+)/
  67. (?:chapter(?:-content)?/)?
  68. (?P<part>part\d+)\.html
  69. '''
  70. _TESTS = [{
  71. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
  72. 'md5': 'dcc5a425e79f2564148652616af1f2a3',
  73. 'info_dict': {
  74. 'id': '0_qbqx90ic',
  75. 'ext': 'mp4',
  76. 'title': 'Introduction to Hadoop Fundamentals LiveLessons',
  77. 'timestamp': 1437758058,
  78. 'upload_date': '20150724',
  79. 'uploader_id': 'stork',
  80. },
  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(url, '%s/%s' % (course_id, part))
  94. reference_id = self._search_regex(r'data-reference-id="([^"]+)"', webpage, 'kaltura reference id')
  95. partner_id = self._search_regex(r'data-partner-id="([^"]+)"', webpage, 'kaltura widget id')
  96. ui_id = self._search_regex(r'data-ui-id="([^"]+)"', webpage, 'kaltura uiconf id')
  97. query = {
  98. 'wid': '_%s' % partner_id,
  99. 'uiconf_id': ui_id,
  100. 'flashvars[referenceId]': reference_id,
  101. }
  102. if self.LOGGED_IN:
  103. kaltura_session = self._download_json(
  104. '%s/player/kaltura_session/?reference_id=%s' % (self._API_BASE, reference_id),
  105. course_id, 'Downloading kaltura session JSON',
  106. 'Unable to download kaltura session JSON', fatal=False)
  107. if kaltura_session:
  108. session = kaltura_session.get('session')
  109. if session:
  110. query['flashvars[ks]'] = session
  111. return self.url_result(update_url_query(
  112. 'https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', query),
  113. 'Kaltura')
  114. class SafariCourseIE(SafariBaseIE):
  115. IE_NAME = 'safari:course'
  116. IE_DESC = 'safaribooksonline.com online courses'
  117. _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)/(?P<id>[^/]+)/?(?:[#?]|$)'
  118. _TESTS = [{
  119. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
  120. 'info_dict': {
  121. 'id': '9780133392838',
  122. 'title': 'Hadoop Fundamentals LiveLessons',
  123. },
  124. 'playlist_count': 22,
  125. 'skip': 'Requires safaribooksonline account credentials',
  126. }, {
  127. 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
  128. 'only_matching': True,
  129. }]
  130. def _real_extract(self, url):
  131. course_id = self._match_id(url)
  132. course_json = self._download_json(
  133. '%s/book/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
  134. course_id, 'Downloading course JSON')
  135. if 'chapters' not in course_json:
  136. raise ExtractorError(
  137. 'No chapters found for course %s' % course_id, expected=True)
  138. entries = [
  139. self.url_result(chapter, 'Safari')
  140. for chapter in course_json['chapters']]
  141. course_title = course_json['title']
  142. return self.playlist_result(entries, course_id, course_title)