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.

244 lines
8.3 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. clean_html,
  8. determine_ext,
  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. _API_BASE_URL = 'https://app.lecturio.com/api/en/latest/html5/'
  19. _LOGIN_URL = 'https://app.lecturio.com/en/login'
  20. _NETRC_MACHINE = 'lecturio'
  21. def _real_initialize(self):
  22. self._login()
  23. def _login(self):
  24. username, password = self._get_login_info()
  25. if username is None:
  26. return
  27. # Sets some cookies
  28. _, urlh = self._download_webpage_handle(
  29. self._LOGIN_URL, None, 'Downloading login popup')
  30. def is_logged(url_handle):
  31. return self._LOGIN_URL not in compat_str(url_handle.geturl())
  32. # Already logged in
  33. if is_logged(urlh):
  34. return
  35. login_form = {
  36. 'signin[email]': username,
  37. 'signin[password]': password,
  38. 'signin[remember]': 'on',
  39. }
  40. response, urlh = self._download_webpage_handle(
  41. self._LOGIN_URL, None, 'Logging in',
  42. data=urlencode_postdata(login_form))
  43. # Logged in successfully
  44. if is_logged(urlh):
  45. return
  46. errors = self._html_search_regex(
  47. r'(?s)<ul[^>]+class=["\']error_list[^>]+>(.+?)</ul>', response,
  48. 'errors', default=None)
  49. if errors:
  50. raise ExtractorError('Unable to login: %s' % errors, expected=True)
  51. raise ExtractorError('Unable to log in')
  52. class LecturioIE(LecturioBaseIE):
  53. _VALID_URL = r'''(?x)
  54. https://
  55. (?:
  56. app\.lecturio\.com/([^/]+/(?P<nt>[^/?#&]+)\.lecture|(?:\#/)?lecture/c/\d+/(?P<id>\d+))|
  57. (?:www\.)?lecturio\.de/[^/]+/(?P<nt_de>[^/?#&]+)\.vortrag
  58. )
  59. '''
  60. _TESTS = [{
  61. 'url': 'https://app.lecturio.com/medical-courses/important-concepts-and-terms-introduction-to-microbiology.lecture#tab/videos',
  62. 'md5': '9a42cf1d8282a6311bf7211bbde26fde',
  63. 'info_dict': {
  64. 'id': '39634',
  65. 'ext': 'mp4',
  66. 'title': 'Important Concepts and Terms — Introduction to Microbiology',
  67. },
  68. 'skip': 'Requires lecturio account credentials',
  69. }, {
  70. 'url': 'https://www.lecturio.de/jura/oeffentliches-recht-staatsexamen.vortrag',
  71. 'only_matching': True,
  72. }, {
  73. 'url': 'https://app.lecturio.com/#/lecture/c/6434/39634',
  74. 'only_matching': True,
  75. }]
  76. _CC_LANGS = {
  77. 'Arabic': 'ar',
  78. 'Bulgarian': 'bg',
  79. 'German': 'de',
  80. 'English': 'en',
  81. 'Spanish': 'es',
  82. 'Persian': 'fa',
  83. 'French': 'fr',
  84. 'Japanese': 'ja',
  85. 'Polish': 'pl',
  86. 'Pashto': 'ps',
  87. 'Russian': 'ru',
  88. }
  89. def _real_extract(self, url):
  90. mobj = re.match(self._VALID_URL, url)
  91. nt = mobj.group('nt') or mobj.group('nt_de')
  92. lecture_id = mobj.group('id')
  93. display_id = nt or lecture_id
  94. api_path = 'lectures/' + lecture_id if lecture_id else 'lecture/' + nt + '.json'
  95. video = self._download_json(
  96. self._API_BASE_URL + api_path, display_id)
  97. title = video['title'].strip()
  98. if not lecture_id:
  99. pid = video.get('productId') or video.get('uid')
  100. if pid:
  101. spid = pid.split('_')
  102. if spid and len(spid) == 2:
  103. lecture_id = spid[1]
  104. formats = []
  105. for format_ in video['content']['media']:
  106. if not isinstance(format_, dict):
  107. continue
  108. file_ = format_.get('file')
  109. if not file_:
  110. continue
  111. ext = determine_ext(file_)
  112. if ext == 'smil':
  113. # smil contains only broken RTMP formats anyway
  114. continue
  115. file_url = url_or_none(file_)
  116. if not file_url:
  117. continue
  118. label = str_or_none(format_.get('label'))
  119. filesize = int_or_none(format_.get('fileSize'))
  120. f = {
  121. 'url': file_url,
  122. 'format_id': label,
  123. 'filesize': float_or_none(filesize, invscale=1000)
  124. }
  125. if label:
  126. mobj = re.match(r'(\d+)p\s*\(([^)]+)\)', label)
  127. if mobj:
  128. f.update({
  129. 'format_id': mobj.group(2),
  130. 'height': int(mobj.group(1)),
  131. })
  132. formats.append(f)
  133. self._sort_formats(formats)
  134. subtitles = {}
  135. automatic_captions = {}
  136. captions = video.get('captions') or []
  137. for cc in captions:
  138. cc_url = cc.get('url')
  139. if not cc_url:
  140. continue
  141. cc_label = cc.get('translatedCode')
  142. lang = cc.get('languageCode') or self._search_regex(
  143. r'/([a-z]{2})_', cc_url, 'lang',
  144. default=cc_label.split()[0] if cc_label else 'en')
  145. original_lang = self._search_regex(
  146. r'/[a-z]{2}_([a-z]{2})_', cc_url, 'original lang',
  147. default=None)
  148. sub_dict = (automatic_captions
  149. if 'auto-translated' in cc_label or original_lang
  150. else subtitles)
  151. sub_dict.setdefault(self._CC_LANGS.get(lang, lang), []).append({
  152. 'url': cc_url,
  153. })
  154. return {
  155. 'id': lecture_id or nt,
  156. 'title': title,
  157. 'formats': formats,
  158. 'subtitles': subtitles,
  159. 'automatic_captions': automatic_captions,
  160. }
  161. class LecturioCourseIE(LecturioBaseIE):
  162. _VALID_URL = r'https://app\.lecturio\.com/(?:[^/]+/(?P<nt>[^/?#&]+)\.course|(?:#/)?course/c/(?P<id>\d+))'
  163. _TESTS = [{
  164. 'url': 'https://app.lecturio.com/medical-courses/microbiology-introduction.course#/',
  165. 'info_dict': {
  166. 'id': 'microbiology-introduction',
  167. 'title': 'Microbiology: Introduction',
  168. 'description': 'md5:13da8500c25880c6016ae1e6d78c386a',
  169. },
  170. 'playlist_count': 45,
  171. 'skip': 'Requires lecturio account credentials',
  172. }, {
  173. 'url': 'https://app.lecturio.com/#/course/c/6434',
  174. 'only_matching': True,
  175. }]
  176. def _real_extract(self, url):
  177. nt, course_id = re.match(self._VALID_URL, url).groups()
  178. display_id = nt or course_id
  179. api_path = 'courses/' + course_id if course_id else 'course/content/' + nt + '.json'
  180. course = self._download_json(
  181. self._API_BASE_URL + api_path, display_id)
  182. entries = []
  183. for lecture in course.get('lectures', []):
  184. lecture_id = str_or_none(lecture.get('id'))
  185. lecture_url = lecture.get('url')
  186. if lecture_url:
  187. lecture_url = urljoin(url, lecture_url)
  188. else:
  189. lecture_url = 'https://app.lecturio.com/#/lecture/c/%s/%s' % (course_id, lecture_id)
  190. entries.append(self.url_result(
  191. lecture_url, ie=LecturioIE.ie_key(), video_id=lecture_id))
  192. return self.playlist_result(
  193. entries, display_id, course.get('title'),
  194. clean_html(course.get('description')))
  195. class LecturioDeCourseIE(LecturioBaseIE):
  196. _VALID_URL = r'https://(?:www\.)?lecturio\.de/[^/]+/(?P<id>[^/?#&]+)\.kurs'
  197. _TEST = {
  198. 'url': 'https://www.lecturio.de/jura/grundrechte.kurs',
  199. 'only_matching': True,
  200. }
  201. def _real_extract(self, url):
  202. display_id = self._match_id(url)
  203. webpage = self._download_webpage(url, display_id)
  204. entries = []
  205. for mobj in re.finditer(
  206. r'(?s)<td[^>]+\bdata-lecture-id=["\'](?P<id>\d+).+?\bhref=(["\'])(?P<url>(?:(?!\2).)+\.vortrag)\b[^>]+>',
  207. webpage):
  208. lecture_url = urljoin(url, mobj.group('url'))
  209. lecture_id = mobj.group('id')
  210. entries.append(self.url_result(
  211. lecture_url, ie=LecturioIE.ie_key(), video_id=lecture_id))
  212. title = self._search_regex(
  213. r'<h1[^>]*>([^<]+)', webpage, 'title', default=None)
  214. return self.playlist_result(entries, display_id, title)