[vvvvid] Fix typo
[youtube-dl] / youtube_dl / extractor / vvvvid.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     int_or_none,
10     str_or_none,
11 )
12
13
14 class VVVVIDIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?vvvvid\.it/#!(?:show|anime|film|series)/(?P<show_id>\d+)/[^/]+/(?P<season_id>\d+)/(?P<id>[0-9]+)'
16     _TESTS = [{
17         # video_type == 'video/vvvvid'
18         'url': 'https://www.vvvvid.it/#!show/434/perche-dovrei-guardarlo-di-dario-moccia/437/489048/ping-pong',
19         'md5': 'b8d3cecc2e981adc3835adf07f6df91b',
20         'info_dict': {
21             'id': '489048',
22             'ext': 'mp4',
23             'title': 'Ping Pong',
24         },
25     }, {
26         # video_type == 'video/rcs'
27         'url': 'https://www.vvvvid.it/#!show/376/death-note-live-action/377/482493/episodio-01',
28         'md5': '33e0edfba720ad73a8782157fdebc648',
29         'info_dict': {
30             'id': '482493',
31             'ext': 'mp4',
32             'title': 'Episodio 01',
33         },
34     }]
35     _conn_id = None
36
37     def _real_initialize(self):
38         self._conn_id = self._download_json(
39             'https://www.vvvvid.it/user/login',
40             None, headers=self.geo_verification_headers())['data']['conn_id']
41
42     def _real_extract(self, url):
43         show_id, season_id, video_id = re.match(self._VALID_URL, url).groups()
44         response = self._download_json(
45             'https://www.vvvvid.it/vvvvid/ondemand/%s/season/%s' % (show_id, season_id),
46             video_id, headers=self.geo_verification_headers(), query={
47                 'conn_id': self._conn_id,
48             })
49         if response['result'] == 'error':
50             raise ExtractorError('%s said: %s' % (
51                 self.IE_NAME, response['message']), expected=True)
52
53         vid = int(video_id)
54         video_data = list(filter(
55             lambda episode: episode.get('video_id') == vid, response['data']))[0]
56         formats = []
57
58         # vvvvid embed_info decryption algorithm is reverse engineered from function $ds(h) at vvvvid.js
59         def ds(h):
60             g = "MNOPIJKL89+/4567UVWXQRSTEFGHABCDcdefYZabstuvopqr0123wxyzklmnghij"
61
62             def f(m):
63                 l = []
64                 o = 0
65                 b = False
66                 m_len = len(m)
67                 while ((not b) and o < m_len):
68                     n = m[o] << 2
69                     o += 1
70                     k = -1
71                     j = -1
72                     if o < m_len:
73                         n += m[o] >> 4
74                         o += 1
75                         if o < m_len:
76                             k = (m[o - 1] << 4) & 255
77                             k += m[o] >> 2
78                             o += 1
79                             if o < m_len:
80                                 j = (m[o - 1] << 6) & 255
81                                 j += m[o]
82                                 o += 1
83                             else:
84                                 b = True
85                         else:
86                             b = True
87                     else:
88                         b = True
89                     l.append(n)
90                     if k != -1:
91                         l.append(k)
92                     if j != -1:
93                         l.append(j)
94                 return l
95
96             c = []
97             for e in h:
98                 c.append(g.index(e))
99
100             c_len = len(c)
101             for e in range(c_len * 2 - 1, -1, -1):
102                 a = c[e % c_len] ^ c[(e + 1) % c_len]
103                 c[e % c_len] = a
104
105             c = f(c)
106             d = ''
107             for e in c:
108                 d += chr(e)
109
110             return d
111
112         for quality in ('_sd', ''):
113             embed_code = video_data.get('embed_info' + quality)
114             if not embed_code:
115                 continue
116             embed_code = ds(embed_code)
117             video_type = video_data.get('video_type')
118             if video_type in ('video/rcs', 'video/kenc'):
119                 formats.extend(self._extract_akamai_formats(
120                     embed_code, video_id))
121             else:
122                 formats.extend(self._extract_wowza_formats(
123                     'http://sb.top-ix.org/videomg/_definst_/mp4:%s/playlist.m3u8' % embed_code, video_id))
124         self._sort_formats(formats)
125
126         return {
127             'id': video_id,
128             'title': video_data['title'],
129             'formats': formats,
130             'thumbnail': video_data.get('thumbnail'),
131             'duration': int_or_none(video_data.get('length')),
132             'series': video_data.get('show_title'),
133             'season_id': season_id,
134             'season_number': video_data.get('season_number'),
135             'episode_id': str_or_none(video_data.get('id')),
136             'episode_number': int_or_none(video_data.get('number')),
137             'episode_title': video_data['title'],
138             'view_count': int_or_none(video_data.get('views')),
139             'like_count': int_or_none(video_data.get('video_likes')),
140         }