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.

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