]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/itprotv.py
[cleanup] Use `_html_extract_title`
[yt-dlp.git] / yt_dlp / extractor / itprotv.py
CommitLineData
4628a3aa
TS
1# coding: utf-8
2
3import re
4
5from .common import InfoExtractor
6
7from ..utils import (
8 int_or_none,
9 str_or_none,
10 traverse_obj,
11 urljoin
12)
13
14
15class ITProTVBaseIE(InfoExtractor):
16 _ENDPOINTS = {
17 'course': 'course?url={}&brand=00002560-0000-3fa9-0000-1d61000035f3',
18 'episode': 'brand/00002560-0000-3fa9-0000-1d61000035f3/episode?url={}'
19 }
20
21 def _call_api(self, ep, item_id, webpage):
22 return self._download_json(
23 f'https://api.itpro.tv/api/urza/v3/consumer-web/{self._ENDPOINTS[ep].format(item_id)}',
24 item_id, note=f'Fetching {ep} data API',
25 headers={'Authorization': f'Bearer {self._fetch_jwt(webpage)}'})[ep]
26
27 def _fetch_jwt(self, webpage):
28 return self._search_regex(r'{"passedToken":"([\w-]+\.[\w-]+\.[\w-]+)",', webpage, 'jwt')
29
30 def _check_if_logged_in(self, webpage):
31 if re.match(r'{\s*member\s*:\s*null', webpage):
32 self.raise_login_required()
33
34
35class ITProTVIE(ITProTVBaseIE):
36 _VALID_URL = r'https://app.itpro.tv/course/(?P<course>[\w-]+)/(?P<id>[\w-]+)'
37 _TESTS = [{
38 'url': 'https://app.itpro.tv/course/guided-tour/introductionitprotv',
39 'md5': 'bca4a28c2667fd1a63052e71a94bb88c',
40 'info_dict': {
41 'id': 'introductionitprotv',
42 'ext': 'mp4',
43 'title': 'An Introduction to ITProTV 101',
44 'thumbnail': 'https://itprotv-image-bucket.s3.amazonaws.com/getting-started/itprotv-101-introduction-PGM.11_39_56_02.Still001.png',
45 'description': 'md5:b175c2c3061ce35a4dd33865b2c1da4e',
46 'duration': 269,
47 'series': 'ITProTV 101',
48 'series_id': 'guided-tour',
49 'availability': 'needs_auth',
50 'chapter': 'ITProTV 101',
51 'chapter_number': 1,
52 'chapter_id': '5dbb3de426b46c0010b5d1b6'
53 },
54 },
55 {
56 'url': 'https://app.itpro.tv/course/beyond-tech/job-interview-tips',
57 'md5': '101a299b98c47ccf4c67f9f0951defa8',
58 'info_dict': {
59 'id': 'job-interview-tips',
60 'ext': 'mp4',
61 'title': 'Job Interview Tips',
62 'thumbnail': 'https://s3.amazonaws.com:443/production-itprotv-thumbnails/2f370bf5-294d-4bbe-ab80-c0b5781630ea.png',
63 'description': 'md5:30d8ba483febdf89ec85623aad3c3cb6',
64 'duration': 267,
65 'series': 'Beyond Tech',
66 'series_id': 'beyond-tech',
67 'availability': 'needs_auth',
68 'chapter': 'Job Development',
69 'chapter_number': 2,
70 'chapter_id': '5f7c78d424330c000edf04d9'
71 },
72 }]
73
74 def _real_extract(self, url):
75 episode_id, course_name = self._match_valid_url(url).group('id', 'course')
76 webpage = self._download_webpage(url, episode_id)
77 self._check_if_logged_in(webpage)
78 course = self._call_api('course', course_name, webpage)
79 episode = self._call_api('episode', episode_id, webpage)
80
81 chapter_number, chapter = next((
82 (i, topic) for i, topic in enumerate(course.get('topics') or [], 1)
83 if traverse_obj(topic, 'id') == episode.get('topic')), {})
84
85 return {
86 'id': episode_id,
87 'title': episode.get('title'),
88 'description': episode.get('description'),
89 'thumbnail': episode.get('thumbnail'),
90 'formats': [
91 {'url': episode[f'jwVideo{h}Embed'], 'height': h}
92 for h in (320, 480, 720, 1080) if episode.get(f'jwVideo{h}Embed')
93 ],
94 'duration': int_or_none(episode.get('length')),
95 'series': course.get('name'),
96 'series_id': course.get('url'),
97 'chapter': str_or_none(chapter.get('title')),
98 'chapter_number': chapter_number,
99 'chapter_id': str_or_none(chapter.get('id')),
100 'subtitles': {
101 'en': [{'ext': 'vtt', 'data': episode['enCaptionData']}]
102 } if episode.get('enCaptionData') else None,
103 }
104
105
106class ITProTVCourseIE(ITProTVBaseIE):
107 _VALID_URL = r'https?://app.itpro.tv/course/(?P<id>[\w-]+)/?(?:$|[#?])'
108 _TESTS = [
109 {
110 'url': 'https://app.itpro.tv/course/guided-tour',
111 'info_dict': {
112 'id': 'guided-tour',
113 'description': 'md5:b175c2c3061ce35a4dd33865b2c1da4e',
114 'title': 'ITProTV 101',
115 },
116 'playlist_count': 6
117 },
118 {
119 'url': 'https://app.itpro.tv/course/beyond-tech',
120 'info_dict': {
121 'id': 'beyond-tech',
122 'description': 'md5:44cd99855e7f81a15ce1269bd0621fed',
123 'title': 'Beyond Tech'
124 },
125 'playlist_count': 15
126 },
127 ]
128
129 def _real_extract(self, url):
130 course_id = self._match_id(url)
131 webpage = self._download_webpage(url, course_id)
132 self._check_if_logged_in(webpage)
133 course = self._call_api('course', course_id, webpage)
134
135 entries = [self.url_result(
136 urljoin(url, f'{course_id}/{episode["url"]}'), ITProTVIE,
137 episode['url'], episode.get('title'), url_transparent=True)
138 for episode in course['episodes']]
139
140 return self.playlist_result(
141 entries, course_id, course.get('name'), course.get('description'))