[Rte] Improve extractor
[youtube-dl] / youtube_dl / extractor / gamespot.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_urllib_parse,
9     compat_urlparse,
10     unescapeHTML,
11     get_meta_content,
12 )
13
14
15 class GameSpotIE(InfoExtractor):
16     _VALID_URL = r'(?:http://)?(?:www\.)?gamespot\.com/.*-(?P<page_id>\d+)/?'
17     _TEST = {
18         'url': 'http://www.gamespot.com/videos/arma-3-community-guide-sitrep-i/2300-6410818/',
19         'md5': 'b2a30deaa8654fcccd43713a6b6a4825',
20         'info_dict': {
21             'id': 'gs-2300-6410818',
22             'ext': 'mp4',
23             'title': 'Arma 3 - Community Guide: SITREP I',
24             'description': 'Check out this video where some of the basics of Arma 3 is explained.',
25         }
26     }
27
28     def _real_extract(self, url):
29         mobj = re.match(self._VALID_URL, url)
30         page_id = mobj.group('page_id')
31         webpage = self._download_webpage(url, page_id)
32         data_video_json = self._search_regex(r'data-video=["\'](.*?)["\']', webpage, 'data video')
33         data_video = json.loads(unescapeHTML(data_video_json))
34
35         # Transform the manifest url to a link to the mp4 files
36         # they are used in mobile devices.
37         f4m_url = data_video['videoStreams']['f4m_stream']
38         f4m_path = compat_urlparse.urlparse(f4m_url).path
39         QUALITIES_RE = r'((,\d+)+,?)'
40         qualities = self._search_regex(QUALITIES_RE, f4m_path, 'qualities').strip(',').split(',')
41         http_path = f4m_path[1:].split('/', 1)[1]
42         http_template = re.sub(QUALITIES_RE, r'%s', http_path)
43         http_template = http_template.replace('.csmil/manifest.f4m', '')
44         http_template = compat_urlparse.urljoin('http://video.gamespotcdn.com/', http_template)
45         formats = []
46         for q in qualities:
47             formats.append({
48                 'url': http_template % q,
49                 'ext': 'mp4',
50                 'format_id': q,
51             })
52
53         return {
54             'id': data_video['guid'],
55             'title': compat_urllib_parse.unquote(data_video['title']),
56             'formats': formats,
57             'description': get_meta_content('description', webpage),
58             'thumbnail': self._og_search_thumbnail(webpage),
59         }