[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / gamespot.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .once import OnceIE
6 from ..compat import (
7     compat_urllib_parse_unquote,
8 )
9 from ..utils import (
10     unescapeHTML,
11     url_basename,
12     dict_get,
13 )
14
15
16 class GameSpotIE(OnceIE):
17     _VALID_URL = r'https?://(?:www\.)?gamespot\.com/(?:video|article|review)s/(?:[^/]+/\d+-|embed/)(?P<id>\d+)'
18     _TESTS = [{
19         'url': 'http://www.gamespot.com/videos/arma-3-community-guide-sitrep-i/2300-6410818/',
20         'md5': 'b2a30deaa8654fcccd43713a6b6a4825',
21         'info_dict': {
22             'id': 'gs-2300-6410818',
23             'ext': 'mp4',
24             'title': 'Arma 3 - Community Guide: SITREP I',
25             'description': 'Check out this video where some of the basics of Arma 3 is explained.',
26         },
27     }, {
28         'url': 'http://www.gamespot.com/videos/the-witcher-3-wild-hunt-xbox-one-now-playing/2300-6424837/',
29         'info_dict': {
30             'id': 'gs-2300-6424837',
31             'ext': 'mp4',
32             'title': 'Now Playing - The Witcher 3: Wild Hunt',
33             'description': 'Join us as we take a look at the early hours of The Witcher 3: Wild Hunt and more.',
34         },
35         'params': {
36             'skip_download': True,  # m3u8 downloads
37         },
38     }, {
39         'url': 'https://www.gamespot.com/videos/embed/6439218/',
40         'only_matching': True,
41     }, {
42         'url': 'https://www.gamespot.com/articles/the-last-of-us-2-receives-new-ps4-trailer/1100-6454469/',
43         'only_matching': True,
44     }, {
45         'url': 'https://www.gamespot.com/reviews/gears-of-war-review/1900-6161188/',
46         'only_matching': True,
47     }]
48
49     def _real_extract(self, url):
50         page_id = self._match_id(url)
51         webpage = self._download_webpage(url, page_id)
52         data_video_json = self._search_regex(
53             r'data-video=["\'](.*?)["\']', webpage, 'data video')
54         data_video = self._parse_json(unescapeHTML(data_video_json), page_id)
55         streams = data_video['videoStreams']
56
57         manifest_url = None
58         formats = []
59         f4m_url = streams.get('f4m_stream')
60         if f4m_url:
61             manifest_url = f4m_url
62             formats.extend(self._extract_f4m_formats(
63                 f4m_url + '?hdcore=3.7.0', page_id, f4m_id='hds', fatal=False))
64         m3u8_url = dict_get(streams, ('m3u8_stream', 'adaptive_stream'))
65         if m3u8_url:
66             manifest_url = m3u8_url
67             m3u8_formats = self._extract_m3u8_formats(
68                 m3u8_url, page_id, 'mp4', 'm3u8_native',
69                 m3u8_id='hls', fatal=False)
70             formats.extend(m3u8_formats)
71         progressive_url = dict_get(
72             streams, ('progressive_hd', 'progressive_high', 'progressive_low', 'other_lr'))
73         if progressive_url and manifest_url:
74             qualities_basename = self._search_regex(
75                 r'/([^/]+)\.csmil/',
76                 manifest_url, 'qualities basename', default=None)
77             if qualities_basename:
78                 QUALITIES_RE = r'((,\d+)+,?)'
79                 qualities = self._search_regex(
80                     QUALITIES_RE, qualities_basename,
81                     'qualities', default=None)
82                 if qualities:
83                     qualities = list(map(lambda q: int(q), qualities.strip(',').split(',')))
84                     qualities.sort()
85                     http_template = re.sub(QUALITIES_RE, r'%d', qualities_basename)
86                     http_url_basename = url_basename(progressive_url)
87                     if m3u8_formats:
88                         self._sort_formats(m3u8_formats)
89                         m3u8_formats = list(filter(
90                             lambda f: f.get('vcodec') != 'none', m3u8_formats))
91                     if len(qualities) == len(m3u8_formats):
92                         for q, m3u8_format in zip(qualities, m3u8_formats):
93                             f = m3u8_format.copy()
94                             f.update({
95                                 'url': progressive_url.replace(
96                                     http_url_basename, http_template % q),
97                                 'format_id': f['format_id'].replace('hls', 'http'),
98                                 'protocol': 'http',
99                             })
100                             formats.append(f)
101                     else:
102                         for q in qualities:
103                             formats.append({
104                                 'url': progressive_url.replace(
105                                     http_url_basename, http_template % q),
106                                 'ext': 'mp4',
107                                 'format_id': 'http-%d' % q,
108                                 'tbr': q,
109                             })
110
111         onceux_json = self._search_regex(
112             r'data-onceux-options=["\'](.*?)["\']', webpage, 'data video', default=None)
113         if onceux_json:
114             onceux_url = self._parse_json(unescapeHTML(onceux_json), page_id).get('metadataUri')
115             if onceux_url:
116                 formats.extend(self._extract_once_formats(re.sub(
117                     r'https?://[^/]+', 'http://once.unicornmedia.com', onceux_url),
118                     http_formats_preference=-1))
119
120         if not formats:
121             for quality in ['sd', 'hd']:
122                 # It's actually a link to a flv file
123                 flv_url = streams.get('f4m_{0}'.format(quality))
124                 if flv_url is not None:
125                     formats.append({
126                         'url': flv_url,
127                         'ext': 'flv',
128                         'format_id': quality,
129                     })
130         self._sort_formats(formats)
131
132         return {
133             'id': data_video['guid'],
134             'display_id': page_id,
135             'title': compat_urllib_parse_unquote(data_video['title']),
136             'formats': formats,
137             'description': self._html_search_meta('description', webpage),
138             'thumbnail': self._og_search_thumbnail(webpage),
139         }