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.

263 lines
9.4 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 groot_sessionid cookies in Set-Cookie header
  56. # and expects first one to be actually set
  57. self._apply_first_set_cookie_header(urlh, 'groot_sessionid')
  58. _, urlh = self._download_webpage_handle(
  59. auth.get('redirect_uri') or next_uri, None, 'Completing login',)
  60. if is_logged(urlh):
  61. self.LOGGED_IN = True
  62. return
  63. raise ExtractorError('Unable to log in')
  64. class SafariIE(SafariBaseIE):
  65. IE_NAME = 'safari'
  66. IE_DESC = 'safaribooksonline.com online video'
  67. _VALID_URL = r'''(?x)
  68. https?://
  69. (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
  70. (?:
  71. library/view/[^/]+/(?P<course_id>[^/]+)/(?P<part>[^/?\#&]+)\.html|
  72. videos/[^/]+/[^/]+/(?P<reference_id>[^-]+-[^/?\#&]+)
  73. )
  74. '''
  75. _TESTS = [{
  76. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
  77. 'md5': 'dcc5a425e79f2564148652616af1f2a3',
  78. 'info_dict': {
  79. 'id': '0_qbqx90ic',
  80. 'ext': 'mp4',
  81. 'title': 'Introduction to Hadoop Fundamentals LiveLessons',
  82. 'timestamp': 1437758058,
  83. 'upload_date': '20150724',
  84. 'uploader_id': 'stork',
  85. },
  86. }, {
  87. # non-digits in course id
  88. 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
  89. 'only_matching': True,
  90. }, {
  91. 'url': 'https://www.safaribooksonline.com/library/view/learning-path-red/9780134664057/RHCE_Introduction.html',
  92. 'only_matching': True,
  93. }, {
  94. 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314/9780134217314-PYMC_13_00',
  95. 'only_matching': True,
  96. }, {
  97. 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838/9780133392838-00_SeriesIntro',
  98. 'only_matching': True,
  99. }, {
  100. 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/00_SeriesIntro.html',
  101. 'only_matching': True,
  102. }]
  103. _PARTNER_ID = '1926081'
  104. _UICONF_ID = '29375172'
  105. def _real_extract(self, url):
  106. mobj = re.match(self._VALID_URL, url)
  107. reference_id = mobj.group('reference_id')
  108. if reference_id:
  109. video_id = reference_id
  110. partner_id = self._PARTNER_ID
  111. ui_id = self._UICONF_ID
  112. else:
  113. video_id = '%s-%s' % (mobj.group('course_id'), mobj.group('part'))
  114. webpage, urlh = self._download_webpage_handle(url, video_id)
  115. mobj = re.match(self._VALID_URL, urlh.geturl())
  116. reference_id = mobj.group('reference_id')
  117. if not reference_id:
  118. reference_id = self._search_regex(
  119. r'data-reference-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
  120. webpage, 'kaltura reference id', group='id')
  121. partner_id = self._search_regex(
  122. r'data-partner-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
  123. webpage, 'kaltura widget id', default=self._PARTNER_ID,
  124. group='id')
  125. ui_id = self._search_regex(
  126. r'data-ui-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
  127. webpage, 'kaltura uiconf id', default=self._UICONF_ID,
  128. group='id')
  129. query = {
  130. 'wid': '_%s' % partner_id,
  131. 'uiconf_id': ui_id,
  132. 'flashvars[referenceId]': reference_id,
  133. }
  134. if self.LOGGED_IN:
  135. kaltura_session = self._download_json(
  136. '%s/player/kaltura_session/?reference_id=%s' % (self._API_BASE, reference_id),
  137. video_id, 'Downloading kaltura session JSON',
  138. 'Unable to download kaltura session JSON', fatal=False)
  139. if kaltura_session:
  140. session = kaltura_session.get('session')
  141. if session:
  142. query['flashvars[ks]'] = session
  143. return self.url_result(update_url_query(
  144. 'https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', query),
  145. 'Kaltura')
  146. class SafariApiIE(SafariBaseIE):
  147. IE_NAME = 'safari:api'
  148. _VALID_URL = r'https?://(?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/api/v1/book/(?P<course_id>[^/]+)/chapter(?:-content)?/(?P<part>[^/?#&]+)\.html'
  149. _TESTS = [{
  150. 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
  151. 'only_matching': True,
  152. }, {
  153. 'url': 'https://www.safaribooksonline.com/api/v1/book/9780134664057/chapter/RHCE_Introduction.html',
  154. 'only_matching': True,
  155. }]
  156. def _real_extract(self, url):
  157. mobj = re.match(self._VALID_URL, url)
  158. part = self._download_json(
  159. url, '%s/%s' % (mobj.group('course_id'), mobj.group('part')),
  160. 'Downloading part JSON')
  161. return self.url_result(part['web_url'], SafariIE.ie_key())
  162. class SafariCourseIE(SafariBaseIE):
  163. IE_NAME = 'safari:course'
  164. IE_DESC = 'safaribooksonline.com online courses'
  165. _VALID_URL = r'''(?x)
  166. https?://
  167. (?:
  168. (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
  169. (?:
  170. library/view/[^/]+|
  171. api/v1/book|
  172. videos/[^/]+
  173. )|
  174. techbus\.safaribooksonline\.com
  175. )
  176. /(?P<id>[^/]+)
  177. '''
  178. _TESTS = [{
  179. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
  180. 'info_dict': {
  181. 'id': '9780133392838',
  182. 'title': 'Hadoop Fundamentals LiveLessons',
  183. },
  184. 'playlist_count': 22,
  185. 'skip': 'Requires safaribooksonline account credentials',
  186. }, {
  187. 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
  188. 'only_matching': True,
  189. }, {
  190. 'url': 'http://techbus.safaribooksonline.com/9780134426365',
  191. 'only_matching': True,
  192. }, {
  193. 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314',
  194. 'only_matching': True,
  195. }, {
  196. 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838',
  197. 'only_matching': True,
  198. }, {
  199. 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
  200. 'only_matching': True,
  201. }]
  202. @classmethod
  203. def suitable(cls, url):
  204. return (False if SafariIE.suitable(url) or SafariApiIE.suitable(url)
  205. else super(SafariCourseIE, cls).suitable(url))
  206. def _real_extract(self, url):
  207. course_id = self._match_id(url)
  208. course_json = self._download_json(
  209. '%s/book/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
  210. course_id, 'Downloading course JSON')
  211. if 'chapters' not in course_json:
  212. raise ExtractorError(
  213. 'No chapters found for course %s' % course_id, expected=True)
  214. entries = [
  215. self.url_result(chapter, SafariApiIE.ie_key())
  216. for chapter in course_json['chapters']]
  217. course_title = course_json['title']
  218. return self.playlist_result(entries, course_id, course_title)