logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

vvvvid.py (10826B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from .youtube import YoutubeIE
  6. from ..utils import (
  7. ExtractorError,
  8. int_or_none,
  9. str_or_none,
  10. )
  11. class VVVVIDIE(InfoExtractor):
  12. _VALID_URL_BASE = r'https?://(?:www\.)?vvvvid\.it/(?:#!)?(?:show|anime|film|series)/'
  13. _VALID_URL = r'%s(?P<show_id>\d+)/[^/]+/(?P<season_id>\d+)/(?P<id>[0-9]+)' % _VALID_URL_BASE
  14. _TESTS = [{
  15. # video_type == 'video/vvvvid'
  16. 'url': 'https://www.vvvvid.it/#!show/434/perche-dovrei-guardarlo-di-dario-moccia/437/489048/ping-pong',
  17. 'md5': 'b8d3cecc2e981adc3835adf07f6df91b',
  18. 'info_dict': {
  19. 'id': '489048',
  20. 'ext': 'mp4',
  21. 'title': 'Ping Pong',
  22. 'duration': 239,
  23. 'series': '"Perché dovrei guardarlo?" di Dario Moccia',
  24. 'season_id': '437',
  25. 'episode': 'Ping Pong',
  26. 'episode_number': 1,
  27. 'episode_id': '3334',
  28. 'view_count': int,
  29. 'like_count': int,
  30. 'repost_count': int,
  31. },
  32. 'params': {
  33. 'skip_download': True,
  34. },
  35. }, {
  36. # video_type == 'video/rcs'
  37. 'url': 'https://www.vvvvid.it/#!show/376/death-note-live-action/377/482493/episodio-01',
  38. 'md5': '33e0edfba720ad73a8782157fdebc648',
  39. 'info_dict': {
  40. 'id': '482493',
  41. 'ext': 'mp4',
  42. 'title': 'Episodio 01',
  43. },
  44. 'params': {
  45. 'skip_download': True,
  46. },
  47. }, {
  48. # video_type == 'video/youtube'
  49. 'url': 'https://www.vvvvid.it/show/404/one-punch-man/406/486683/trailer',
  50. 'md5': '33e0edfba720ad73a8782157fdebc648',
  51. 'info_dict': {
  52. 'id': 'RzmFKUDOUgw',
  53. 'ext': 'mp4',
  54. 'title': 'Trailer',
  55. 'upload_date': '20150906',
  56. 'description': 'md5:a5e802558d35247fee285875328c0b80',
  57. 'uploader_id': 'BandaiVisual',
  58. 'uploader': 'BANDAI NAMCO Arts Channel',
  59. },
  60. 'params': {
  61. 'skip_download': True,
  62. },
  63. }, {
  64. # video_type == 'video/dash'
  65. 'url': 'https://www.vvvvid.it/show/683/made-in-abyss/1542/693786/nanachi',
  66. 'info_dict': {
  67. 'id': '693786',
  68. 'ext': 'mp4',
  69. 'title': 'Nanachi',
  70. },
  71. 'params': {
  72. 'skip_download': True,
  73. 'format': 'mp4',
  74. },
  75. }, {
  76. 'url': 'https://www.vvvvid.it/show/434/perche-dovrei-guardarlo-di-dario-moccia/437/489048',
  77. 'only_matching': True
  78. }]
  79. _conn_id = None
  80. def _real_initialize(self):
  81. self._conn_id = self._download_json(
  82. 'https://www.vvvvid.it/user/login',
  83. None, headers=self.geo_verification_headers())['data']['conn_id']
  84. def _download_info(self, show_id, path, video_id, fatal=True, query=None):
  85. q = {
  86. 'conn_id': self._conn_id,
  87. }
  88. if query:
  89. q.update(query)
  90. response = self._download_json(
  91. 'https://www.vvvvid.it/vvvvid/ondemand/%s/%s' % (show_id, path),
  92. video_id, headers=self.geo_verification_headers(), query=q, fatal=fatal)
  93. if not (response or fatal):
  94. return
  95. if response.get('result') == 'error':
  96. raise ExtractorError('%s said: %s' % (
  97. self.IE_NAME, response['message']), expected=True)
  98. return response['data']
  99. def _extract_common_video_info(self, video_data):
  100. return {
  101. 'thumbnail': video_data.get('thumbnail'),
  102. 'episode_id': str_or_none(video_data.get('id')),
  103. }
  104. def _real_extract(self, url):
  105. show_id, season_id, video_id = re.match(self._VALID_URL, url).groups()
  106. response = self._download_info(
  107. show_id, 'season/%s' % season_id,
  108. video_id, query={'video_id': video_id})
  109. vid = int(video_id)
  110. video_data = list(filter(
  111. lambda episode: episode.get('video_id') == vid, response))[0]
  112. title = video_data['title']
  113. formats = []
  114. # vvvvid embed_info decryption algorithm is reverse engineered from function $ds(h) at vvvvid.js
  115. def ds(h):
  116. g = "MNOPIJKL89+/4567UVWXQRSTEFGHABCDcdefYZabstuvopqr0123wxyzklmnghij"
  117. def f(m):
  118. l = []
  119. o = 0
  120. b = False
  121. m_len = len(m)
  122. while ((not b) and o < m_len):
  123. n = m[o] << 2
  124. o += 1
  125. k = -1
  126. j = -1
  127. if o < m_len:
  128. n += m[o] >> 4
  129. o += 1
  130. if o < m_len:
  131. k = (m[o - 1] << 4) & 255
  132. k += m[o] >> 2
  133. o += 1
  134. if o < m_len:
  135. j = (m[o - 1] << 6) & 255
  136. j += m[o]
  137. o += 1
  138. else:
  139. b = True
  140. else:
  141. b = True
  142. else:
  143. b = True
  144. l.append(n)
  145. if k != -1:
  146. l.append(k)
  147. if j != -1:
  148. l.append(j)
  149. return l
  150. c = []
  151. for e in h:
  152. c.append(g.index(e))
  153. c_len = len(c)
  154. for e in range(c_len * 2 - 1, -1, -1):
  155. a = c[e % c_len] ^ c[(e + 1) % c_len]
  156. c[e % c_len] = a
  157. c = f(c)
  158. d = ''
  159. for e in c:
  160. d += chr(e)
  161. return d
  162. info = {}
  163. def metadata_from_url(r_url):
  164. if not info and r_url:
  165. mobj = re.search(r'_(?:S(\d+))?Ep(\d+)', r_url)
  166. if mobj:
  167. info['episode_number'] = int(mobj.group(2))
  168. season_number = mobj.group(1)
  169. if season_number:
  170. info['season_number'] = int(season_number)
  171. video_type = video_data.get('video_type')
  172. is_youtube = False
  173. for quality in ('', '_sd'):
  174. embed_code = video_data.get('embed_info' + quality)
  175. if not embed_code:
  176. continue
  177. embed_code = ds(embed_code)
  178. if video_type == 'video/kenc':
  179. embed_code = re.sub(r'https?(://[^/]+)/z/', r'https\1/i/', embed_code).replace('/manifest.f4m', '/master.m3u8')
  180. kenc = self._download_json(
  181. 'https://www.vvvvid.it/kenc', video_id, query={
  182. 'action': 'kt',
  183. 'conn_id': self._conn_id,
  184. 'url': embed_code,
  185. }, fatal=False) or {}
  186. kenc_message = kenc.get('message')
  187. if kenc_message:
  188. embed_code += '?' + ds(kenc_message)
  189. formats.extend(self._extract_m3u8_formats(
  190. embed_code, video_id, 'mp4', m3u8_id='hls', fatal=False))
  191. elif video_type == 'video/rcs':
  192. formats.extend(self._extract_akamai_formats(embed_code, video_id))
  193. elif video_type == 'video/youtube':
  194. info.update({
  195. '_type': 'url_transparent',
  196. 'ie_key': YoutubeIE.ie_key(),
  197. 'url': embed_code,
  198. })
  199. is_youtube = True
  200. break
  201. elif video_type == 'video/dash':
  202. formats.extend(self._extract_m3u8_formats(
  203. embed_code, video_id, 'mp4', m3u8_id='hls', fatal=False))
  204. else:
  205. formats.extend(self._extract_wowza_formats(
  206. 'http://sb.top-ix.org/videomg/_definst_/mp4:%s/playlist.m3u8' % embed_code, video_id))
  207. metadata_from_url(embed_code)
  208. if not is_youtube:
  209. self._sort_formats(formats)
  210. info['formats'] = formats
  211. metadata_from_url(video_data.get('thumbnail'))
  212. info.update(self._extract_common_video_info(video_data))
  213. info.update({
  214. 'id': video_id,
  215. 'title': title,
  216. 'duration': int_or_none(video_data.get('length')),
  217. 'series': video_data.get('show_title'),
  218. 'season_id': season_id,
  219. 'episode': title,
  220. 'view_count': int_or_none(video_data.get('views')),
  221. 'like_count': int_or_none(video_data.get('video_likes')),
  222. 'repost_count': int_or_none(video_data.get('video_shares')),
  223. })
  224. return info
  225. class VVVVIDShowIE(VVVVIDIE):
  226. _VALID_URL = r'(?P<base_url>%s(?P<id>\d+)(?:/(?P<show_title>[^/?&#]+))?)/?(?:[?#&]|$)' % VVVVIDIE._VALID_URL_BASE
  227. _TESTS = [{
  228. 'url': 'https://www.vvvvid.it/show/156/psyco-pass',
  229. 'info_dict': {
  230. 'id': '156',
  231. 'title': 'Psycho-Pass',
  232. 'description': 'md5:94d572c0bd85894b193b8aebc9a3a806',
  233. },
  234. 'playlist_count': 46,
  235. }, {
  236. 'url': 'https://www.vvvvid.it/show/156',
  237. 'only_matching': True,
  238. }]
  239. def _real_extract(self, url):
  240. base_url, show_id, show_title = re.match(self._VALID_URL, url).groups()
  241. seasons = self._download_info(
  242. show_id, 'seasons/', show_title)
  243. show_info = self._download_info(
  244. show_id, 'info/', show_title, fatal=False)
  245. if not show_title:
  246. base_url += "/title"
  247. entries = []
  248. for season in (seasons or []):
  249. episodes = season.get('episodes') or []
  250. playlist_title = season.get('name') or show_info.get('title')
  251. for episode in episodes:
  252. if episode.get('playable') is False:
  253. continue
  254. season_id = str_or_none(episode.get('season_id'))
  255. video_id = str_or_none(episode.get('video_id'))
  256. if not (season_id and video_id):
  257. continue
  258. info = self._extract_common_video_info(episode)
  259. info.update({
  260. '_type': 'url_transparent',
  261. 'ie_key': VVVVIDIE.ie_key(),
  262. 'url': '/'.join([base_url, season_id, video_id]),
  263. 'title': episode.get('title'),
  264. 'description': episode.get('description'),
  265. 'season_id': season_id,
  266. 'playlist_title': playlist_title,
  267. })
  268. entries.append(info)
  269. return self.playlist_result(
  270. entries, show_id, show_info.get('title'), show_info.get('description'))