]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/teamcoco.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / teamcoco.py
1 import json
2
3 from .turner import TurnerBaseIE
4 from ..utils import (
5 determine_ext,
6 ExtractorError,
7 int_or_none,
8 mimetype2ext,
9 parse_duration,
10 parse_iso8601,
11 qualities,
12 )
13
14
15 class TeamcocoIE(TurnerBaseIE):
16 _VALID_URL = r'https?://(?:\w+\.)?teamcoco\.com/(?P<id>([^/]+/)*[^/?#]+)'
17 _TESTS = [
18 {
19 'url': 'http://teamcoco.com/video/mary-kay-remote',
20 'md5': '55d532f81992f5c92046ad02fec34d7d',
21 'info_dict': {
22 'id': '80187',
23 'ext': 'mp4',
24 'title': 'Conan Becomes A Mary Kay Beauty Consultant',
25 'description': 'Mary Kay is perhaps the most trusted name in female beauty, so of course Conan is a natural choice to sell their products.',
26 'duration': 495.0,
27 'upload_date': '20140402',
28 'timestamp': 1396407600,
29 }
30 }, {
31 'url': 'http://teamcoco.com/video/louis-ck-interview-george-w-bush',
32 'md5': 'cde9ba0fa3506f5f017ce11ead928f9a',
33 'info_dict': {
34 'id': '19705',
35 'ext': 'mp4',
36 'description': 'Louis C.K. got starstruck by George W. Bush, so what? Part one.',
37 'title': 'Louis C.K. Interview Pt. 1 11/3/11',
38 'duration': 288,
39 'upload_date': '20111104',
40 'timestamp': 1320405840,
41 }
42 }, {
43 'url': 'http://teamcoco.com/video/timothy-olyphant-drinking-whiskey',
44 'info_dict': {
45 'id': '88748',
46 'ext': 'mp4',
47 'title': 'Timothy Olyphant Raises A Toast To “Justified”',
48 'description': 'md5:15501f23f020e793aeca761205e42c24',
49 'upload_date': '20150415',
50 'timestamp': 1429088400,
51 },
52 'params': {
53 'skip_download': True, # m3u8 downloads
54 }
55 }, {
56 'url': 'http://teamcoco.com/video/full-episode-mon-6-1-joel-mchale-jake-tapper-and-musical-guest-courtney-barnett?playlist=x;eyJ0eXBlIjoidGFnIiwiaWQiOjl9',
57 'info_dict': {
58 'id': '89341',
59 'ext': 'mp4',
60 'title': 'Full Episode - Mon. 6/1 - Joel McHale, Jake Tapper, And Musical Guest Courtney Barnett',
61 'description': 'Guests: Joel McHale, Jake Tapper, And Musical Guest Courtney Barnett',
62 },
63 'params': {
64 'skip_download': True, # m3u8 downloads
65 },
66 'skip': 'This video is no longer available.',
67 }, {
68 'url': 'http://teamcoco.com/video/the-conan-audiencey-awards-for-04/25/18',
69 'only_matching': True,
70 }, {
71 'url': 'http://teamcoco.com/italy/conan-jordan-schlansky-hit-the-streets-of-florence',
72 'only_matching': True,
73 }, {
74 'url': 'http://teamcoco.com/haiti/conan-s-haitian-history-lesson',
75 'only_matching': True,
76 }, {
77 'url': 'http://teamcoco.com/israel/conan-hits-the-streets-beaches-of-tel-aviv',
78 'only_matching': True,
79 }, {
80 'url': 'https://conan25.teamcoco.com/video/ice-cube-kevin-hart-conan-share-lyft',
81 'only_matching': True,
82 }
83 ]
84 _RECORD_TEMPL = '''id
85 title
86 teaser
87 publishOn
88 thumb {
89 preview
90 }
91 tags {
92 name
93 }
94 duration
95 turnerMediaId
96 turnerMediaAuthToken'''
97
98 def _graphql_call(self, query_template, object_type, object_id):
99 find_object = 'find' + object_type
100 return self._download_json(
101 'https://teamcoco.com/graphql', object_id, data=json.dumps({
102 'query': query_template % (find_object, object_id)
103 }).encode(), headers={
104 'Content-Type': 'application/json',
105 })['data'][find_object]
106
107 def _real_extract(self, url):
108 display_id = self._match_id(url)
109
110 response = self._graphql_call('''{
111 %%s(slug: "%%s") {
112 ... on RecordSlug {
113 record {
114 %s
115 }
116 }
117 ... on PageSlug {
118 child {
119 id
120 }
121 }
122 ... on NotFoundSlug {
123 status
124 }
125 }
126 }''' % self._RECORD_TEMPL, 'Slug', display_id)
127 if response.get('status'):
128 raise ExtractorError('This video is no longer available.', expected=True)
129
130 child = response.get('child')
131 if child:
132 record = self._graphql_call('''{
133 %%s(id: "%%s") {
134 ... on Video {
135 %s
136 }
137 }
138 }''' % self._RECORD_TEMPL, 'Record', child['id'])
139 else:
140 record = response['record']
141 video_id = record['id']
142
143 info = {
144 'id': video_id,
145 'display_id': display_id,
146 'title': record['title'],
147 'thumbnail': record.get('thumb', {}).get('preview'),
148 'description': record.get('teaser'),
149 'duration': parse_duration(record.get('duration')),
150 'timestamp': parse_iso8601(record.get('publishOn')),
151 }
152
153 media_id = record.get('turnerMediaId')
154 if media_id:
155 self._initialize_geo_bypass({
156 'countries': ['US'],
157 })
158 info.update(self._extract_ngtv_info(media_id, {
159 'accessToken': record['turnerMediaAuthToken'],
160 'accessTokenType': 'jws',
161 }))
162 else:
163 video_sources = self._download_json(
164 'https://teamcoco.com/_truman/d/' + video_id,
165 video_id)['meta']['src']
166 if isinstance(video_sources, dict):
167 video_sources = video_sources.values()
168
169 formats = []
170 get_quality = qualities(['low', 'sd', 'hd', 'uhd'])
171 for src in video_sources:
172 if not isinstance(src, dict):
173 continue
174 src_url = src.get('src')
175 if not src_url:
176 continue
177 format_id = src.get('label')
178 ext = determine_ext(src_url, mimetype2ext(src.get('type')))
179 if format_id == 'hls' or ext == 'm3u8':
180 # compat_urllib_parse.urljoin does not work here
181 if src_url.startswith('/'):
182 src_url = 'http://ht.cdn.turner.com/tbs/big/teamcoco' + src_url
183 formats.extend(self._extract_m3u8_formats(
184 src_url, video_id, 'mp4', m3u8_id=format_id, fatal=False))
185 else:
186 if src_url.startswith('/mp4:protected/'):
187 # TODO Correct extraction for these files
188 continue
189 tbr = int_or_none(self._search_regex(
190 r'(\d+)k\.mp4', src_url, 'tbr', default=None))
191
192 formats.append({
193 'url': src_url,
194 'ext': ext,
195 'tbr': tbr,
196 'format_id': format_id,
197 'quality': get_quality(format_id),
198 })
199 info['formats'] = formats
200
201 return info