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