[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / escapist.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import (
5     determine_ext,
6     clean_html,
7     int_or_none,
8     float_or_none,
9 )
10
11
12 def _decrypt_config(key, string):
13     a = ''
14     i = ''
15     r = ''
16
17     while len(a) < (len(string) / 2):
18         a += key
19
20     a = a[0:int(len(string) / 2)]
21
22     t = 0
23     while t < len(string):
24         i += chr(int(string[t] + string[t + 1], 16))
25         t += 2
26
27     icko = [s for s in i]
28
29     for t, c in enumerate(a):
30         r += chr(ord(c) ^ ord(icko[t]))
31
32     return r
33
34
35 class EscapistIE(InfoExtractor):
36     _VALID_URL = r'https?://?(?:(?:www|v1)\.)?escapistmagazine\.com/videos/view/[^/]+/(?P<id>[0-9]+)'
37     _TESTS = [{
38         'url': 'http://www.escapistmagazine.com/videos/view/the-escapist-presents/6618-Breaking-Down-Baldurs-Gate',
39         'md5': 'ab3a706c681efca53f0a35f1415cf0d1',
40         'info_dict': {
41             'id': '6618',
42             'ext': 'mp4',
43             '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.",
44             'title': "Breaking Down Baldur's Gate",
45             'thumbnail': r're:^https?://.*\.jpg$',
46             'duration': 264,
47             'uploader': 'The Escapist',
48         }
49     }, {
50         'url': 'http://www.escapistmagazine.com/videos/view/zero-punctuation/10044-Evolve-One-vs-Multiplayer',
51         'md5': '9e8c437b0dbb0387d3bd3255ca77f6bf',
52         'info_dict': {
53             'id': '10044',
54             'ext': 'mp4',
55             'description': 'This week, Zero Punctuation reviews Evolve.',
56             'title': 'Evolve - One vs Multiplayer',
57             'thumbnail': r're:^https?://.*\.jpg$',
58             'duration': 304,
59             'uploader': 'The Escapist',
60         }
61     }, {
62         'url': 'http://escapistmagazine.com/videos/view/the-escapist-presents/6618',
63         'only_matching': True,
64     }, {
65         'url': 'https://v1.escapistmagazine.com/videos/view/the-escapist-presents/6618-Breaking-Down-Baldurs-Gate',
66         'only_matching': True,
67     }]
68
69     def _real_extract(self, url):
70         video_id = self._match_id(url)
71         webpage = self._download_webpage(url, video_id)
72
73         ims_video = self._parse_json(
74             self._search_regex(
75                 r'imsVideo\.play\(({.+?})\);', webpage, 'imsVideo'),
76             video_id)
77         video_id = ims_video['videoID']
78         key = ims_video['hash']
79
80         config = self._download_webpage(
81             'http://www.escapistmagazine.com/videos/vidconfig.php',
82             video_id, 'Downloading video config', headers={
83                 'Referer': url,
84             }, query={
85                 'videoID': video_id,
86                 'hash': key,
87             })
88
89         data = self._parse_json(_decrypt_config(key, config), video_id)
90
91         video_data = data['videoData']
92
93         title = clean_html(video_data['title'])
94
95         formats = [{
96             'url': video['src'],
97             'format_id': '%s-%sp' % (determine_ext(video['src']), video['res']),
98             'height': int_or_none(video.get('res')),
99         } for video in data['files']['videos']]
100         self._sort_formats(formats)
101
102         return {
103             'id': video_id,
104             'formats': formats,
105             'title': title,
106             'thumbnail': self._og_search_thumbnail(webpage) or data.get('poster'),
107             'description': self._og_search_description(webpage),
108             'duration': float_or_none(video_data.get('duration'), 1000),
109             'uploader': video_data.get('publisher'),
110             'series': video_data.get('show'),
111         }