safari.py 8.6 KB

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