]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/egghead.py
[youtube:comments] Add more options for limiting number of comments extracted (#1626)
[yt-dlp.git] / yt_dlp / extractor / egghead.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_str
6 from ..utils import (
7 determine_ext,
8 int_or_none,
9 try_get,
10 unified_timestamp,
11 url_or_none,
12 )
13
14
15 class EggheadBaseIE(InfoExtractor):
16 def _call_api(self, path, video_id, resource, fatal=True):
17 return self._download_json(
18 'https://app.egghead.io/api/v1/' + path,
19 video_id, 'Downloading %s JSON' % resource, fatal=fatal)
20
21
22 class EggheadCourseIE(EggheadBaseIE):
23 IE_DESC = 'egghead.io course'
24 IE_NAME = 'egghead:course'
25 _VALID_URL = r'https://(?:app\.)?egghead\.io/(?:course|playlist)s/(?P<id>[^/?#&]+)'
26 _TESTS = [{
27 'url': 'https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript',
28 'playlist_count': 29,
29 'info_dict': {
30 'id': '432655',
31 'title': 'Professor Frisby Introduces Composable Functional JavaScript',
32 'description': 're:(?s)^This course teaches the ubiquitous.*You\'ll start composing functionality before you know it.$',
33 },
34 }, {
35 'url': 'https://app.egghead.io/playlists/professor-frisby-introduces-composable-functional-javascript',
36 'only_matching': True,
37 }]
38
39 def _real_extract(self, url):
40 playlist_id = self._match_id(url)
41 series_path = 'series/' + playlist_id
42 lessons = self._call_api(
43 series_path + '/lessons', playlist_id, 'course lessons')
44
45 entries = []
46 for lesson in lessons:
47 lesson_url = url_or_none(lesson.get('http_url'))
48 if not lesson_url:
49 continue
50 lesson_id = lesson.get('id')
51 if lesson_id:
52 lesson_id = compat_str(lesson_id)
53 entries.append(self.url_result(
54 lesson_url, ie=EggheadLessonIE.ie_key(), video_id=lesson_id))
55
56 course = self._call_api(
57 series_path, playlist_id, 'course', False) or {}
58
59 playlist_id = course.get('id')
60 if playlist_id:
61 playlist_id = compat_str(playlist_id)
62
63 return self.playlist_result(
64 entries, playlist_id, course.get('title'),
65 course.get('description'))
66
67
68 class EggheadLessonIE(EggheadBaseIE):
69 IE_DESC = 'egghead.io lesson'
70 IE_NAME = 'egghead:lesson'
71 _VALID_URL = r'https://(?:app\.)?egghead\.io/(?:api/v1/)?lessons/(?P<id>[^/?#&]+)'
72 _TESTS = [{
73 'url': 'https://egghead.io/lessons/javascript-linear-data-flow-with-container-style-types-box',
74 'info_dict': {
75 'id': '1196',
76 'display_id': 'javascript-linear-data-flow-with-container-style-types-box',
77 'ext': 'mp4',
78 'title': 'Create linear data flow with container style types (Box)',
79 'description': 'md5:9aa2cdb6f9878ed4c39ec09e85a8150e',
80 'thumbnail': r're:^https?:.*\.jpg$',
81 'timestamp': 1481296768,
82 'upload_date': '20161209',
83 'duration': 304,
84 'view_count': 0,
85 'tags': 'count:2',
86 },
87 'params': {
88 'skip_download': True,
89 },
90 }, {
91 'url': 'https://egghead.io/api/v1/lessons/react-add-redux-to-a-react-application',
92 'only_matching': True,
93 }, {
94 'url': 'https://app.egghead.io/lessons/javascript-linear-data-flow-with-container-style-types-box',
95 'only_matching': True,
96 }]
97
98 def _real_extract(self, url):
99 display_id = self._match_id(url)
100
101 lesson = self._call_api(
102 'lessons/' + display_id, display_id, 'lesson')
103
104 lesson_id = compat_str(lesson['id'])
105 title = lesson['title']
106
107 formats = []
108 for _, format_url in lesson['media_urls'].items():
109 format_url = url_or_none(format_url)
110 if not format_url:
111 continue
112 ext = determine_ext(format_url)
113 if ext == 'm3u8':
114 formats.extend(self._extract_m3u8_formats(
115 format_url, lesson_id, 'mp4', m3u8_id='hls', fatal=False))
116 elif ext == 'mpd':
117 formats.extend(self._extract_mpd_formats(
118 format_url, lesson_id, mpd_id='dash', fatal=False))
119 else:
120 formats.append({
121 'url': format_url,
122 })
123 self._sort_formats(formats)
124
125 return {
126 'id': lesson_id,
127 'display_id': display_id,
128 'title': title,
129 'description': lesson.get('summary'),
130 'thumbnail': lesson.get('thumb_nail'),
131 'timestamp': unified_timestamp(lesson.get('published_at')),
132 'duration': int_or_none(lesson.get('duration')),
133 'view_count': int_or_none(lesson.get('plays_count')),
134 'tags': try_get(lesson, lambda x: x['tag_list'], list),
135 'series': try_get(
136 lesson, lambda x: x['series']['title'], compat_str),
137 'formats': formats,
138 }