[escapist] Support JavaScript player (Fixes #5034)
[youtube-dl] / youtube_dl / extractor / escapist.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..compat import (
5     compat_urllib_parse,
6 )
7 from ..utils import (
8     ExtractorError,
9     js_to_json,
10 )
11
12
13 class EscapistIE(InfoExtractor):
14     _VALID_URL = r'https?://?(www\.)?escapistmagazine\.com/videos/view/[^/?#]+/(?P<id>[0-9]+)-[^/?#]*(?:$|[?#])'
15     _TEST = {
16         'url': 'http://www.escapistmagazine.com/videos/view/the-escapist-presents/6618-Breaking-Down-Baldurs-Gate',
17         'md5': 'ab3a706c681efca53f0a35f1415cf0d1',
18         'info_dict': {
19             'id': '6618',
20             'ext': 'mp4',
21             'description': "Baldur's Gate: Original, Modded or Enhanced Edition? I'll break down what you can expect from the new Baldur's Gate: Enhanced Edition.",
22             'uploader_id': 'the-escapist-presents',
23             'uploader': 'The Escapist Presents',
24             'title': "Breaking Down Baldur's Gate",
25             'thumbnail': 're:^https?://.*\.jpg$',
26         }
27     }
28
29     def _real_extract(self, url):
30         video_id = self._match_id(url)
31         webpage = self._download_webpage(url, video_id)
32
33         uploader_id = self._html_search_regex(
34             r"<h1\s+class='headline'>\s*<a\s+href='/videos/view/(.*?)'",
35             webpage, 'uploader ID', fatal=False)
36         uploader = self._html_search_regex(
37             r"<h1\s+class='headline'>(.*?)</a>",
38             webpage, 'uploader', fatal=False)
39         description = self._html_search_meta('description', webpage)
40
41         raw_title = self._html_search_meta('title', webpage, fatal=True)
42         title = raw_title.partition(' : ')[2]
43
44         config_url = compat_urllib_parse.unquote(self._html_search_regex(
45             r'''(?x)
46             (?:
47                 <param\s+name="flashvars"\s+value="config=|
48                 flashvars=&quot;config=
49             )
50             ([^"&]+)
51             ''',
52             webpage, 'config URL'))
53
54         formats = []
55
56         def _add_format(name, cfgurl, quality):
57             config = self._download_json(
58                 cfgurl, video_id,
59                 'Downloading ' + name + ' configuration',
60                 'Unable to download ' + name + ' configuration',
61                 transform_source=js_to_json)
62
63             playlist = config['playlist']
64             video_url = next(
65                 p['url'] for p in playlist
66                 if p.get('eventCategory') == 'Video')
67             formats.append({
68                 'url': video_url,
69                 'format_id': name,
70                 'quality': quality,
71             })
72
73         _add_format('normal', config_url, quality=0)
74         hq_url = (config_url +
75                   ('&hq=1' if '?' in config_url else config_url + '?hq=1'))
76         try:
77             _add_format('hq', hq_url, quality=1)
78         except ExtractorError:
79             pass  # That's fine, we'll just use normal quality
80
81         self._sort_formats(formats)
82
83         return {
84             'id': video_id,
85             'formats': formats,
86             'uploader': uploader,
87             'uploader_id': uploader_id,
88             'title': title,
89             'thumbnail': self._og_search_thumbnail(webpage),
90             'description': description,
91         }