lecturio.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. clean_html,
  5. determine_ext,
  6. ExtractorError,
  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.geturl()
  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('Unable to login: %s' % 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://app.lecturio.com/#/lecture/c/6434/39634',
  66. 'only_matching': True,
  67. }]
  68. _CC_LANGS = {
  69. 'Arabic': 'ar',
  70. 'Bulgarian': 'bg',
  71. 'German': 'de',
  72. 'English': 'en',
  73. 'Spanish': 'es',
  74. 'Persian': 'fa',
  75. 'French': 'fr',
  76. 'Japanese': 'ja',
  77. 'Polish': 'pl',
  78. 'Pashto': 'ps',
  79. 'Russian': 'ru',
  80. }
  81. def _real_extract(self, url):
  82. mobj = self._match_valid_url(url)
  83. nt = mobj.group('nt') or mobj.group('nt_de')
  84. lecture_id = mobj.group('id')
  85. display_id = nt or lecture_id
  86. api_path = 'lectures/' + lecture_id if lecture_id else 'lecture/' + nt + '.json'
  87. video = self._download_json(
  88. self._API_BASE_URL + api_path, display_id)
  89. title = video['title'].strip()
  90. if not lecture_id:
  91. pid = video.get('productId') or video.get('uid')
  92. if pid:
  93. spid = pid.split('_')
  94. if spid and len(spid) == 2:
  95. lecture_id = spid[1]
  96. formats = []
  97. for format_ in video['content']['media']:
  98. if not isinstance(format_, dict):
  99. continue
  100. file_ = format_.get('file')
  101. if not file_:
  102. continue
  103. ext = determine_ext(file_)
  104. if ext == 'smil':
  105. # smil contains only broken RTMP formats anyway
  106. continue
  107. file_url = url_or_none(file_)
  108. if not file_url:
  109. continue
  110. label = str_or_none(format_.get('label'))
  111. filesize = int_or_none(format_.get('fileSize'))
  112. f = {
  113. 'url': file_url,
  114. 'format_id': label,
  115. 'filesize': float_or_none(filesize, invscale=1000)
  116. }
  117. if label:
  118. mobj = re.match(r'(\d+)p\s*\(([^)]+)\)', label)
  119. if mobj:
  120. f.update({
  121. 'format_id': mobj.group(2),
  122. 'height': int(mobj.group(1)),
  123. })
  124. formats.append(f)
  125. subtitles = {}
  126. automatic_captions = {}
  127. captions = video.get('captions') or []
  128. for cc in captions:
  129. cc_url = cc.get('url')
  130. if not cc_url:
  131. continue
  132. cc_label = cc.get('translatedCode')
  133. lang = cc.get('languageCode') or self._search_regex(
  134. r'/([a-z]{2})_', cc_url, 'lang',
  135. default=cc_label.split()[0] if cc_label else 'en')
  136. original_lang = self._search_regex(
  137. r'/[a-z]{2}_([a-z]{2})_', cc_url, 'original lang',
  138. default=None)
  139. sub_dict = (automatic_captions
  140. if 'auto-translated' in cc_label or original_lang
  141. else subtitles)
  142. sub_dict.setdefault(self._CC_LANGS.get(lang, lang), []).append({
  143. 'url': cc_url,
  144. })
  145. return {
  146. 'id': lecture_id or nt,
  147. 'title': title,
  148. 'formats': formats,
  149. 'subtitles': subtitles,
  150. 'automatic_captions': automatic_captions,
  151. }
  152. class LecturioCourseIE(LecturioBaseIE):
  153. _VALID_URL = r'https://app\.lecturio\.com/(?:[^/]+/(?P<nt>[^/?#&]+)\.course|(?:#/)?course/c/(?P<id>\d+))'
  154. _TESTS = [{
  155. 'url': 'https://app.lecturio.com/medical-courses/microbiology-introduction.course#/',
  156. 'info_dict': {
  157. 'id': 'microbiology-introduction',
  158. 'title': 'Microbiology: Introduction',
  159. 'description': 'md5:13da8500c25880c6016ae1e6d78c386a',
  160. },
  161. 'playlist_count': 45,
  162. 'skip': 'Requires lecturio account credentials',
  163. }, {
  164. 'url': 'https://app.lecturio.com/#/course/c/6434',
  165. 'only_matching': True,
  166. }]
  167. def _real_extract(self, url):
  168. nt, course_id = self._match_valid_url(url).groups()
  169. display_id = nt or course_id
  170. api_path = 'courses/' + course_id if course_id else 'course/content/' + nt + '.json'
  171. course = self._download_json(
  172. self._API_BASE_URL + api_path, display_id)
  173. entries = []
  174. for lecture in course.get('lectures', []):
  175. lecture_id = str_or_none(lecture.get('id'))
  176. lecture_url = lecture.get('url')
  177. if lecture_url:
  178. lecture_url = urljoin(url, lecture_url)
  179. else:
  180. lecture_url = 'https://app.lecturio.com/#/lecture/c/%s/%s' % (course_id, lecture_id)
  181. entries.append(self.url_result(
  182. lecture_url, ie=LecturioIE.ie_key(), video_id=lecture_id))
  183. return self.playlist_result(
  184. entries, display_id, course.get('title'),
  185. clean_html(course.get('description')))
  186. class LecturioDeCourseIE(LecturioBaseIE):
  187. _VALID_URL = r'https://(?:www\.)?lecturio\.de/[^/]+/(?P<id>[^/?#&]+)\.kurs'
  188. _TEST = {
  189. 'url': 'https://www.lecturio.de/jura/grundrechte.kurs',
  190. 'only_matching': True,
  191. }
  192. def _real_extract(self, url):
  193. display_id = self._match_id(url)
  194. webpage = self._download_webpage(url, display_id)
  195. entries = []
  196. for mobj in re.finditer(
  197. r'(?s)<td[^>]+\bdata-lecture-id=["\'](?P<id>\d+).+?\bhref=(["\'])(?P<url>(?:(?!\2).)+\.vortrag)\b[^>]+>',
  198. webpage):
  199. lecture_url = urljoin(url, mobj.group('url'))
  200. lecture_id = mobj.group('id')
  201. entries.append(self.url_result(
  202. lecture_url, ie=LecturioIE.ie_key(), video_id=lecture_id))
  203. title = self._search_regex(
  204. r'<h1[^>]*>([^<]+)', webpage, 'title', default=None)
  205. return self.playlist_result(entries, display_id, title)