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.

264 lines
9.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_parse_qs,
  8. compat_str,
  9. compat_urlparse,
  10. )
  11. from ..utils import (
  12. ExtractorError,
  13. update_url_query,
  14. )
  15. class SafariBaseIE(InfoExtractor):
  16. _LOGIN_URL = 'https://learning.oreilly.com/accounts/login/'
  17. _NETRC_MACHINE = 'safari'
  18. _API_BASE = 'https://learning.oreilly.com/api/v1'
  19. _API_FORMAT = 'json'
  20. LOGGED_IN = False
  21. def _real_initialize(self):
  22. self._login()
  23. def _login(self):
  24. username, password = self._get_login_info()
  25. if username is None:
  26. return
  27. _, urlh = self._download_webpage_handle(
  28. 'https://learning.oreilly.com/accounts/login-check/', None,
  29. 'Downloading login page')
  30. def is_logged(urlh):
  31. return 'learning.oreilly.com/home/' in compat_str(urlh.geturl())
  32. if is_logged(urlh):
  33. self.LOGGED_IN = True
  34. return
  35. redirect_url = compat_str(urlh.geturl())
  36. parsed_url = compat_urlparse.urlparse(redirect_url)
  37. qs = compat_parse_qs(parsed_url.query)
  38. next_uri = compat_urlparse.urljoin(
  39. 'https://api.oreilly.com', qs['next'][0])
  40. auth, urlh = self._download_json_handle(
  41. 'https://www.oreilly.com/member/auth/login/', None, 'Logging in',
  42. data=json.dumps({
  43. 'email': username,
  44. 'password': password,
  45. 'redirect_uri': next_uri,
  46. }).encode(), headers={
  47. 'Content-Type': 'application/json',
  48. 'Referer': redirect_url,
  49. }, expected_status=400)
  50. credentials = auth.get('credentials')
  51. if (not auth.get('logged_in') and not auth.get('redirect_uri')
  52. and credentials):
  53. raise ExtractorError(
  54. 'Unable to login: %s' % credentials, expected=True)
  55. # oreilly serves two same instances of the following cookies
  56. # in Set-Cookie header and expects first one to be actually set
  57. for cookie in ('groot_sessionid', 'orm-jwt', 'orm-rt'):
  58. self._apply_first_set_cookie_header(urlh, cookie)
  59. _, urlh = self._download_webpage_handle(
  60. auth.get('redirect_uri') or next_uri, None, 'Completing login',)
  61. if is_logged(urlh):
  62. self.LOGGED_IN = True
  63. return
  64. raise ExtractorError('Unable to log in')
  65. class SafariIE(SafariBaseIE):
  66. IE_NAME = 'safari'
  67. IE_DESC = 'safaribooksonline.com online video'
  68. _VALID_URL = r'''(?x)
  69. https?://
  70. (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
  71. (?:
  72. library/view/[^/]+/(?P<course_id>[^/]+)/(?P<part>[^/?\#&]+)\.html|
  73. videos/[^/]+/[^/]+/(?P<reference_id>[^-]+-[^/?\#&]+)
  74. )
  75. '''
  76. _TESTS = [{
  77. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
  78. 'md5': 'dcc5a425e79f2564148652616af1f2a3',
  79. 'info_dict': {
  80. 'id': '0_qbqx90ic',
  81. 'ext': 'mp4',
  82. 'title': 'Introduction to Hadoop Fundamentals LiveLessons',
  83. 'timestamp': 1437758058,
  84. 'upload_date': '20150724',
  85. 'uploader_id': 'stork',
  86. },
  87. }, {
  88. # non-digits in course id
  89. 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
  90. 'only_matching': True,
  91. }, {
  92. 'url': 'https://www.safaribooksonline.com/library/view/learning-path-red/9780134664057/RHCE_Introduction.html',
  93. 'only_matching': True,
  94. }, {
  95. 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314/9780134217314-PYMC_13_00',
  96. 'only_matching': True,
  97. }, {
  98. 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838/9780133392838-00_SeriesIntro',
  99. 'only_matching': True,
  100. }, {
  101. 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/00_SeriesIntro.html',
  102. 'only_matching': True,
  103. }]
  104. _PARTNER_ID = '1926081'
  105. _UICONF_ID = '29375172'
  106. def _real_extract(self, url):
  107. mobj = re.match(self._VALID_URL, url)
  108. reference_id = mobj.group('reference_id')
  109. if reference_id:
  110. video_id = reference_id
  111. partner_id = self._PARTNER_ID
  112. ui_id = self._UICONF_ID
  113. else:
  114. video_id = '%s-%s' % (mobj.group('course_id'), mobj.group('part'))
  115. webpage, urlh = self._download_webpage_handle(url, video_id)
  116. mobj = re.match(self._VALID_URL, urlh.geturl())
  117. reference_id = mobj.group('reference_id')
  118. if not reference_id:
  119. reference_id = self._search_regex(
  120. r'data-reference-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
  121. webpage, 'kaltura reference id', group='id')
  122. partner_id = self._search_regex(
  123. r'data-partner-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
  124. webpage, 'kaltura widget id', default=self._PARTNER_ID,
  125. group='id')
  126. ui_id = self._search_regex(
  127. r'data-ui-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
  128. webpage, 'kaltura uiconf id', default=self._UICONF_ID,
  129. group='id')
  130. query = {
  131. 'wid': '_%s' % partner_id,
  132. 'uiconf_id': ui_id,
  133. 'flashvars[referenceId]': reference_id,
  134. }
  135. if self.LOGGED_IN:
  136. kaltura_session = self._download_json(
  137. '%s/player/kaltura_session/?reference_id=%s' % (self._API_BASE, reference_id),
  138. video_id, 'Downloading kaltura session JSON',
  139. 'Unable to download kaltura session JSON', fatal=False)
  140. if kaltura_session:
  141. session = kaltura_session.get('session')
  142. if session:
  143. query['flashvars[ks]'] = session
  144. return self.url_result(update_url_query(
  145. 'https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', query),
  146. 'Kaltura')
  147. class SafariApiIE(SafariBaseIE):
  148. IE_NAME = 'safari:api'
  149. _VALID_URL = r'https?://(?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/api/v1/book/(?P<course_id>[^/]+)/chapter(?:-content)?/(?P<part>[^/?#&]+)\.html'
  150. _TESTS = [{
  151. 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
  152. 'only_matching': True,
  153. }, {
  154. 'url': 'https://www.safaribooksonline.com/api/v1/book/9780134664057/chapter/RHCE_Introduction.html',
  155. 'only_matching': True,
  156. }]
  157. def _real_extract(self, url):
  158. mobj = re.match(self._VALID_URL, url)
  159. part = self._download_json(
  160. url, '%s/%s' % (mobj.group('course_id'), mobj.group('part')),
  161. 'Downloading part JSON')
  162. return self.url_result(part['web_url'], SafariIE.ie_key())
  163. class SafariCourseIE(SafariBaseIE):
  164. IE_NAME = 'safari:course'
  165. IE_DESC = 'safaribooksonline.com online courses'
  166. _VALID_URL = r'''(?x)
  167. https?://
  168. (?:
  169. (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
  170. (?:
  171. library/view/[^/]+|
  172. api/v1/book|
  173. videos/[^/]+
  174. )|
  175. techbus\.safaribooksonline\.com
  176. )
  177. /(?P<id>[^/]+)
  178. '''
  179. _TESTS = [{
  180. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
  181. 'info_dict': {
  182. 'id': '9780133392838',
  183. 'title': 'Hadoop Fundamentals LiveLessons',
  184. },
  185. 'playlist_count': 22,
  186. 'skip': 'Requires safaribooksonline account credentials',
  187. }, {
  188. 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
  189. 'only_matching': True,
  190. }, {
  191. 'url': 'http://techbus.safaribooksonline.com/9780134426365',
  192. 'only_matching': True,
  193. }, {
  194. 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314',
  195. 'only_matching': True,
  196. }, {
  197. 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838',
  198. 'only_matching': True,
  199. }, {
  200. 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
  201. 'only_matching': True,
  202. }]
  203. @classmethod
  204. def suitable(cls, url):
  205. return (False if SafariIE.suitable(url) or SafariApiIE.suitable(url)
  206. else super(SafariCourseIE, cls).suitable(url))
  207. def _real_extract(self, url):
  208. course_id = self._match_id(url)
  209. course_json = self._download_json(
  210. '%s/book/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
  211. course_id, 'Downloading course JSON')
  212. if 'chapters' not in course_json:
  213. raise ExtractorError(
  214. 'No chapters found for course %s' % course_id, expected=True)
  215. entries = [
  216. self.url_result(chapter, SafariApiIE.ie_key())
  217. for chapter in course_json['chapters']]
  218. course_title = course_json['title']
  219. return self.playlist_result(entries, course_id, course_title)