[ellentube] Fix extraction (closes #14407)
[youtube-dl] / youtube_dl / extractor / ellentube.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     clean_html,
7     int_or_none,
8 )
9
10
11 class EllenTubeIE(InfoExtractor):
12     _VALID_URL = r'''(?x)
13                     (?:
14                         https://api-prod\.ellentube\.com/ellenapi/api/item/
15                         |ellentube:
16                     )
17                     (?P<id>
18                         [\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}
19                     )'''
20
21     _TESTS = [{
22         'url': 'https://api-prod.ellentube.com/ellenapi/api/item/75c64c16-aefd-4558-b4f5-3de09b22e6fc',
23         'match_only': True,
24     }, {
25         'url': 'ellentube:734a3353-f697-4e79-9ca9-bfc3002dc1e0',
26         'match_only': True,
27     }]
28
29     def _real_extract(self, url):
30         video_id = self._match_id(url)
31         data = self._download_json(
32             'https://api-prod.ellentube.com/ellenapi/api/item/%s' % video_id, video_id)
33         title = data['title']
34         description = data.get('description')
35         publish_time = int_or_none(data.get('publishTime'))
36         thumbnail = data.get('thumbnail')
37
38         formats = []
39         duration = None
40         for entry in data.get('media'):
41             if entry.get('id') == 'm3u8':
42                 formats = self._extract_m3u8_formats(
43                     entry.get('url'), video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id='hls')
44                 duration = int_or_none(entry.get('duration'))
45                 break
46         self._sort_formats(formats)
47         return {
48             'id': video_id,
49             'title': title,
50             'description': description,
51             'duration': duration,
52             'thumbnail': thumbnail,
53             'timestamp': publish_time,
54             'formats': formats,
55         }
56
57
58 class EllenTubeVideoIE(InfoExtractor):
59     _VALID_URL = r'https?://(?:www\.)?ellentube\.com/video/(?P<id>.+)\.html'
60
61     _TEST = {
62         'url': 'https://www.ellentube.com/video/ellen-meets-las-vegas-survivors-jesus-campos-and-stephen-schuck.html',
63         'md5': '2fabc277131bddafdd120e0fc0f974c9',
64         'info_dict': {
65             'id': '0822171c-3829-43bf-b99f-d77358ae75e3',
66             'ext': 'mp4',
67             'title': 'Ellen Meets Las Vegas Survivors Jesus Campos and Stephen Schuck',
68             'description': 'md5:76e3355e2242a78ad9e3858e5616923f',
69             'duration': 514,
70             'timestamp': 1508505120000,
71             'thumbnail': 'https://warnerbros-h.assetsadobe.com/is/image/content/dam/ellen/videos/episodes/season15/32/video--2728751654987218111',
72         }
73     }
74
75     def _real_extract(self, url):
76         display_id = self._match_id(url)
77         webpage = self._download_webpage(url, display_id)
78         video_id = self._html_search_regex(
79             r'(?s)<!--\s*CONTENT\s*-->.*data-config.+([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})',
80             webpage, 'video id')
81         return self.url_result('ellentube:%s' % video_id, 'EllenTube')
82
83
84 class EllenTubePlaylistIE(InfoExtractor):
85     def _extract_videos_from_json(self, data, display_id):
86         return [self.url_result('ellentube:%s' % elem['id'], 'EllenTube')
87                 for elem in data if elem.get('type') == 'VIDEO']
88
89     def _extract_playlist(self, url, display_id, extract_description=True):
90         webpage = self._download_webpage(url, display_id)
91         playlist_data = self._html_search_regex(
92             r'<div\s+data-component\s*=\s*"Details"(.+)</div>', webpage, 'playlist data')
93         playlist_title = self._search_regex(
94             r'"title"\s*:\s*"(.+?)"', playlist_data, 'playlist title')
95         playlist_description = clean_html(self._search_regex(
96             r'"description"\s*:\s*"(.+?)"', playlist_data, 'playlist description',
97             fatal=False)) if extract_description else None
98         api_search = self._search_regex(
99             r'"filter"\s*:\s*"(.+?)"', playlist_data, 'playlist api request')
100         api_data = self._download_json(
101             'https://api-prod.ellentube.com/ellenapi/api/feed/?%s' % api_search,
102             display_id)
103         return self.playlist_result(
104             self._extract_videos_from_json(api_data, display_id),
105             display_id, playlist_title, playlist_description)
106
107
108 class EllenTubeEpisodeIE(EllenTubePlaylistIE):
109     _VALID_URL = r'https?://(?:www\.)?ellentube\.com/episode/(?P<id>.+)\.html'
110
111     _TEST = {
112         'url': 'https://www.ellentube.com/episode/dax-shepard-jordan-fisher-haim.html',
113         'info_dict': {
114             'id': 'dax-shepard-jordan-fisher-haim',
115             'title': 'Dax Shepard, \'DWTS\' Team Jordan Fisher & Lindsay Arnold, HAIM',
116             'description': 'md5:aed85d42892f6126e71ec5ed2aea2a0d'
117         },
118         'playlist_count': 6,
119     }
120
121     def _real_extract(self, url):
122         display_id = self._match_id(url)
123         return self._extract_playlist(url, display_id)
124
125
126 class EllenTubeStudioIE(EllenTubePlaylistIE):
127     _VALID_URL = r'https?://(?:www\.)?ellentube\.com/studios/(?P<id>.+)\.html'
128
129     _TEST = {
130         'url': 'https://www.ellentube.com/studios/macey-goes-rving0.html',
131         'info_dict': {
132             'id': 'macey-goes-rving0',
133             'title': 'Macey Goes RVing',
134         },
135         'playlist_mincount': 3,
136     }
137
138     def _real_extract(self, url):
139         display_id = self._match_id(url)
140         return self._extract_playlist(url, display_id, False)