Merge branch 'master' of github.com:rg3/youtube-dl
[youtube-dl] / youtube_dl / extractor / escapist.py
1 import json
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_str,
7     compat_urllib_parse,
8
9     ExtractorError,
10 )
11
12
13 class EscapistIE(InfoExtractor):
14     _VALID_URL = r'^(https?://)?(www\.)?escapistmagazine\.com/videos/view/(?P<showname>[^/]+)/(?P<episode>[^/?]+)[/?]?.*$'
15     _TEST = {
16         u'url': u'http://www.escapistmagazine.com/videos/view/the-escapist-presents/6618-Breaking-Down-Baldurs-Gate',
17         u'file': u'6618-Breaking-Down-Baldurs-Gate.mp4',
18         u'md5': u'c6793dbda81388f4264c1ba18684a74d',
19         u'info_dict': {
20             u"description": u"Baldur's Gate: Original, Modded or Enhanced Edition? I'll break down what you can expect from the new Baldur's Gate: Enhanced Edition.", 
21             u"uploader": u"the-escapist-presents", 
22             u"title": u"Breaking Down Baldur's Gate"
23         }
24     }
25
26     def _real_extract(self, url):
27         mobj = re.match(self._VALID_URL, url)
28         if mobj is None:
29             raise ExtractorError(u'Invalid URL: %s' % url)
30         showName = mobj.group('showname')
31         videoId = mobj.group('episode')
32
33         self.report_extraction(videoId)
34         webpage = self._download_webpage(url, videoId)
35
36         videoDesc = self._html_search_regex('<meta name="description" content="([^"]*)"',
37             webpage, u'description', fatal=False)
38
39         imgUrl = self._html_search_regex('<meta property="og:image" content="([^"]*)"',
40             webpage, u'thumbnail', fatal=False)
41
42         playerUrl = self._html_search_regex('<meta property="og:video" content="([^"]*)"',
43             webpage, u'player url')
44
45         title = self._html_search_regex('<meta name="title" content="([^"]*)"',
46             webpage, u'player url').split(' : ')[-1]
47
48         configUrl = self._search_regex('config=(.*)$', playerUrl, u'config url')
49         configUrl = compat_urllib_parse.unquote(configUrl)
50
51         configJSON = self._download_webpage(configUrl, videoId,
52                                             u'Downloading configuration',
53                                             u'unable to download configuration')
54
55         # Technically, it's JavaScript, not JSON
56         configJSON = configJSON.replace("'", '"')
57
58         try:
59             config = json.loads(configJSON)
60         except (ValueError,) as err:
61             raise ExtractorError(u'Invalid JSON in configuration file: ' + compat_str(err))
62
63         playlist = config['playlist']
64         videoUrl = playlist[1]['url']
65
66         info = {
67             'id': videoId,
68             'url': videoUrl,
69             'uploader': showName,
70             'upload_date': None,
71             'title': title,
72             'ext': 'mp4',
73             'thumbnail': imgUrl,
74             'description': videoDesc,
75             'player_url': playerUrl,
76         }
77
78         return [info]