]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/ign.py
[extractor/FranceCulture] Fix extractor (#3874)
[yt-dlp.git] / yt_dlp / extractor / ign.py
1 import re
2
3 from .common import InfoExtractor
4 from ..compat import (
5 compat_parse_qs,
6 compat_urllib_parse_urlparse,
7 )
8 from ..utils import (
9 HEADRequest,
10 determine_ext,
11 int_or_none,
12 parse_iso8601,
13 strip_or_none,
14 try_get,
15 )
16
17
18 class IGNBaseIE(InfoExtractor):
19 def _call_api(self, slug):
20 return self._download_json(
21 'http://apis.ign.com/{0}/v3/{0}s/slug/{1}'.format(self._PAGE_TYPE, slug), slug)
22
23
24 class IGNIE(IGNBaseIE):
25 """
26 Extractor for some of the IGN sites, like www.ign.com, es.ign.com de.ign.com.
27 Some videos of it.ign.com are also supported
28 """
29
30 _VALID_URL = r'https?://(?:.+?\.ign|www\.pcmag)\.com/videos/(?:\d{4}/\d{2}/\d{2}/)?(?P<id>[^/?&#]+)'
31 IE_NAME = 'ign.com'
32 _PAGE_TYPE = 'video'
33
34 _TESTS = [{
35 'url': 'http://www.ign.com/videos/2013/06/05/the-last-of-us-review',
36 'md5': 'd2e1586d9987d40fad7867bf96a018ea',
37 'info_dict': {
38 'id': '8f862beef863986b2785559b9e1aa599',
39 'ext': 'mp4',
40 'title': 'The Last of Us Review',
41 'description': 'md5:c8946d4260a4d43a00d5ae8ed998870c',
42 'timestamp': 1370440800,
43 'upload_date': '20130605',
44 'tags': 'count:9',
45 }
46 }, {
47 'url': 'http://www.pcmag.com/videos/2015/01/06/010615-whats-new-now-is-gogo-snooping-on-your-data',
48 'md5': 'f1581a6fe8c5121be5b807684aeac3f6',
49 'info_dict': {
50 'id': 'ee10d774b508c9b8ec07e763b9125b91',
51 'ext': 'mp4',
52 'title': 'What\'s New Now: Is GoGo Snooping on Your Data?',
53 'description': 'md5:817a20299de610bd56f13175386da6fa',
54 'timestamp': 1420571160,
55 'upload_date': '20150106',
56 'tags': 'count:4',
57 }
58 }, {
59 'url': 'https://www.ign.com/videos/is-a-resident-evil-4-remake-on-the-way-ign-daily-fix',
60 'only_matching': True,
61 }]
62
63 def _real_extract(self, url):
64 display_id = self._match_id(url)
65 video = self._call_api(display_id)
66 video_id = video['videoId']
67 metadata = video['metadata']
68 title = metadata.get('longTitle') or metadata.get('title') or metadata['name']
69
70 formats = []
71 refs = video.get('refs') or {}
72
73 m3u8_url = refs.get('m3uUrl')
74 if m3u8_url:
75 formats.extend(self._extract_m3u8_formats(
76 m3u8_url, video_id, 'mp4', 'm3u8_native',
77 m3u8_id='hls', fatal=False))
78
79 f4m_url = refs.get('f4mUrl')
80 if f4m_url:
81 formats.extend(self._extract_f4m_formats(
82 f4m_url, video_id, f4m_id='hds', fatal=False))
83
84 for asset in (video.get('assets') or []):
85 asset_url = asset.get('url')
86 if not asset_url:
87 continue
88 formats.append({
89 'url': asset_url,
90 'tbr': int_or_none(asset.get('bitrate'), 1000),
91 'fps': int_or_none(asset.get('frame_rate')),
92 'height': int_or_none(asset.get('height')),
93 'width': int_or_none(asset.get('width')),
94 })
95
96 mezzanine_url = try_get(video, lambda x: x['system']['mezzanineUrl'])
97 if mezzanine_url:
98 formats.append({
99 'ext': determine_ext(mezzanine_url, 'mp4'),
100 'format_id': 'mezzanine',
101 'quality': 1,
102 'url': mezzanine_url,
103 })
104
105 self._sort_formats(formats)
106
107 thumbnails = []
108 for thumbnail in (video.get('thumbnails') or []):
109 thumbnail_url = thumbnail.get('url')
110 if not thumbnail_url:
111 continue
112 thumbnails.append({
113 'url': thumbnail_url,
114 })
115
116 tags = []
117 for tag in (video.get('tags') or []):
118 display_name = tag.get('displayName')
119 if not display_name:
120 continue
121 tags.append(display_name)
122
123 return {
124 'id': video_id,
125 'title': title,
126 'description': strip_or_none(metadata.get('description')),
127 'timestamp': parse_iso8601(metadata.get('publishDate')),
128 'duration': int_or_none(metadata.get('duration')),
129 'display_id': display_id,
130 'thumbnails': thumbnails,
131 'formats': formats,
132 'tags': tags,
133 }
134
135
136 class IGNVideoIE(InfoExtractor):
137 _VALID_URL = r'https?://.+?\.ign\.com/(?:[a-z]{2}/)?[^/]+/(?P<id>\d+)/(?:video|trailer)/'
138 _TESTS = [{
139 'url': 'http://me.ign.com/en/videos/112203/video/how-hitman-aims-to-be-different-than-every-other-s',
140 'md5': 'dd9aca7ed2657c4e118d8b261e5e9de1',
141 'info_dict': {
142 'id': 'e9be7ea899a9bbfc0674accc22a36cc8',
143 'ext': 'mp4',
144 'title': 'How Hitman Aims to Be Different Than Every Other Stealth Game - NYCC 2015',
145 'description': 'Taking out assassination targets in Hitman has never been more stylish.',
146 'timestamp': 1444665600,
147 'upload_date': '20151012',
148 }
149 }, {
150 'url': 'http://me.ign.com/ar/angry-birds-2/106533/video/lrd-ldyy-lwl-lfylm-angry-birds',
151 'only_matching': True,
152 }, {
153 # Youtube embed
154 'url': 'https://me.ign.com/ar/ratchet-clank-rift-apart/144327/trailer/embed',
155 'only_matching': True,
156 }, {
157 # Twitter embed
158 'url': 'http://adria.ign.com/sherlock-season-4/9687/trailer/embed',
159 'only_matching': True,
160 }, {
161 # Vimeo embed
162 'url': 'https://kr.ign.com/bic-2018/3307/trailer/embed',
163 'only_matching': True,
164 }]
165
166 def _real_extract(self, url):
167 video_id = self._match_id(url)
168 req = HEADRequest(url.rsplit('/', 1)[0] + '/embed')
169 url = self._request_webpage(req, video_id).geturl()
170 ign_url = compat_parse_qs(
171 compat_urllib_parse_urlparse(url).query).get('url', [None])[0]
172 if ign_url:
173 return self.url_result(ign_url, IGNIE.ie_key())
174 return self.url_result(url)
175
176
177 class IGNArticleIE(IGNBaseIE):
178 _VALID_URL = r'https?://.+?\.ign\.com/(?:articles(?:/\d{4}/\d{2}/\d{2})?|(?:[a-z]{2}/)?feature/\d+)/(?P<id>[^/?&#]+)'
179 _PAGE_TYPE = 'article'
180 _TESTS = [{
181 'url': 'http://me.ign.com/en/feature/15775/100-little-things-in-gta-5-that-will-blow-your-mind',
182 'info_dict': {
183 'id': '524497489e4e8ff5848ece34',
184 'title': '100 Little Things in GTA 5 That Will Blow Your Mind',
185 },
186 'playlist': [
187 {
188 'info_dict': {
189 'id': '5ebbd138523268b93c9141af17bec937',
190 'ext': 'mp4',
191 'title': 'GTA 5 Video Review',
192 'description': 'Rockstar drops the mic on this generation of games. Watch our review of the masterly Grand Theft Auto V.',
193 'timestamp': 1379339880,
194 'upload_date': '20130916',
195 },
196 },
197 {
198 'info_dict': {
199 'id': '638672ee848ae4ff108df2a296418ee2',
200 'ext': 'mp4',
201 'title': '26 Twisted Moments from GTA 5 in Slow Motion',
202 'description': 'The twisted beauty of GTA 5 in stunning slow motion.',
203 'timestamp': 1386878820,
204 'upload_date': '20131212',
205 },
206 },
207 ],
208 'params': {
209 'playlist_items': '2-3',
210 'skip_download': True,
211 },
212 }, {
213 'url': 'http://www.ign.com/articles/2014/08/15/rewind-theater-wild-trailer-gamescom-2014?watch',
214 'info_dict': {
215 'id': '53ee806780a81ec46e0790f8',
216 'title': 'Rewind Theater - Wild Trailer Gamescom 2014',
217 },
218 'playlist_count': 2,
219 }, {
220 # videoId pattern
221 'url': 'http://www.ign.com/articles/2017/06/08/new-ducktales-short-donalds-birthday-doesnt-go-as-planned',
222 'only_matching': True,
223 }, {
224 # Youtube embed
225 'url': 'https://www.ign.com/articles/2021-mvp-named-in-puppy-bowl-xvii',
226 'only_matching': True,
227 }, {
228 # IMDB embed
229 'url': 'https://www.ign.com/articles/2014/08/07/sons-of-anarchy-final-season-trailer',
230 'only_matching': True,
231 }, {
232 # Facebook embed
233 'url': 'https://www.ign.com/articles/2017/09/20/marvels-the-punisher-watch-the-new-trailer-for-the-netflix-series',
234 'only_matching': True,
235 }, {
236 # Brightcove embed
237 'url': 'https://www.ign.com/articles/2016/01/16/supergirl-goes-flying-with-martian-manhunter-in-new-clip',
238 'only_matching': True,
239 }]
240
241 def _real_extract(self, url):
242 display_id = self._match_id(url)
243 article = self._call_api(display_id)
244
245 def entries():
246 media_url = try_get(article, lambda x: x['mediaRelations'][0]['media']['metadata']['url'])
247 if media_url:
248 yield self.url_result(media_url, IGNIE.ie_key())
249 for content in (article.get('content') or []):
250 for video_url in re.findall(r'(?:\[(?:ignvideo\s+url|youtube\s+clip_id)|<iframe[^>]+src)="([^"]+)"', content):
251 yield self.url_result(video_url)
252
253 return self.playlist_result(
254 entries(), article.get('articleId'),
255 strip_or_none(try_get(article, lambda x: x['metadata']['headline'])))