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