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