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