]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/teamcoco.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / teamcoco.py
CommitLineData
3a105f7b 1import json
cd8b8302 2
e0d42dd4 3from .turner import TurnerBaseIE
ce9f47de 4from ..utils import (
ff8889cd 5 determine_ext,
ce9f47de 6 ExtractorError,
ff8889cd
RA
7 int_or_none,
8 mimetype2ext,
9 parse_duration,
10 parse_iso8601,
ce9f47de
NJ
11 qualities,
12)
cd8b8302
PH
13
14
e0d42dd4 15class TeamcocoIE(TurnerBaseIE):
69e6efac 16 _VALID_URL = r'https?://(?:\w+\.)?teamcoco\.com/(?P<id>([^/]+/)*[^/?#]+)'
bb799e81 17 _TESTS = [
9e1a5b84 18 {
ff8889cd
RA
19 'url': 'http://teamcoco.com/video/mary-kay-remote',
20 'md5': '55d532f81992f5c92046ad02fec34d7d',
9e1a5b84 21 'info_dict': {
17d2712d
PH
22 'id': '80187',
23 'ext': 'mp4',
9e1a5b84 24 'title': 'Conan Becomes A Mary Kay Beauty Consultant',
641eb10d 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.',
ff8889cd
RA
26 'duration': 495.0,
27 'upload_date': '20140402',
28 'timestamp': 1396407600,
9e1a5b84
JW
29 }
30 }, {
31 'url': 'http://teamcoco.com/video/louis-ck-interview-george-w-bush',
9e1a5b84
JW
32 'md5': 'cde9ba0fa3506f5f017ce11ead928f9a',
33 'info_dict': {
17d2712d
PH
34 'id': '19705',
35 'ext': 'mp4',
314368c8
NJ
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',
7088f5b5 38 'duration': 288,
ff8889cd
RA
39 'upload_date': '20111104',
40 'timestamp': 1320405840,
9e1a5b84 41 }
9c5335a0
YCH
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',
ff8889cd
RA
49 'upload_date': '20150415',
50 'timestamp': 1429088400,
9c5335a0
YCH
51 },
52 'params': {
53 'skip_download': True, # m3u8 downloads
54 }
d31573fa
YCH
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
ff8889cd
RA
65 },
66 'skip': 'This video is no longer available.',
acd620c9
RA
67 }, {
68 'url': 'http://teamcoco.com/video/the-conan-audiencey-awards-for-04/25/18',
69 'only_matching': True,
f2b1fa07
RA
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,
69e6efac
RA
79 }, {
80 'url': 'https://conan25.teamcoco.com/video/ice-cube-kevin-hart-conan-share-lyft',
81 'only_matching': True,
bb799e81 82 }
bb799e81 83 ]
48970d5c
RA
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'''
cd8b8302 97
ff8889cd
RA
98 def _graphql_call(self, query_template, object_type, object_id):
99 find_object = 'find' + object_type
100 return self._download_json(
69e6efac 101 'https://teamcoco.com/graphql', object_id, data=json.dumps({
ff8889cd 102 'query': query_template % (find_object, object_id)
69e6efac
RA
103 }).encode(), headers={
104 'Content-Type': 'application/json',
105 })['data'][find_object]
dfb2cb5c 106
ff8889cd
RA
107 def _real_extract(self, url):
108 display_id = self._match_id(url)
109
110 response = self._graphql_call('''{
48970d5c 111 %%s(slug: "%%s") {
ff8889cd
RA
112 ... on RecordSlug {
113 record {
48970d5c
RA
114 %s
115 }
116 }
117 ... on PageSlug {
118 child {
ff8889cd 119 id
ff8889cd
RA
120 }
121 }
122 ... on NotFoundSlug {
123 status
124 }
125 }
48970d5c 126}''' % self._RECORD_TEMPL, 'Slug', display_id)
ff8889cd
RA
127 if response.get('status'):
128 raise ExtractorError('This video is no longer available.', expected=True)
129
48970d5c
RA
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']
ff8889cd
RA
141 video_id = record['id']
142
e0d42dd4 143 info = {
bb198c95 144 'id': video_id,
dfb2cb5c 145 'display_id': display_id,
ff8889cd
RA
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')),
e7e6b54d 151 }
e0d42dd4
RA
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:
48970d5c 163 video_sources = self._download_json(
69e6efac 164 'https://teamcoco.com/_truman/d/' + video_id,
48970d5c
RA
165 video_id)['meta']['src']
166 if isinstance(video_sources, dict):
167 video_sources = video_sources.values()
e0d42dd4
RA
168
169 formats = []
170 get_quality = qualities(['low', 'sd', 'hd', 'uhd'])
48970d5c 171 for src in video_sources:
e0d42dd4
RA
172 if not isinstance(src, dict):
173 continue
174 src_url = src.get('src')
175 if not src_url:
176 continue
48970d5c 177 format_id = src.get('label')
e0d42dd4
RA
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 })
e0d42dd4
RA
199 info['formats'] = formats
200
201 return info