lecturio.py 8.2 KB

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