]> jfr.im git - yt-dlp.git/blob - youtube_dlc/extractor/egghead.py
Option `--windows-filenames` to force use of windows compatible filenames
[yt-dlp.git] / youtube_dlc / 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://egghead\.io/courses/(?P<id>[^/?#&]+)'
26 _TEST = {
27 'url': 'https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript',
28 'playlist_count': 29,
29 'info_dict': {
30 'id': '72',
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
36 def _real_extract(self, url):
37 playlist_id = self._match_id(url)
38 series_path = 'series/' + playlist_id
39 lessons = self._call_api(
40 series_path + '/lessons', playlist_id, 'course lessons')
41
42 entries = []
43 for lesson in lessons:
44 lesson_url = url_or_none(lesson.get('http_url'))
45 if not lesson_url:
46 continue
47 lesson_id = lesson.get('id')
48 if lesson_id:
49 lesson_id = compat_str(lesson_id)
50 entries.append(self.url_result(
51 lesson_url, ie=EggheadLessonIE.ie_key(), video_id=lesson_id))
52
53 course = self._call_api(
54 series_path, playlist_id, 'course', False) or {}
55
56 playlist_id = course.get('id')
57 if playlist_id:
58 playlist_id = compat_str(playlist_id)
59
60 return self.playlist_result(
61 entries, playlist_id, course.get('title'),
62 course.get('description'))
63
64
65 class EggheadLessonIE(EggheadBaseIE):
66 IE_DESC = 'egghead.io lesson'
67 IE_NAME = 'egghead:lesson'
68 _VALID_URL = r'https://egghead\.io/(?:api/v1/)?lessons/(?P<id>[^/?#&]+)'
69 _TESTS = [{
70 'url': 'https://egghead.io/lessons/javascript-linear-data-flow-with-container-style-types-box',
71 'info_dict': {
72 'id': '1196',
73 'display_id': 'javascript-linear-data-flow-with-container-style-types-box',
74 'ext': 'mp4',
75 'title': 'Create linear data flow with container style types (Box)',
76 'description': 'md5:9aa2cdb6f9878ed4c39ec09e85a8150e',
77 'thumbnail': r're:^https?:.*\.jpg$',
78 'timestamp': 1481296768,
79 'upload_date': '20161209',
80 'duration': 304,
81 'view_count': 0,
82 'tags': 'count:2',
83 },
84 'params': {
85 'skip_download': True,
86 'format': 'bestvideo',
87 },
88 }, {
89 'url': 'https://egghead.io/api/v1/lessons/react-add-redux-to-a-react-application',
90 'only_matching': True,
91 }]
92
93 def _real_extract(self, url):
94 display_id = self._match_id(url)
95
96 lesson = self._call_api(
97 'lessons/' + display_id, display_id, 'lesson')
98
99 lesson_id = compat_str(lesson['id'])
100 title = lesson['title']
101
102 formats = []
103 for _, format_url in lesson['media_urls'].items():
104 format_url = url_or_none(format_url)
105 if not format_url:
106 continue
107 ext = determine_ext(format_url)
108 if ext == 'm3u8':
109 formats.extend(self._extract_m3u8_formats(
110 format_url, lesson_id, 'mp4', entry_protocol='m3u8',
111 m3u8_id='hls', fatal=False))
112 elif ext == 'mpd':
113 formats.extend(self._extract_mpd_formats(
114 format_url, lesson_id, mpd_id='dash', fatal=False))
115 else:
116 formats.append({
117 'url': format_url,
118 })
119 self._sort_formats(formats)
120
121 return {
122 'id': lesson_id,
123 'display_id': display_id,
124 'title': title,
125 'description': lesson.get('summary'),
126 'thumbnail': lesson.get('thumb_nail'),
127 'timestamp': unified_timestamp(lesson.get('published_at')),
128 'duration': int_or_none(lesson.get('duration')),
129 'view_count': int_or_none(lesson.get('plays_count')),
130 'tags': try_get(lesson, lambda x: x['tag_list'], list),
131 'series': try_get(
132 lesson, lambda x: x['series']['title'], compat_str),
133 'formats': formats,
134 }