logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

safari.py (9746B)


  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_urlparse,
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. update_url_query,
  13. )
  14. class SafariBaseIE(InfoExtractor):
  15. _LOGIN_URL = 'https://learning.oreilly.com/accounts/login/'
  16. _NETRC_MACHINE = 'safari'
  17. _API_BASE = 'https://learning.oreilly.com/api/v1'
  18. _API_FORMAT = 'json'
  19. LOGGED_IN = False
  20. def _real_initialize(self):
  21. self._login()
  22. def _login(self):
  23. username, password = self._get_login_info()
  24. if username is None:
  25. return
  26. _, urlh = self._download_webpage_handle(
  27. 'https://learning.oreilly.com/accounts/login-check/', None,
  28. 'Downloading login page')
  29. def is_logged(urlh):
  30. return 'learning.oreilly.com/home/' in urlh.geturl()
  31. if is_logged(urlh):
  32. self.LOGGED_IN = True
  33. return
  34. redirect_url = urlh.geturl()
  35. parsed_url = compat_urlparse.urlparse(redirect_url)
  36. qs = compat_parse_qs(parsed_url.query)
  37. next_uri = compat_urlparse.urljoin(
  38. 'https://api.oreilly.com', qs['next'][0])
  39. auth, urlh = self._download_json_handle(
  40. 'https://www.oreilly.com/member/auth/login/', None, 'Logging in',
  41. data=json.dumps({
  42. 'email': username,
  43. 'password': password,
  44. 'redirect_uri': next_uri,
  45. }).encode(), headers={
  46. 'Content-Type': 'application/json',
  47. 'Referer': redirect_url,
  48. }, expected_status=400)
  49. credentials = auth.get('credentials')
  50. if (not auth.get('logged_in') and not auth.get('redirect_uri')
  51. and credentials):
  52. raise ExtractorError(
  53. 'Unable to login: %s' % credentials, expected=True)
  54. # oreilly serves two same instances of the following cookies
  55. # in Set-Cookie header and expects first one to be actually set
  56. for cookie in ('groot_sessionid', 'orm-jwt', 'orm-rt'):
  57. self._apply_first_set_cookie_header(urlh, cookie)
  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. headers={'Accept': 'application/json'})
  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)