[youtube] Fix extraction.
[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     extract_attributes,
8     float_or_none,
9     int_or_none,
10     try_get,
11 )
12
13
14 class EllenTubeBaseIE(InfoExtractor):
15     def _extract_data_config(self, webpage, video_id):
16         details = self._search_regex(
17             r'(<[^>]+\bdata-component=(["\'])[Dd]etails.+?></div>)', webpage,
18             'details')
19         return self._parse_json(
20             extract_attributes(details)['data-config'], video_id)
21
22     def _extract_video(self, data, video_id):
23         title = data['title']
24
25         formats = []
26         duration = None
27         for entry in data.get('media'):
28             if entry.get('id') == 'm3u8':
29                 formats = self._extract_m3u8_formats(
30                     entry['url'], video_id, 'mp4',
31                     entry_protocol='m3u8_native', m3u8_id='hls')
32                 duration = int_or_none(entry.get('duration'))
33                 break
34         self._sort_formats(formats)
35
36         def get_insight(kind):
37             return int_or_none(try_get(
38                 data, lambda x: x['insight']['%ss' % kind]))
39
40         return {
41             'extractor_key': EllenTubeIE.ie_key(),
42             'id': video_id,
43             'title': title,
44             'description': data.get('description'),
45             'duration': duration,
46             'thumbnail': data.get('thumbnail'),
47             'timestamp': float_or_none(data.get('publishTime'), scale=1000),
48             'view_count': get_insight('view'),
49             'like_count': get_insight('like'),
50             'formats': formats,
51         }
52
53
54 class EllenTubeIE(EllenTubeBaseIE):
55     _VALID_URL = r'''(?x)
56                         (?:
57                             ellentube:|
58                             https://api-prod\.ellentube\.com/ellenapi/api/item/
59                         )
60                         (?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})
61                     '''
62     _TESTS = [{
63         'url': 'https://api-prod.ellentube.com/ellenapi/api/item/0822171c-3829-43bf-b99f-d77358ae75e3',
64         'md5': '2fabc277131bddafdd120e0fc0f974c9',
65         'info_dict': {
66             'id': '0822171c-3829-43bf-b99f-d77358ae75e3',
67             'ext': 'mp4',
68             'title': 'Ellen Meets Las Vegas Survivors Jesus Campos and Stephen Schuck',
69             'description': 'md5:76e3355e2242a78ad9e3858e5616923f',
70             'thumbnail': r're:^https?://.+?',
71             'duration': 514,
72             'timestamp': 1508505120,
73             'upload_date': '20171020',
74             'view_count': int,
75             'like_count': int,
76         }
77     }, {
78         'url': 'ellentube:734a3353-f697-4e79-9ca9-bfc3002dc1e0',
79         'only_matching': True,
80     }]
81
82     def _real_extract(self, url):
83         video_id = self._match_id(url)
84         data = self._download_json(
85             'https://api-prod.ellentube.com/ellenapi/api/item/%s' % video_id,
86             video_id)
87         return self._extract_video(data, video_id)
88
89
90 class EllenTubeVideoIE(EllenTubeBaseIE):
91     _VALID_URL = r'https?://(?:www\.)?ellentube\.com/video/(?P<id>.+?)\.html'
92     _TEST = {
93         'url': 'https://www.ellentube.com/video/ellen-meets-las-vegas-survivors-jesus-campos-and-stephen-schuck.html',
94         'only_matching': True,
95     }
96
97     def _real_extract(self, url):
98         display_id = self._match_id(url)
99         webpage = self._download_webpage(url, display_id)
100         video_id = self._extract_data_config(webpage, display_id)['id']
101         return self.url_result(
102             'ellentube:%s' % video_id, ie=EllenTubeIE.ie_key(),
103             video_id=video_id)
104
105
106 class EllenTubePlaylistIE(EllenTubeBaseIE):
107     _VALID_URL = r'https?://(?:www\.)?ellentube\.com/(?:episode|studios)/(?P<id>.+?)\.html'
108     _TESTS = [{
109         'url': 'https://www.ellentube.com/episode/dax-shepard-jordan-fisher-haim.html',
110         'info_dict': {
111             'id': 'dax-shepard-jordan-fisher-haim',
112             'title': "Dax Shepard, 'DWTS' Team Jordan Fisher & Lindsay Arnold, HAIM",
113             'description': 'md5:bfc982194dabb3f4e325e43aa6b2e21c',
114         },
115         'playlist_count': 6,
116     }, {
117         'url': 'https://www.ellentube.com/studios/macey-goes-rving0.html',
118         'only_matching': True,
119     }]
120
121     def _real_extract(self, url):
122         display_id = self._match_id(url)
123         webpage = self._download_webpage(url, display_id)
124         data = self._extract_data_config(webpage, display_id)['data']
125         feed = self._download_json(
126             'https://api-prod.ellentube.com/ellenapi/api/feed/?%s'
127             % data['filter'], display_id)
128         entries = [
129             self._extract_video(elem, elem['id'])
130             for elem in feed if elem.get('type') == 'VIDEO' and elem.get('id')]
131         return self.playlist_result(
132             entries, display_id, data.get('title'),
133             clean_html(data.get('description')))