lecturio.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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'https://app\.lecturio\.com/[^/]+/(?P<id>[^/?#&]+)\.lecture'
  53. _TEST = {
  54. 'url': 'https://app.lecturio.com/medical-courses/important-concepts-and-terms-introduction-to-microbiology.lecture#tab/videos',
  55. 'md5': 'f576a797a5b7a5e4e4bbdfc25a6a6870',
  56. 'info_dict': {
  57. 'id': '39634',
  58. 'ext': 'mp4',
  59. 'title': 'Important Concepts and Terms – Introduction to Microbiology',
  60. },
  61. 'skip': 'Requires lecturio account credentials',
  62. }
  63. _CC_LANGS = {
  64. 'German': 'de',
  65. 'English': 'en',
  66. 'Spanish': 'es',
  67. 'French': 'fr',
  68. 'Polish': 'pl',
  69. 'Russian': 'ru',
  70. }
  71. def _real_extract(self, url):
  72. display_id = self._match_id(url)
  73. webpage = self._download_webpage(
  74. 'https://app.lecturio.com/en/lecture/%s/player.html' % display_id,
  75. display_id)
  76. lecture_id = self._search_regex(
  77. r'lecture_id\s*=\s*(?:L_)?(\d+)', webpage, 'lecture id')
  78. api_url = self._search_regex(
  79. r'lectureDataLink\s*:\s*(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
  80. 'api url', group='url')
  81. video = self._download_json(api_url, display_id)
  82. title = video['title'].strip()
  83. formats = []
  84. for format_ in video['content']['media']:
  85. if not isinstance(format_, dict):
  86. continue
  87. file_ = format_.get('file')
  88. if not file_:
  89. continue
  90. ext = determine_ext(file_)
  91. if ext == 'smil':
  92. # smil contains only broken RTMP formats anyway
  93. continue
  94. file_url = url_or_none(file_)
  95. if not file_url:
  96. continue
  97. label = str_or_none(format_.get('label'))
  98. filesize = int_or_none(format_.get('fileSize'))
  99. formats.append({
  100. 'url': file_url,
  101. 'format_id': label,
  102. 'filesize': float_or_none(filesize, invscale=1000)
  103. })
  104. self._sort_formats(formats)
  105. subtitles = {}
  106. automatic_captions = {}
  107. cc = self._parse_json(
  108. self._search_regex(
  109. r'subtitleUrls\s*:\s*({.+?})\s*,', webpage, 'subtitles',
  110. default='{}'), display_id, fatal=False)
  111. for cc_label, cc_url in cc.items():
  112. cc_url = url_or_none(cc_url)
  113. if not cc_url:
  114. continue
  115. sub_dict = automatic_captions if 'auto-translated' in cc_label else subtitles
  116. lang = self._search_regex(
  117. r'/([a-z]{2})_', cc_url, 'lang', default=cc_label.split()[0])
  118. sub_dict.setdefault(self._CC_LANGS.get(lang, lang), []).append({
  119. 'url': cc_url,
  120. })
  121. return {
  122. 'id': lecture_id,
  123. 'title': title,
  124. 'formats': formats,
  125. 'subtitles': subtitles,
  126. 'automatic_captions': automatic_captions,
  127. }
  128. class LecturioCourseIE(LecturioBaseIE):
  129. _VALID_URL = r'https://app\.lecturio\.com/[^/]+/(?P<id>[^/?#&]+)\.course'
  130. _TEST = {
  131. 'url': 'https://app.lecturio.com/medical-courses/microbiology-introduction.course#/',
  132. 'info_dict': {
  133. 'id': 'microbiology-introduction',
  134. 'title': 'Microbiology: Introduction',
  135. },
  136. 'playlist_count': 45,
  137. 'skip': 'Requires lecturio account credentials',
  138. }
  139. def _real_extract(self, url):
  140. display_id = self._match_id(url)
  141. webpage = self._download_webpage(url, display_id)
  142. entries = []
  143. for mobj in re.finditer(
  144. r'(?s)<[^>]+\bdata-url=(["\'])(?:(?!\1).)+\.lecture\b[^>]+>',
  145. webpage):
  146. params = extract_attributes(mobj.group(0))
  147. lecture_url = urljoin(url, params.get('data-url'))
  148. lecture_id = params.get('data-id')
  149. entries.append(self.url_result(
  150. lecture_url, ie=LecturioIE.ie_key(), video_id=lecture_id))
  151. title = self._search_regex(
  152. r'<span[^>]+class=["\']content-title[^>]+>([^<]+)', webpage,
  153. 'title', default=None)
  154. return self.playlist_result(entries, display_id, title)