[ign] add support for pcmag and extract all formats and more metadata
[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             }
36         },
37         {
38             'url': 'http://me.ign.com/en/feature/15775/100-little-things-in-gta-5-that-will-blow-your-mind',
39             'info_dict': {
40                 'id': '100-little-things-in-gta-5-that-will-blow-your-mind',
41             },
42             'playlist': [
43                 {
44                     'info_dict': {
45                         'id': '5ebbd138523268b93c9141af17bec937',
46                         'ext': 'mp4',
47                         'title': 'GTA 5 Video Review',
48                         'description': 'Rockstar drops the mic on this generation of games. Watch our review of the masterly Grand Theft Auto V.',
49                         'timestamp': 1379339880,
50                         'upload_date': '20130916',
51                     },
52                 },
53                 {
54                     'info_dict': {
55                         'id': '638672ee848ae4ff108df2a296418ee2',
56                         'ext': 'mp4',
57                         'title': '26 Twisted Moments from GTA 5 in Slow Motion',
58                         'description': 'The twisted beauty of GTA 5 in stunning slow motion.',
59                         'timestamp': 1386878820,
60                         'upload_date': '20131212',
61                     },
62                 },
63             ],
64             'params': {
65                 'skip_download': True,
66             },
67         },
68         {
69             'url': 'http://www.ign.com/articles/2014/08/15/rewind-theater-wild-trailer-gamescom-2014?watch',
70             'md5': '618fedb9c901fd086f6f093564ef8558',
71             'info_dict': {
72                 'id': '078fdd005f6d3c02f63d795faa1b984f',
73                 'ext': 'mp4',
74                 'title': 'Rewind Theater - Wild Trailer Gamescom 2014',
75                 'description': 'Brian and Jared explore Michel Ancel\'s captivating new preview.',
76                 'timestamp': 1408047180,
77                 'upload_date': '20140814',
78             },
79         },
80     ]
81
82     def _find_video_id(self, webpage):
83         res_id = [
84             r'"video_id"\s*:\s*"(.*?)"',
85             r'class="hero-poster[^"]*?"[^>]*id="(.+?)"',
86             r'data-video-id="(.+?)"',
87             r'<object id="vid_(.+?)"',
88             r'<meta name="og:image" content=".*/(.+?)-(.+?)/.+.jpg"',
89         ]
90         return self._search_regex(res_id, webpage, 'video id', default=None)
91
92     def _real_extract(self, url):
93         mobj = re.match(self._VALID_URL, url)
94         name_or_id = mobj.group('name_or_id')
95         page_type = mobj.group('type')
96         webpage = self._download_webpage(url, name_or_id)
97         if page_type != 'video':
98             multiple_urls = re.findall(
99                 '<param name="flashvars"[^>]*value="[^"]*?url=(https?://www\.ign\.com/videos/.*?)["&]',
100                 webpage)
101             if multiple_urls:
102                 entries = [self.url_result(u, ie='IGN') for u in multiple_urls]
103                 return {
104                     '_type': 'playlist',
105                     'id': name_or_id,
106                     'entries': entries,
107                 }
108
109         video_id = self._find_video_id(webpage)
110         if not video_id:
111             return self.url_result(self._search_regex(self._EMBED_RE, webpage, 'embed url'))
112         return self._get_video_info(video_id)
113
114     def _get_video_info(self, video_id):
115         api_data = self._download_json(self._API_URL_TEMPLATE % video_id, video_id)
116
117         formats = []
118         m3u8_url = api_data['refs'].get('m3uUrl')
119         if m3u8_url:
120             formats.extend(self._extract_m3u8_formats(m3u8_url, video_id))
121         f4m_url = api_data['refs'].get('f4mUrl')
122         if f4m_url:
123             formats.extend(self._extract_f4m_formats(f4m_url, video_id))
124         for asset in api_data['assets']:
125             formats.append({
126                 'url': asset['url'],
127                 'tbr': asset.get('actual_bitrate_kbps'),
128                 'fps': asset.get('frame_rate'),
129                 'height': int_or_none(asset.get('height')),
130                 'width': int_or_none(asset.get('width')),
131             })
132         self._sort_formats(formats)
133
134         thumbnails = []
135         for thumbnail in api_data['thumbnails']:
136             thumbnails.append({'url': thumbnail['url']})
137
138         metadata = api_data['metadata']
139
140         return {
141             'id': api_data.get('videoId') or video_id,
142             'title': metadata.get('longTitle') or metadata.get('name') or metadata.get['title'],
143             'description': metadata.get('description'),
144             'timestamp': parse_iso8601(metadata.get('publishDate')),
145             'duration': int_or_none(metadata.get('duration')),
146             'display_id': metadata.get('slug') or video_id,
147             'thumbnails': thumbnails,
148             'formats': formats,
149         }
150
151
152 class OneUPIE(IGNIE):
153     _VALID_URL = r'https?://gamevideos\.1up\.com/(?P<type>video)/id/(?P<name_or_id>.+)\.html'
154     IE_NAME = '1up.com'
155
156     _TESTS = [{
157         'url': 'http://gamevideos.1up.com/video/id/34976.html',
158         'md5': 'c9cc69e07acb675c31a16719f909e347',
159         'info_dict': {
160             'id': '34976',
161             'ext': 'mp4',
162             'title': 'Sniper Elite V2 - Trailer',
163             'description': 'md5:bf0516c5ee32a3217aa703e9b1bc7826',
164             'timestamp': 1313099220,
165             'upload_date': '20110811',
166         }
167     }]
168
169     def _real_extract(self, url):
170         mobj = re.match(self._VALID_URL, url)
171         result = super(OneUPIE, self)._real_extract(url)
172         result['id'] = mobj.group('name_or_id')
173         return result
174
175
176 class PCMagIE(IGNIE):
177     _VALID_URL = r'https?://(?:www\.)?pcmag\.com/(?P<type>videos|article2)(/.+)?/(?P<name_or_id>.+)'
178     IE_NAME = 'pcmag'
179
180     _EMBED_RE = r'iframe.setAttribute\("src",\s*__util.objToUrlString\("http://widgets\.ign\.com/video/embed/content.html?[^"]*url=([^"]+)["&]'
181
182     _TESTS = [{
183         'url': 'http://www.pcmag.com/videos/2015/01/06/010615-whats-new-now-is-gogo-snooping-on-your-data',
184         'md5': '212d6154fd0361a2781075f1febbe9ad',
185         'info_dict': {
186             'id': 'ee10d774b508c9b8ec07e763b9125b91',
187             'ext': 'mp4',
188             'title': '010615_What\'s New Now: Is GoGo Snooping on Your Data?',
189             'description': 'md5:a7071ae64d2f68cc821c729d4ded6bb3',
190             'timestamp': 1420571160,
191             'upload_date': '20150106',
192         }
193     },{
194         'url': 'http://www.pcmag.com/article2/0,2817,2470156,00.asp',
195         'md5': '94130c1ca07ba0adb6088350681f16c1',
196         'info_dict': {
197             'id': '042e560ba94823d43afcb12ddf7142ca',
198             'ext': 'mp4',
199             'title': 'HTC\'s Weird New Re Camera - What\'s New Now',
200             'description': 'md5:53433c45df96d2ea5d0fda18be2ca908',
201             'timestamp': 1412953920,
202             'upload_date': '20141010',
203         }
204     }]