[vvvvid] Add new extractor(closes #5915)
[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         if not self._conn_id:
39             user = self._downloader.cache.load('vvvvid', 'user') or {}
40             self._conn_id = user.get('conn_id')
41             if not self._conn_id:
42                 self._conn_id = self._download_json(
43                     'https://www.vvvvid.it/user/login',
44                     None, headers=self.geo_verification_headers())['data']['conn_id']
45                 self._downloader.cache.store(
46                     'vvvvid', 'user', {
47                         'conn_id': self._conn_id,
48                     })
49
50     def _real_extract(self, url):
51         show_id, season_id, video_id = re.match(self._VALID_URL, url).groups()
52         response = self._download_json(
53             'https://www.vvvvid.it/vvvvid/ondemand/%s/season/%s' % (show_id, season_id),
54             video_id, headers=self.geo_verification_headers(), query={
55                 'conn_id': self._conn_id,
56             })
57         if response['result'] == 'error':
58             raise ExtractorError('%s said: %s' % (
59                 self.IE_NAME, response['message']), expected=True)
60
61         vid = int(video_id)
62         video_data = list(filter(
63             lambda episode: episode.get('video_id') == vid, response['data']))[0]
64         formats = []
65
66         # vvvvid embed_info decryption algorithm is reverse engineered from function $ds(h) at vvvvid.js
67         def ds(h):
68             g = "MNOPIJKL89+/4567UVWXQRSTEFGHABCDcdefYZabstuvopqr0123wxyzklmnghij"
69
70             def f(m):
71                 l = []
72                 o = 0
73                 b = False
74                 m_len = len(m)
75                 while ((not b) and o < m_len):
76                     n = m[o] << 2
77                     o += 1
78                     k = -1
79                     j = -1
80                     if o < m_len:
81                         n += m[o] >> 4
82                         o += 1
83                         if o < m_len:
84                             k = (m[o - 1] << 4) & 255
85                             k += m[o] >> 2
86                             o += 1
87                             if o < m_len:
88                                 j = (m[o - 1] << 6) & 255
89                                 j += m[o]
90                                 o += 1
91                             else:
92                                 b = True
93                         else:
94                             b = True
95                     else:
96                         b = True
97                     l.append(n)
98                     if k != -1:
99                         l.append(k)
100                     if j != -1:
101                         l.append(j)
102                 return l
103
104             c = []
105             for e in h:
106                 c.append(g.index(e))
107
108             c_len = len(c)
109             for e in range(c_len * 2 - 1, -1, -1):
110                 a = c[e % c_len] ^ c[(e + 1) % c_len]
111                 c[e % c_len] = a
112
113             c = f(c)
114             d = ''
115             for e in c:
116                 d += chr(e)
117
118             return d
119
120         for quality in ('_sd', ''):
121             embed_code = video_data.get('embed_info' + quality)
122             if not embed_code:
123                 continue
124             embed_code = ds(embed_code)
125             video_type = video_data.get('video_type')
126             if video_type in ('video/rcs', 'video/kenc'):
127                 formats.extend(self._extract_akamai_formats(
128                     embed_code, video_id))
129             else:
130                 formats.extend(self._extract_wowza_formats(
131                     'http://sb.top-ix.org/videomg/_definst_/mp4:%s/playlist.m3u8' % embed_code, video_id))
132         self._sort_formats(formats)
133
134         return {
135             'id': video_id,
136             'title': video_data['title'],
137             'formats': formats,
138             'thumbnail': video_data.get('thumbnail'),
139             'duration': int_or_none(video_data.get('length')),
140             'series': video_data.get('show_title'),
141             'season_id': season_id,
142             'season_number': video_data.get('season_number'),
143             'episode_id': str_or_none(video_data.get('id')),
144             'epidode_number': int_or_none(video_data.get('number')),
145             'episode_title': video_data['title'],
146             'view_count': int_or_none(video_data.get('views')),
147             'like_count': int_or_none(video_data.get('video_likes')),
148         }