Fix imports and general cleanup
[youtube-dl] / youtube_dl / extractor / escapist.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_urllib_parse,
8 )
9 from ..utils import (
10     ExtractorError,
11 )
12
13
14 class EscapistIE(InfoExtractor):
15     _VALID_URL = r'^https?://?(www\.)?escapistmagazine\.com/videos/view/(?P<showname>[^/]+)/(?P<id>[0-9]+)-'
16     _TEST = {
17         'url': 'http://www.escapistmagazine.com/videos/view/the-escapist-presents/6618-Breaking-Down-Baldurs-Gate',
18         'md5': 'ab3a706c681efca53f0a35f1415cf0d1',
19         'info_dict': {
20             'id': '6618',
21             'ext': 'mp4',
22             '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.",
23             'uploader': 'the-escapist-presents',
24             'title': "Breaking Down Baldur's Gate",
25         }
26     }
27
28     def _real_extract(self, url):
29         mobj = re.match(self._VALID_URL, url)
30         showName = mobj.group('showname')
31         video_id = mobj.group('id')
32
33         self.report_extraction(video_id)
34         webpage = self._download_webpage(url, video_id)
35
36         videoDesc = self._html_search_regex(
37             r'<meta name="description" content="([^"]*)"',
38             webpage, 'description', fatal=False)
39
40         playerUrl = self._og_search_video_url(webpage, name='player URL')
41
42         title = self._html_search_regex(
43             r'<meta name="title" content="([^"]*)"',
44             webpage, 'title').split(' : ')[-1]
45
46         configUrl = self._search_regex('config=(.*)$', playerUrl, 'config URL')
47         configUrl = compat_urllib_parse.unquote(configUrl)
48
49         formats = []
50
51         def _add_format(name, cfgurl, quality):
52             config = self._download_json(
53                 cfgurl, video_id,
54                 'Downloading ' + name + ' configuration',
55                 'Unable to download ' + name + ' configuration',
56                 transform_source=lambda s: s.replace("'", '"'))
57
58             playlist = config['playlist']
59             formats.append({
60                 'url': playlist[1]['url'],
61                 'format_id': name,
62                 'quality': quality,
63             })
64
65         _add_format('normal', configUrl, quality=0)
66         hq_url = (configUrl +
67                   ('&hq=1' if '?' in configUrl else configUrl + '?hq=1'))
68         try:
69             _add_format('hq', hq_url, quality=1)
70         except ExtractorError:
71             pass  # That's fine, we'll just use normal quality
72
73         self._sort_formats(formats)
74
75         return {
76             'id': video_id,
77             'formats': formats,
78             'uploader': showName,
79             'title': title,
80             'thumbnail': self._og_search_thumbnail(webpage),
81             'description': videoDesc,
82             'player_url': playerUrl,
83         }