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.

229 lines
7.4 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. determine_ext,
  8. extract_attributes,
  9. ExtractorError,
  10. float_or_none,
  11. int_or_none,
  12. str_or_none,
  13. url_or_none,
  14. urlencode_postdata,
  15. urljoin,
  16. )
  17. class LecturioBaseIE(InfoExtractor):
  18. _LOGIN_URL = 'https://app.lecturio.com/en/login'
  19. _NETRC_MACHINE = 'lecturio'
  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. # Sets some cookies
  27. _, urlh = self._download_webpage_handle(
  28. self._LOGIN_URL, None, 'Downloading login popup')
  29. def is_logged(url_handle):
  30. return self._LOGIN_URL not in compat_str(url_handle.geturl())
  31. # Already logged in
  32. if is_logged(urlh):
  33. return
  34. login_form = {
  35. 'signin[email]': username,
  36. 'signin[password]': password,
  37. 'signin[remember]': 'on',
  38. }
  39. response, urlh = self._download_webpage_handle(
  40. self._LOGIN_URL, None, 'Logging in',
  41. data=urlencode_postdata(login_form))
  42. # Logged in successfully
  43. if is_logged(urlh):
  44. return
  45. errors = self._html_search_regex(
  46. r'(?s)<ul[^>]+class=["\']error_list[^>]+>(.+?)</ul>', response,
  47. 'errors', default=None)
  48. if errors:
  49. raise ExtractorError('Unable to login: %s' % errors, expected=True)
  50. raise ExtractorError('Unable to log in')
  51. class LecturioIE(LecturioBaseIE):
  52. _VALID_URL = r'''(?x)
  53. https://
  54. (?:
  55. app\.lecturio\.com/[^/]+/(?P<id>[^/?#&]+)\.lecture|
  56. (?:www\.)?lecturio\.de/[^/]+/(?P<id_de>[^/?#&]+)\.vortrag
  57. )
  58. '''
  59. _TESTS = [{
  60. 'url': 'https://app.lecturio.com/medical-courses/important-concepts-and-terms-introduction-to-microbiology.lecture#tab/videos',
  61. 'md5': 'f576a797a5b7a5e4e4bbdfc25a6a6870',
  62. 'info_dict': {
  63. 'id': '39634',
  64. 'ext': 'mp4',
  65. 'title': 'Important Concepts and Terms – Introduction to Microbiology',
  66. },
  67. 'skip': 'Requires lecturio account credentials',
  68. }, {
  69. 'url': 'https://www.lecturio.de/jura/oeffentliches-recht-staatsexamen.vortrag',
  70. 'only_matching': True,
  71. }]
  72. _CC_LANGS = {
  73. 'German': 'de',
  74. 'English': 'en',
  75. 'Spanish': 'es',
  76. 'French': 'fr',
  77. 'Polish': 'pl',
  78. 'Russian': 'ru',
  79. }
  80. def _real_extract(self, url):
  81. mobj = re.match(self._VALID_URL, url)
  82. display_id = mobj.group('id') or mobj.group('id_de')
  83. webpage = self._download_webpage(
  84. 'https://app.lecturio.com/en/lecture/%s/player.html' % display_id,
  85. display_id)
  86. lecture_id = self._search_regex(
  87. r'lecture_id\s*=\s*(?:L_)?(\d+)', webpage, 'lecture id')
  88. api_url = self._search_regex(
  89. r'lectureDataLink\s*:\s*(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
  90. 'api url', group='url')
  91. video = self._download_json(api_url, display_id)
  92. title = video['title'].strip()
  93. formats = []
  94. for format_ in video['content']['media']:
  95. if not isinstance(format_, dict):
  96. continue
  97. file_ = format_.get('file')
  98. if not file_:
  99. continue
  100. ext = determine_ext(file_)
  101. if ext == 'smil':
  102. # smil contains only broken RTMP formats anyway
  103. continue
  104. file_url = url_or_none(file_)
  105. if not file_url:
  106. continue
  107. label = str_or_none(format_.get('label'))
  108. filesize = int_or_none(format_.get('fileSize'))
  109. formats.append({
  110. 'url': file_url,
  111. 'format_id': label,
  112. 'filesize': float_or_none(filesize, invscale=1000)
  113. })
  114. self._sort_formats(formats)
  115. subtitles = {}
  116. automatic_captions = {}
  117. cc = self._parse_json(
  118. self._search_regex(
  119. r'subtitleUrls\s*:\s*({.+?})\s*,', webpage, 'subtitles',
  120. default='{}'), display_id, fatal=False)
  121. for cc_label, cc_url in cc.items():
  122. cc_url = url_or_none(cc_url)
  123. if not cc_url:
  124. continue
  125. lang = self._search_regex(
  126. r'/([a-z]{2})_', cc_url, 'lang',
  127. default=cc_label.split()[0] if cc_label else 'en')
  128. original_lang = self._search_regex(
  129. r'/[a-z]{2}_([a-z]{2})_', cc_url, 'original lang',
  130. default=None)
  131. sub_dict = (automatic_captions
  132. if 'auto-translated' in cc_label or original_lang
  133. else subtitles)
  134. sub_dict.setdefault(self._CC_LANGS.get(lang, lang), []).append({
  135. 'url': cc_url,
  136. })
  137. return {
  138. 'id': lecture_id,
  139. 'title': title,
  140. 'formats': formats,
  141. 'subtitles': subtitles,
  142. 'automatic_captions': automatic_captions,
  143. }
  144. class LecturioCourseIE(LecturioBaseIE):
  145. _VALID_URL = r'https://app\.lecturio\.com/[^/]+/(?P<id>[^/?#&]+)\.course'
  146. _TEST = {
  147. 'url': 'https://app.lecturio.com/medical-courses/microbiology-introduction.course#/',
  148. 'info_dict': {
  149. 'id': 'microbiology-introduction',
  150. 'title': 'Microbiology: Introduction',
  151. },
  152. 'playlist_count': 45,
  153. 'skip': 'Requires lecturio account credentials',
  154. }
  155. def _real_extract(self, url):
  156. display_id = self._match_id(url)
  157. webpage = self._download_webpage(url, display_id)
  158. entries = []
  159. for mobj in re.finditer(
  160. r'(?s)<[^>]+\bdata-url=(["\'])(?:(?!\1).)+\.lecture\b[^>]+>',
  161. webpage):
  162. params = extract_attributes(mobj.group(0))
  163. lecture_url = urljoin(url, params.get('data-url'))
  164. lecture_id = params.get('data-id')
  165. entries.append(self.url_result(
  166. lecture_url, ie=LecturioIE.ie_key(), video_id=lecture_id))
  167. title = self._search_regex(
  168. r'<span[^>]+class=["\']content-title[^>]+>([^<]+)', webpage,
  169. 'title', default=None)
  170. return self.playlist_result(entries, display_id, title)
  171. class LecturioDeCourseIE(LecturioBaseIE):
  172. _VALID_URL = r'https://(?:www\.)?lecturio\.de/[^/]+/(?P<id>[^/?#&]+)\.kurs'
  173. _TEST = {
  174. 'url': 'https://www.lecturio.de/jura/grundrechte.kurs',
  175. 'only_matching': True,
  176. }
  177. def _real_extract(self, url):
  178. display_id = self._match_id(url)
  179. webpage = self._download_webpage(url, display_id)
  180. entries = []
  181. for mobj in re.finditer(
  182. r'(?s)<td[^>]+\bdata-lecture-id=["\'](?P<id>\d+).+?\bhref=(["\'])(?P<url>(?:(?!\2).)+\.vortrag)\b[^>]+>',
  183. webpage):
  184. lecture_url = urljoin(url, mobj.group('url'))
  185. lecture_id = mobj.group('id')
  186. entries.append(self.url_result(
  187. lecture_url, ie=LecturioIE.ie_key(), video_id=lecture_id))
  188. title = self._search_regex(
  189. r'<h1[^>]*>([^<]+)', webpage, 'title', default=None)
  190. return self.playlist_result(entries, display_id, title)