]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/vvvvid.py
fix compat_urllib_request for python2.7
[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         'params': {
26             'skip_download': True,
27         },
28     }, {
29         # video_type == 'video/rcs'
30         'url': 'https://www.vvvvid.it/#!show/376/death-note-live-action/377/482493/episodio-01',
31         'md5': '33e0edfba720ad73a8782157fdebc648',
32         'info_dict': {
33             'id': '482493',
34             'ext': 'mp4',
35             'title': 'Episodio 01',
36         },
37         'params': {
38             'skip_download': True,
39         },
40     }]
41     _conn_id = None
42
43     def _real_initialize(self):
44         self._conn_id = self._download_json(
45             'https://www.vvvvid.it/user/login',
46             None, headers=self.geo_verification_headers())['data']['conn_id']
47
48     def _real_extract(self, url):
49         show_id, season_id, video_id = re.match(self._VALID_URL, url).groups()
50         response = self._download_json(
51             'https://www.vvvvid.it/vvvvid/ondemand/%s/season/%s' % (show_id, season_id),
52             video_id, headers=self.geo_verification_headers(), query={
53                 'conn_id': self._conn_id,
54             })
55         if response['result'] == 'error':
56             raise ExtractorError('%s said: %s' % (
57                 self.IE_NAME, response['message']), expected=True)
58
59         vid = int(video_id)
60         video_data = list(filter(
61             lambda episode: episode.get('video_id') == vid, response['data']))[0]
62         formats = []
63
64         # vvvvid embed_info decryption algorithm is reverse engineered from function $ds(h) at vvvvid.js
65         def ds(h):
66             g = "MNOPIJKL89+/4567UVWXQRSTEFGHABCDcdefYZabstuvopqr0123wxyzklmnghij"
67
68             def f(m):
69                 l = []
70                 o = 0
71                 b = False
72                 m_len = len(m)
73                 while ((not b) and o < m_len):
74                     n = m[o] << 2
75                     o += 1
76                     k = -1
77                     j = -1
78                     if o < m_len:
79                         n += m[o] >> 4
80                         o += 1
81                         if o < m_len:
82                             k = (m[o - 1] << 4) & 255
83                             k += m[o] >> 2
84                             o += 1
85                             if o < m_len:
86                                 j = (m[o - 1] << 6) & 255
87                                 j += m[o]
88                                 o += 1
89                             else:
90                                 b = True
91                         else:
92                             b = True
93                     else:
94                         b = True
95                     l.append(n)
96                     if k != -1:
97                         l.append(k)
98                     if j != -1:
99                         l.append(j)
100                 return l
101
102             c = []
103             for e in h:
104                 c.append(g.index(e))
105
106             c_len = len(c)
107             for e in range(c_len * 2 - 1, -1, -1):
108                 a = c[e % c_len] ^ c[(e + 1) % c_len]
109                 c[e % c_len] = a
110
111             c = f(c)
112             d = ''
113             for e in c:
114                 d += chr(e)
115
116             return d
117
118         for quality in ('_sd', ''):
119             embed_code = video_data.get('embed_info' + quality)
120             if not embed_code:
121                 continue
122             embed_code = ds(embed_code)
123             video_type = video_data.get('video_type')
124             if video_type in ('video/rcs', 'video/kenc'):
125                 embed_code = re.sub(r'https?://([^/]+)/z/', r'https://\1/i/', embed_code).replace('/manifest.f4m', '/master.m3u8')
126                 if video_type == 'video/kenc':
127                     kenc = self._download_json(
128                         'https://www.vvvvid.it/kenc', video_id, query={
129                             'action': 'kt',
130                             'conn_id': self._conn_id,
131                             'url': embed_code,
132                         }, fatal=False) or {}
133                     kenc_message = kenc.get('message')
134                     if kenc_message:
135                         embed_code += '?' + ds(kenc_message)
136                 formats.extend(self._extract_m3u8_formats(
137                     embed_code, video_id, 'mp4',
138                     m3u8_id='hls', fatal=False))
139             else:
140                 formats.extend(self._extract_wowza_formats(
141                     'http://sb.top-ix.org/videomg/_definst_/mp4:%s/playlist.m3u8' % embed_code, video_id))
142         self._sort_formats(formats)
143
144         return {
145             'id': video_id,
146             'title': video_data['title'],
147             'formats': formats,
148             'thumbnail': video_data.get('thumbnail'),
149             'duration': int_or_none(video_data.get('length')),
150             'series': video_data.get('show_title'),
151             'season_id': season_id,
152             'season_number': video_data.get('season_number'),
153             'episode_id': str_or_none(video_data.get('id')),
154             'episode_number': int_or_none(video_data.get('number')),
155             'episode_title': video_data['title'],
156             'view_count': int_or_none(video_data.get('views')),
157             'like_count': int_or_none(video_data.get('video_likes')),
158         }