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.

250 lines
9.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
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
11 years ago
10 years ago
10 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. ExtractorError,
  8. clean_html,
  9. int_or_none,
  10. sanitized_Request,
  11. urlencode_postdata,
  12. )
  13. class LyndaBaseIE(InfoExtractor):
  14. _LOGIN_URL = 'https://www.lynda.com/login/login.aspx'
  15. _ACCOUNT_CREDENTIALS_HINT = 'Use --username and --password options to provide lynda.com account credentials.'
  16. _NETRC_MACHINE = 'lynda'
  17. def _real_initialize(self):
  18. self._login()
  19. def _login(self):
  20. username, password = self._get_login_info()
  21. if username is None:
  22. return
  23. login_form = {
  24. 'username': username,
  25. 'password': password,
  26. 'remember': 'false',
  27. 'stayPut': 'false'
  28. }
  29. request = sanitized_Request(
  30. self._LOGIN_URL, urlencode_postdata(login_form))
  31. login_page = self._download_webpage(
  32. request, None, 'Logging in as %s' % username)
  33. # Not (yet) logged in
  34. m = re.search(r'loginResultJson\s*=\s*\'(?P<json>[^\']+)\';', login_page)
  35. if m is not None:
  36. response = m.group('json')
  37. response_json = json.loads(response)
  38. state = response_json['state']
  39. if state == 'notlogged':
  40. raise ExtractorError(
  41. 'Unable to login, incorrect username and/or password',
  42. expected=True)
  43. # This is when we get popup:
  44. # > You're already logged in to lynda.com on two devices.
  45. # > If you log in here, we'll log you out of another device.
  46. # So, we need to confirm this.
  47. if state == 'conflicted':
  48. confirm_form = {
  49. 'username': '',
  50. 'password': '',
  51. 'resolve': 'true',
  52. 'remember': 'false',
  53. 'stayPut': 'false',
  54. }
  55. request = sanitized_Request(
  56. self._LOGIN_URL, urlencode_postdata(confirm_form))
  57. login_page = self._download_webpage(
  58. request, None,
  59. 'Confirming log in and log out from another device')
  60. if all(not re.search(p, login_page) for p in ('isLoggedIn\s*:\s*true', r'logout\.aspx', r'>Log out<')):
  61. if 'login error' in login_page:
  62. mobj = re.search(
  63. r'(?s)<h1[^>]+class="topmost">(?P<title>[^<]+)</h1>\s*<div>(?P<description>.+?)</div>',
  64. login_page)
  65. if mobj:
  66. raise ExtractorError(
  67. 'lynda returned error: %s - %s'
  68. % (mobj.group('title'), clean_html(mobj.group('description'))),
  69. expected=True)
  70. raise ExtractorError('Unable to log in')
  71. def _logout(self):
  72. username, _ = self._get_login_info()
  73. if username is None:
  74. return
  75. self._download_webpage(
  76. 'http://www.lynda.com/ajax/logout.aspx', None,
  77. 'Logging out', 'Unable to log out', fatal=False)
  78. class LyndaIE(LyndaBaseIE):
  79. IE_NAME = 'lynda'
  80. IE_DESC = 'lynda.com videos'
  81. _VALID_URL = r'https?://www\.lynda\.com/(?:[^/]+/[^/]+/\d+|player/embed)/(?P<id>\d+)'
  82. _NETRC_MACHINE = 'lynda'
  83. _TIMECODE_REGEX = r'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]'
  84. _TESTS = [{
  85. 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
  86. 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1',
  87. 'info_dict': {
  88. 'id': '114408',
  89. 'ext': 'mp4',
  90. 'title': 'Using the exercise files',
  91. 'duration': 68
  92. }
  93. }, {
  94. 'url': 'https://www.lynda.com/player/embed/133770?tr=foo=1;bar=g;fizz=rt&fs=0',
  95. 'only_matching': True,
  96. }]
  97. def _real_extract(self, url):
  98. video_id = self._match_id(url)
  99. video = self._download_json(
  100. 'http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id,
  101. video_id, 'Downloading video JSON')
  102. if 'Status' in video:
  103. raise ExtractorError(
  104. 'lynda returned error: %s' % video['Message'], expected=True)
  105. if video.get('HasAccess') is False:
  106. self.raise_login_required('Video %s is only available for members' % video_id)
  107. video_id = compat_str(video.get('ID') or video_id)
  108. duration = int_or_none(video.get('DurationInSeconds'))
  109. title = video['Title']
  110. formats = []
  111. fmts = video.get('Formats')
  112. if fmts:
  113. formats.extend([{
  114. 'url': f['Url'],
  115. 'ext': f.get('Extension'),
  116. 'width': int_or_none(f.get('Width')),
  117. 'height': int_or_none(f.get('Height')),
  118. 'filesize': int_or_none(f.get('FileSize')),
  119. 'format_id': compat_str(f.get('Resolution')) if f.get('Resolution') else None,
  120. } for f in fmts if f.get('Url')])
  121. prioritized_streams = video.get('PrioritizedStreams')
  122. if prioritized_streams:
  123. for prioritized_stream_id, prioritized_stream in prioritized_streams.items():
  124. formats.extend([{
  125. 'url': video_url,
  126. 'width': int_or_none(format_id),
  127. 'format_id': '%s-%s' % (prioritized_stream_id, format_id),
  128. } for format_id, video_url in prioritized_stream.items()])
  129. self._check_formats(formats, video_id)
  130. self._sort_formats(formats)
  131. subtitles = self.extract_subtitles(video_id)
  132. return {
  133. 'id': video_id,
  134. 'title': title,
  135. 'duration': duration,
  136. 'subtitles': subtitles,
  137. 'formats': formats
  138. }
  139. def _fix_subtitles(self, subs):
  140. srt = ''
  141. seq_counter = 0
  142. for pos in range(0, len(subs) - 1):
  143. seq_current = subs[pos]
  144. m_current = re.match(self._TIMECODE_REGEX, seq_current['Timecode'])
  145. if m_current is None:
  146. continue
  147. seq_next = subs[pos + 1]
  148. m_next = re.match(self._TIMECODE_REGEX, seq_next['Timecode'])
  149. if m_next is None:
  150. continue
  151. appear_time = m_current.group('timecode')
  152. disappear_time = m_next.group('timecode')
  153. text = seq_current['Caption'].strip()
  154. if text:
  155. seq_counter += 1
  156. srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (seq_counter, appear_time, disappear_time, text)
  157. if srt:
  158. return srt
  159. def _get_subtitles(self, video_id):
  160. url = 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id
  161. subs = self._download_json(url, None, False)
  162. if subs:
  163. return {'en': [{'ext': 'srt', 'data': self._fix_subtitles(subs)}]}
  164. else:
  165. return {}
  166. class LyndaCourseIE(LyndaBaseIE):
  167. IE_NAME = 'lynda:course'
  168. IE_DESC = 'lynda.com online courses'
  169. # Course link equals to welcome/introduction video link of same course
  170. # We will recognize it as course link
  171. _VALID_URL = r'https?://(?:www|m)\.lynda\.com/(?P<coursepath>[^/]+/[^/]+/(?P<courseid>\d+))-\d\.html'
  172. def _real_extract(self, url):
  173. mobj = re.match(self._VALID_URL, url)
  174. course_path = mobj.group('coursepath')
  175. course_id = mobj.group('courseid')
  176. course = self._download_json(
  177. 'http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id,
  178. course_id, 'Downloading course JSON')
  179. self._logout()
  180. if course.get('Status') == 'NotFound':
  181. raise ExtractorError(
  182. 'Course %s does not exist' % course_id, expected=True)
  183. unaccessible_videos = 0
  184. entries = []
  185. # Might want to extract videos right here from video['Formats'] as it seems 'Formats' is not provided
  186. # by single video API anymore
  187. for chapter in course['Chapters']:
  188. for video in chapter.get('Videos', []):
  189. if video.get('HasAccess') is False:
  190. unaccessible_videos += 1
  191. continue
  192. video_id = video.get('ID')
  193. if video_id:
  194. entries.append({
  195. '_type': 'url_transparent',
  196. 'url': 'http://www.lynda.com/%s/%s-4.html' % (course_path, video_id),
  197. 'ie_key': LyndaIE.ie_key(),
  198. 'chapter': chapter.get('Title'),
  199. 'chapter_number': int_or_none(chapter.get('ChapterIndex')),
  200. 'chapter_id': compat_str(chapter.get('ID')),
  201. })
  202. if unaccessible_videos > 0:
  203. self._downloader.report_warning(
  204. '%s videos are only available for members (or paid members) and will not be downloaded. '
  205. % unaccessible_videos + self._ACCOUNT_CREDENTIALS_HINT)
  206. course_title = course.get('Title')
  207. return self.playlist_result(entries, course_id, course_title)