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.

202 lines
7.0 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
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
10 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_urllib_parse,
  6. compat_urllib_request,
  7. )
  8. from ..utils import (
  9. ExtractorError,
  10. sanitized_Request,
  11. )
  12. class UdemyIE(InfoExtractor):
  13. IE_NAME = 'udemy'
  14. _VALID_URL = r'https?://www\.udemy\.com/(?:[^#]+#/lecture/|lecture/view/?\?lectureId=)(?P<id>\d+)'
  15. _LOGIN_URL = 'https://www.udemy.com/join/login-popup/?displayType=ajax&showSkipButton=1'
  16. _ORIGIN_URL = 'https://www.udemy.com'
  17. _NETRC_MACHINE = 'udemy'
  18. _TESTS = [{
  19. 'url': 'https://www.udemy.com/java-tutorial/#/lecture/172757',
  20. 'md5': '98eda5b657e752cf945d8445e261b5c5',
  21. 'info_dict': {
  22. 'id': '160614',
  23. 'ext': 'mp4',
  24. 'title': 'Introduction and Installation',
  25. 'description': 'md5:c0d51f6f21ef4ec65f091055a5eef876',
  26. 'duration': 579.29,
  27. },
  28. 'skip': 'Requires udemy account credentials',
  29. }]
  30. def _handle_error(self, response):
  31. if not isinstance(response, dict):
  32. return
  33. error = response.get('error')
  34. if error:
  35. error_str = 'Udemy returned error #%s: %s' % (error.get('code'), error.get('message'))
  36. error_data = error.get('data')
  37. if error_data:
  38. error_str += ' - %s' % error_data.get('formErrors')
  39. raise ExtractorError(error_str, expected=True)
  40. def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'):
  41. headers = {
  42. 'X-Udemy-Snail-Case': 'true',
  43. 'X-Requested-With': 'XMLHttpRequest',
  44. }
  45. for cookie in self._downloader.cookiejar:
  46. if cookie.name == 'client_id':
  47. headers['X-Udemy-Client-Id'] = cookie.value
  48. elif cookie.name == 'access_token':
  49. headers['X-Udemy-Bearer-Token'] = cookie.value
  50. if isinstance(url_or_request, compat_urllib_request.Request):
  51. for header, value in headers.items():
  52. url_or_request.add_header(header, value)
  53. else:
  54. url_or_request = sanitized_Request(url_or_request, headers=headers)
  55. response = super(UdemyIE, self)._download_json(url_or_request, video_id, note)
  56. self._handle_error(response)
  57. return response
  58. def _real_initialize(self):
  59. self._login()
  60. def _login(self):
  61. (username, password) = self._get_login_info()
  62. if username is None:
  63. self.raise_login_required('Udemy account is required')
  64. login_popup = self._download_webpage(
  65. self._LOGIN_URL, None, 'Downloading login popup')
  66. def is_logged(webpage):
  67. return any(p in webpage for p in ['href="https://www.udemy.com/user/logout/', '>Logout<'])
  68. # already logged in
  69. if is_logged(login_popup):
  70. return
  71. login_form = self._form_hidden_inputs('login-form', login_popup)
  72. login_form.update({
  73. 'email': username.encode('utf-8'),
  74. 'password': password.encode('utf-8'),
  75. })
  76. request = sanitized_Request(
  77. self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8'))
  78. request.add_header('Referer', self._ORIGIN_URL)
  79. request.add_header('Origin', self._ORIGIN_URL)
  80. response = self._download_webpage(
  81. request, None, 'Logging in as %s' % username)
  82. if not is_logged(response):
  83. error = self._html_search_regex(
  84. r'(?s)<div[^>]+class="form-errors[^"]*">(.+?)</div>',
  85. response, 'error message', default=None)
  86. if error:
  87. raise ExtractorError('Unable to login: %s' % error, expected=True)
  88. raise ExtractorError('Unable to log in')
  89. def _real_extract(self, url):
  90. lecture_id = self._match_id(url)
  91. lecture = self._download_json(
  92. 'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id,
  93. lecture_id, 'Downloading lecture JSON')
  94. asset_type = lecture.get('assetType') or lecture.get('asset_type')
  95. if asset_type != 'Video':
  96. raise ExtractorError(
  97. 'Lecture %s is not a video' % lecture_id, expected=True)
  98. asset = lecture['asset']
  99. stream_url = asset.get('streamUrl') or asset.get('stream_url')
  100. mobj = re.search(r'(https?://www\.youtube\.com/watch\?v=.*)', stream_url)
  101. if mobj:
  102. return self.url_result(mobj.group(1), 'Youtube')
  103. video_id = asset['id']
  104. thumbnail = asset.get('thumbnailUrl') or asset.get('thumbnail_url')
  105. duration = asset['data']['duration']
  106. download_url = asset.get('downloadUrl') or asset.get('download_url')
  107. video = download_url.get('Video') or download_url.get('video')
  108. video_480p = download_url.get('Video480p') or download_url.get('video_480p')
  109. formats = [
  110. {
  111. 'url': video_480p[0],
  112. 'format_id': '360p',
  113. },
  114. {
  115. 'url': video[0],
  116. 'format_id': '720p',
  117. },
  118. ]
  119. title = lecture['title']
  120. description = lecture['description']
  121. return {
  122. 'id': video_id,
  123. 'title': title,
  124. 'description': description,
  125. 'thumbnail': thumbnail,
  126. 'duration': duration,
  127. 'formats': formats
  128. }
  129. class UdemyCourseIE(UdemyIE):
  130. IE_NAME = 'udemy:course'
  131. _VALID_URL = r'https?://www\.udemy\.com/(?P<coursepath>[\da-z-]+)'
  132. _SUCCESSFULLY_ENROLLED = '>You have enrolled in this course!<'
  133. _ALREADY_ENROLLED = '>You are already taking this course.<'
  134. _TESTS = []
  135. @classmethod
  136. def suitable(cls, url):
  137. return False if UdemyIE.suitable(url) else super(UdemyCourseIE, cls).suitable(url)
  138. def _real_extract(self, url):
  139. mobj = re.match(self._VALID_URL, url)
  140. course_path = mobj.group('coursepath')
  141. response = self._download_json(
  142. 'https://www.udemy.com/api-1.1/courses/%s' % course_path,
  143. course_path, 'Downloading course JSON')
  144. course_id = int(response['id'])
  145. course_title = response['title']
  146. webpage = self._download_webpage(
  147. 'https://www.udemy.com/course/subscribe/?courseId=%s' % course_id,
  148. course_id, 'Enrolling in the course')
  149. if self._SUCCESSFULLY_ENROLLED in webpage:
  150. self.to_screen('%s: Successfully enrolled in' % course_id)
  151. elif self._ALREADY_ENROLLED in webpage:
  152. self.to_screen('%s: Already enrolled in' % course_id)
  153. response = self._download_json(
  154. 'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id,
  155. course_id, 'Downloading course curriculum')
  156. entries = [
  157. self.url_result(
  158. 'https://www.udemy.com/%s/#/lecture/%s' % (course_path, asset['id']), 'Udemy')
  159. for asset in response if asset.get('assetType') or asset.get('asset_type') == 'Video'
  160. ]
  161. return self.playlist_result(entries, course_id, course_title)