Merge pull request #7045 from remitamine/ign
[youtube-dl] / youtube_dl / extractor / ign.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     int_or_none,
8     parse_iso8601,
9 )
10
11
12 class IGNIE(InfoExtractor):
13     """
14     Extractor for some of the IGN sites, like www.ign.com, es.ign.com de.ign.com.
15     Some videos of it.ign.com are also supported
16     """
17
18     _VALID_URL = r'https?://.+?\.ign\.com/(?:[^/]+/)?(?P<type>videos|show_videos|articles|feature|(?:[^/]+/\d+/video))(/.+)?/(?P<name_or_id>.+)'
19     IE_NAME = 'ign.com'
20
21     _API_URL_TEMPLATE = 'http://apis.ign.com/video/v3/videos/%s'
22     _EMBED_RE = r'<iframe[^>]+?["\']((?:https?:)?//.+?\.ign\.com.+?/embed.+?)["\']'
23
24     _TESTS = [
25         {
26             'url': 'http://www.ign.com/videos/2013/06/05/the-last-of-us-review',
27             'md5': 'febda82c4bafecd2d44b6e1a18a595f8',
28             'info_dict': {
29                 'id': '8f862beef863986b2785559b9e1aa599',
30                 'ext': 'mp4',
31                 'title': 'The Last of Us Review',
32                 'description': 'md5:c8946d4260a4d43a00d5ae8ed998870c',
33                 'timestamp': 1370440800,
34                 'upload_date': '20130605',
35                 'uploader_id': 'cberidon@ign.com',
36             }
37         },
38         {
39             'url': 'http://me.ign.com/en/feature/15775/100-little-things-in-gta-5-that-will-blow-your-mind',
40             'info_dict': {
41                 'id': '100-little-things-in-gta-5-that-will-blow-your-mind',
42             },
43             'playlist': [
44                 {
45                     'info_dict': {
46                         'id': '5ebbd138523268b93c9141af17bec937',
47                         'ext': 'mp4',
48                         'title': 'GTA 5 Video Review',
49                         'description': 'Rockstar drops the mic on this generation of games. Watch our review of the masterly Grand Theft Auto V.',
50                         'timestamp': 1379339880,
51                         'upload_date': '20130916',
52                         'uploader_id': 'danieljkrupa@gmail.com',
53                     },
54                 },
55                 {
56                     'info_dict': {
57                         'id': '638672ee848ae4ff108df2a296418ee2',
58                         'ext': 'mp4',
59                         'title': '26 Twisted Moments from GTA 5 in Slow Motion',
60                         'description': 'The twisted beauty of GTA 5 in stunning slow motion.',
61                         'timestamp': 1386878820,
62                         'upload_date': '20131212',
63                         'uploader_id': 'togilvie@ign.com',
64                     },
65                 },
66             ],
67             'params': {
68                 'skip_download': True,
69             },
70         },
71         {
72             'url': 'http://www.ign.com/articles/2014/08/15/rewind-theater-wild-trailer-gamescom-2014?watch',
73             'md5': '618fedb9c901fd086f6f093564ef8558',
74             'info_dict': {
75                 'id': '078fdd005f6d3c02f63d795faa1b984f',
76                 'ext': 'mp4',
77                 'title': 'Rewind Theater - Wild Trailer Gamescom 2014',
78                 'description': 'Brian and Jared explore Michel Ancel\'s captivating new preview.',
79                 'timestamp': 1408047180,
80                 'upload_date': '20140814',
81                 'uploader_id': 'jamesduggan1990@gmail.com',
82             },
83         },
84         {
85             'url': 'http://me.ign.com/en/videos/112203/video/how-hitman-aims-to-be-different-than-every-other-s',
86             'only_matching': True,
87         },
88         {
89             'url': 'http://me.ign.com/ar/angry-birds-2/106533/video/lrd-ldyy-lwl-lfylm-angry-birds',
90             'only_matching': True,
91         },
92     ]
93
94     def _find_video_id(self, webpage):
95         res_id = [
96             r'"video_id"\s*:\s*"(.*?)"',
97             r'class="hero-poster[^"]*?"[^>]*id="(.+?)"',
98             r'data-video-id="(.+?)"',
99             r'<object id="vid_(.+?)"',
100             r'<meta name="og:image" content=".*/(.+?)-(.+?)/.+.jpg"',
101         ]
102         return self._search_regex(res_id, webpage, 'video id', default=None)
103
104     def _real_extract(self, url):
105         mobj = re.match(self._VALID_URL, url)
106         name_or_id = mobj.group('name_or_id')
107         page_type = mobj.group('type')
108         webpage = self._download_webpage(url, name_or_id)
109         if page_type != 'video':
110             multiple_urls = re.findall(
111                 r'<param name="flashvars"[^>]*value="[^"]*?url=(https?://www\.ign\.com/videos/.*?)["&]',
112                  webpage)
113             if multiple_urls:
114                 entries = [self.url_result(u, ie='IGN') for u in multiple_urls]
115                 return {
116                     '_type': 'playlist',
117                     'id': name_or_id,
118                     'entries': entries,
119                 }
120
121         video_id = self._find_video_id(webpage)
122         if not video_id:
123             return self.url_result(self._search_regex(self._EMBED_RE, webpage, 'embed url'))
124         return self._get_video_info(video_id)
125
126     def _get_video_info(self, video_id):
127         api_data = self._download_json(self._API_URL_TEMPLATE % video_id, video_id)
128
129         formats = []
130         m3u8_url = api_data['refs'].get('m3uUrl')
131         if m3u8_url:
132             m3u8_formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
133             if m3u8_formats:
134                 formats.extend(m3u8_formats)
135         f4m_url = api_data['refs'].get('f4mUrl')
136         if f4m_url:
137             f4m_formats = self._extract_f4m_formats(f4m_url, video_id, f4m_id='hds', fatal=False)
138             if f4m_formats:
139                 formats.extend(f4m_formats)
140         for asset in api_data['assets']:
141             formats.append({
142                 'url': asset['url'],
143                 'tbr': asset.get('actual_bitrate_kbps'),
144                 'fps': asset.get('frame_rate'),
145                 'height': int_or_none(asset.get('height')),
146                 'width': int_or_none(asset.get('width')),
147             })
148         self._sort_formats(formats)
149
150         thumbnails = [{
151             'url': thumbnail['url']
152         } for thumbnail in api_data.get('thumbnails', [])]
153
154         metadata = api_data['metadata']
155
156         return {
157             'id': api_data.get('videoId') or video_id,
158             'title': metadata.get('longTitle') or metadata.get('name') or metadata.get['title'],
159             'description': metadata.get('description'),
160             'timestamp': parse_iso8601(metadata.get('publishDate')),
161             'duration': int_or_none(metadata.get('duration')),
162             'display_id': metadata.get('slug') or video_id,
163             'uploader_id': metadata.get('creator'),
164             'thumbnails': thumbnails,
165             'formats': formats,
166         }
167
168
169 class OneUPIE(IGNIE):
170     _VALID_URL = r'https?://gamevideos\.1up\.com/(?P<type>video)/id/(?P<name_or_id>.+)\.html'
171     IE_NAME = '1up.com'
172
173     _TESTS = [{
174         'url': 'http://gamevideos.1up.com/video/id/34976.html',
175         'md5': 'c9cc69e07acb675c31a16719f909e347',
176         'info_dict': {
177             'id': '34976',
178             'ext': 'mp4',
179             'title': 'Sniper Elite V2 - Trailer',
180             'description': 'md5:bf0516c5ee32a3217aa703e9b1bc7826',
181             'timestamp': 1313099220,
182             'upload_date': '20110811',
183             'uploader_id': 'IGN',
184         }
185     }]
186
187     def _real_extract(self, url):
188         mobj = re.match(self._VALID_URL, url)
189         result = super(OneUPIE, self)._real_extract(url)
190         result['id'] = mobj.group('name_or_id')
191         return result
192
193
194 class PCMagIE(IGNIE):
195     _VALID_URL = r'https?://(?:www\.)?pcmag\.com/(?P<type>videos|article2)(/.+)?/(?P<name_or_id>.+)'
196     IE_NAME = 'pcmag'
197
198     _EMBED_RE = r'iframe.setAttribute\("src",\s*__util.objToUrlString\("http://widgets\.ign\.com/video/embed/content.html?[^"]*url=([^"]+)["&]'
199
200     _TESTS = [{
201         'url': 'http://www.pcmag.com/videos/2015/01/06/010615-whats-new-now-is-gogo-snooping-on-your-data',
202         'md5': '212d6154fd0361a2781075f1febbe9ad',
203         'info_dict': {
204             'id': 'ee10d774b508c9b8ec07e763b9125b91',
205             'ext': 'mp4',
206             'title': '010615_What\'s New Now: Is GoGo Snooping on Your Data?',
207             'description': 'md5:a7071ae64d2f68cc821c729d4ded6bb3',
208             'timestamp': 1420571160,
209             'upload_date': '20150106',
210             'uploader_id': 'cozzipix@gmail.com',
211         }
212     },{
213         'url': 'http://www.pcmag.com/article2/0,2817,2470156,00.asp',
214         'md5': '94130c1ca07ba0adb6088350681f16c1',
215         'info_dict': {
216             'id': '042e560ba94823d43afcb12ddf7142ca',
217             'ext': 'mp4',
218             'title': 'HTC\'s Weird New Re Camera - What\'s New Now',
219             'description': 'md5:53433c45df96d2ea5d0fda18be2ca908',
220             'timestamp': 1412953920,
221             'upload_date': '20141010',
222             'uploader_id': 'chris_snyder@pcmag.com',
223         }
224     }]