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.

240 lines
8.2 KiB

Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
  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://www.safaribooksonline.com/accounts/login/'
  14. _NETRC_MACHINE = 'safari'
  15. _API_BASE = 'https://www.safaribooksonline.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\.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. _PARTNER_ID = '1926081'
  88. _UICONF_ID = '29375172'
  89. def _real_extract(self, url):
  90. mobj = re.match(self._VALID_URL, url)
  91. reference_id = mobj.group('reference_id')
  92. if reference_id:
  93. video_id = reference_id
  94. partner_id = self._PARTNER_ID
  95. ui_id = self._UICONF_ID
  96. else:
  97. video_id = '%s-%s' % (mobj.group('course_id'), mobj.group('part'))
  98. webpage, urlh = self._download_webpage_handle(url, video_id)
  99. mobj = re.match(self._VALID_URL, urlh.geturl())
  100. reference_id = mobj.group('reference_id')
  101. if not reference_id:
  102. reference_id = self._search_regex(
  103. r'data-reference-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
  104. webpage, 'kaltura reference id', group='id')
  105. partner_id = self._search_regex(
  106. r'data-partner-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
  107. webpage, 'kaltura widget id', default=self._PARTNER_ID,
  108. group='id')
  109. ui_id = self._search_regex(
  110. r'data-ui-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
  111. webpage, 'kaltura uiconf id', default=self._UICONF_ID,
  112. group='id')
  113. query = {
  114. 'wid': '_%s' % partner_id,
  115. 'uiconf_id': ui_id,
  116. 'flashvars[referenceId]': reference_id,
  117. }
  118. if self.LOGGED_IN:
  119. kaltura_session = self._download_json(
  120. '%s/player/kaltura_session/?reference_id=%s' % (self._API_BASE, reference_id),
  121. video_id, 'Downloading kaltura session JSON',
  122. 'Unable to download kaltura session JSON', fatal=False)
  123. if kaltura_session:
  124. session = kaltura_session.get('session')
  125. if session:
  126. query['flashvars[ks]'] = session
  127. return self.url_result(update_url_query(
  128. 'https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', query),
  129. 'Kaltura')
  130. class SafariApiIE(SafariBaseIE):
  131. IE_NAME = 'safari:api'
  132. _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/api/v1/book/(?P<course_id>[^/]+)/chapter(?:-content)?/(?P<part>[^/?#&]+)\.html'
  133. _TESTS = [{
  134. 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
  135. 'only_matching': True,
  136. }, {
  137. 'url': 'https://www.safaribooksonline.com/api/v1/book/9780134664057/chapter/RHCE_Introduction.html',
  138. 'only_matching': True,
  139. }]
  140. def _real_extract(self, url):
  141. mobj = re.match(self._VALID_URL, url)
  142. part = self._download_json(
  143. url, '%s/%s' % (mobj.group('course_id'), mobj.group('part')),
  144. 'Downloading part JSON')
  145. return self.url_result(part['web_url'], SafariIE.ie_key())
  146. class SafariCourseIE(SafariBaseIE):
  147. IE_NAME = 'safari:course'
  148. IE_DESC = 'safaribooksonline.com online courses'
  149. _VALID_URL = r'''(?x)
  150. https?://
  151. (?:
  152. (?:www\.)?safaribooksonline\.com/
  153. (?:
  154. library/view/[^/]+|
  155. api/v1/book|
  156. videos/[^/]+
  157. )|
  158. techbus\.safaribooksonline\.com
  159. )
  160. /(?P<id>[^/]+)
  161. '''
  162. _TESTS = [{
  163. 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
  164. 'info_dict': {
  165. 'id': '9780133392838',
  166. 'title': 'Hadoop Fundamentals LiveLessons',
  167. },
  168. 'playlist_count': 22,
  169. 'skip': 'Requires safaribooksonline account credentials',
  170. }, {
  171. 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
  172. 'only_matching': True,
  173. }, {
  174. 'url': 'http://techbus.safaribooksonline.com/9780134426365',
  175. 'only_matching': True,
  176. }, {
  177. 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314',
  178. 'only_matching': True,
  179. }]
  180. @classmethod
  181. def suitable(cls, url):
  182. return (False if SafariIE.suitable(url) or SafariApiIE.suitable(url)
  183. else super(SafariCourseIE, cls).suitable(url))
  184. def _real_extract(self, url):
  185. course_id = self._match_id(url)
  186. course_json = self._download_json(
  187. '%s/book/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
  188. course_id, 'Downloading course JSON')
  189. if 'chapters' not in course_json:
  190. raise ExtractorError(
  191. 'No chapters found for course %s' % course_id, expected=True)
  192. entries = [
  193. self.url_result(chapter, SafariApiIE.ie_key())
  194. for chapter in course_json['chapters']]
  195. course_title = course_json['title']
  196. return self.playlist_result(entries, course_id, course_title)