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