logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

lecturio.py (8437B)


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