safari.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/book'
  19. _API_FORMAT = 'json'
  20. LOGGED_IN = False
  21. def _real_initialize(self):
  22. return
  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.copy()
  32. if 'Referer' not in headers:
  33. headers['Referer'] = self._LOGIN_URL
  34. login_page_request = sanitized_Request(self._LOGIN_URL, headers=headers)
  35. login_page = self._download_webpage(
  36. login_page_request, None,
  37. 'Downloading login form')
  38. csrf = self._html_search_regex(
  39. r"name='csrfmiddlewaretoken'\s+value='([^']+)'",
  40. login_page, 'csrf token')
  41. login_form = {
  42. 'csrfmiddlewaretoken': csrf,
  43. 'email': username,
  44. 'password1': password,
  45. 'login': 'Sign In',
  46. 'next': '',
  47. }
  48. request = sanitized_Request(
  49. self._LOGIN_URL, urlencode_postdata(login_form), headers=headers)
  50. login_page = self._download_webpage(
  51. request, None, 'Logging in as %s' % username)
  52. if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None:
  53. raise ExtractorError(
  54. 'Login failed; make sure your credentials are correct and try again.',
  55. expected=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. return self.url_result(update_url_query('https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', {
  98. 'wid': '_%s' % partner_id,
  99. 'uiconf_id': ui_id,
  100. 'flashvars[referenceId]': reference_id,
  101. }), 'Kaltura')
  102. class SafariCourseIE(SafariBaseIE):
  103. IE_NAME = 'safari:course'
  104. IE_DESC = 'safaribooksonline.com online courses'
  105. _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)/(?P<id>[^/]+)/?(?:[#?]|$)'
  106. _TESTS = [{
  107. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
  108. 'info_dict': {
  109. 'id': '9780133392838',
  110. 'title': 'Hadoop Fundamentals LiveLessons',
  111. },
  112. 'playlist_count': 22,
  113. 'skip': 'Requires safaribooksonline account credentials',
  114. }, {
  115. 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
  116. 'only_matching': True,
  117. }]
  118. def _real_extract(self, url):
  119. course_id = self._match_id(url)
  120. course_json = self._download_json(
  121. '%s/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
  122. course_id, 'Downloading course JSON')
  123. if 'chapters' not in course_json:
  124. raise ExtractorError(
  125. 'No chapters found for course %s' % course_id, expected=True)
  126. entries = [
  127. self.url_result(chapter, 'Safari')
  128. for chapter in course_json['chapters']]
  129. course_title = course_json['title']
  130. return self.playlist_result(entries, course_id, course_title)