[escapist] Fix extractor (fixes #5090)
[youtube-dl] / youtube_dl / extractor / escapist.py
1 from __future__ import unicode_literals
2
3 import json
4
5 from .common import InfoExtractor
6 from ..compat import compat_urllib_request
7
8 from ..utils import (
9     determine_ext,
10     clean_html,
11     qualities,
12 )
13
14
15 def _decrypt_config(key, string):
16     a = ''
17     i = ''
18     r = ''
19
20     while len(a) < (len(string) / 2):
21         a += key
22
23     a = a[0:int(len(string) / 2)]
24
25     t = 0
26     while t < len(string):
27         i += chr(int(string[t] + string[t + 1], 16))
28         t += 2
29
30     icko = [s for s in i]
31
32     for t, c in enumerate(a):
33         r += chr(ord(c) ^ ord(icko[t]))
34
35     return r
36
37
38 class EscapistIE(InfoExtractor):
39     _VALID_URL = r'https?://?(www\.)?escapistmagazine\.com/videos/view/[^/?#]+/(?P<id>[0-9]+)-[^/?#]*(?:$|[?#])'
40     _TESTS = [{
41         'url': 'http://www.escapistmagazine.com/videos/view/the-escapist-presents/6618-Breaking-Down-Baldurs-Gate',
42         'md5': 'c6793dbda81388f4264c1ba18684a74d',
43         'info_dict': {
44             'id': '6618',
45             'ext': 'mp4',
46             '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.",
47             'title': "Breaking Down Baldur's Gate",
48             'thumbnail': 're:^https?://.*\.jpg$',
49             'duration': 264,
50         }
51     }, {
52         'url': 'http://www.escapistmagazine.com/videos/view/zero-punctuation/10044-Evolve-One-vs-Multiplayer',
53         'md5': 'cf8842a8a46444d241f9a9980d7874f2',
54         'info_dict': {
55             'id': '10044',
56             'ext': 'mp4',
57             'description': 'This week, Zero Punctuation reviews Evolve.',
58             'title': 'Evolve - One vs Multiplayer',
59             'thumbnail': 're:^https?://.*\.jpg$',
60             'duration': 304,
61         }
62     }]
63
64     def _real_extract(self, url):
65         video_id = self._match_id(url)
66         webpage = self._download_webpage(url, video_id)
67
68         imsVideo = json.loads(self._search_regex(r'imsVideo\.play\(([^\)]+)\);', webpage, 'imsVideo'))
69         video_id = imsVideo['videoID']
70         key = imsVideo['hash']
71
72         quality = qualities(['lq', 'hq', 'hd'])
73
74         formats = []
75         for q in ['lq', 'hq', 'hd']:
76             config_req = compat_urllib_request.Request('http://www.escapistmagazine.com/videos/'
77                 'vidconfig.php?videoID=%s&hash=%s&quality=%s' % (video_id, key, 'mp4_' + q))
78             config_req.add_header('Referer', url)
79             config = self._download_webpage(config_req, video_id, 'Downloading video config ' + q.upper())
80
81             data = json.loads(_decrypt_config(key, config))
82
83             title = clean_html(data['videoData']['title'])
84             duration = data['videoData']['duration'] / 1000
85
86             for i, v in enumerate(data['files']['videos']):
87
88                 formats.append({
89                     'url': v,
90                     'format_id': determine_ext(v) + '_' + q + str(i),
91                     'quality': quality(q),
92                     })
93
94
95         return {
96             'id': video_id,
97             'formats': formats,
98             'title': title,
99             'thumbnail': self._og_search_thumbnail(webpage),
100             'description': self._og_search_description(webpage),
101             'duration': duration,
102         }