2
0

vvvvid.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. 'url': 'https://www.vvvvid.it/show/434/perche-dovrei-guardarlo-di-dario-moccia/437/489048',
  65. 'only_matching': True
  66. }]
  67. _conn_id = None
  68. def _real_initialize(self):
  69. self._conn_id = self._download_json(
  70. 'https://www.vvvvid.it/user/login',
  71. None, headers=self.geo_verification_headers())['data']['conn_id']
  72. def _download_info(self, show_id, path, video_id, fatal=True):
  73. response = self._download_json(
  74. 'https://www.vvvvid.it/vvvvid/ondemand/%s/%s' % (show_id, path),
  75. video_id, headers=self.geo_verification_headers(), query={
  76. 'conn_id': self._conn_id,
  77. }, fatal=fatal)
  78. if not (response or fatal):
  79. return
  80. if response.get('result') == 'error':
  81. raise ExtractorError('%s said: %s' % (
  82. self.IE_NAME, response['message']), expected=True)
  83. return response['data']
  84. def _extract_common_video_info(self, video_data):
  85. return {
  86. 'thumbnail': video_data.get('thumbnail'),
  87. 'episode_id': str_or_none(video_data.get('id')),
  88. }
  89. def _real_extract(self, url):
  90. show_id, season_id, video_id = re.match(self._VALID_URL, url).groups()
  91. response = self._download_info(
  92. show_id, 'season/%s' % season_id, video_id)
  93. vid = int(video_id)
  94. video_data = list(filter(
  95. lambda episode: episode.get('video_id') == vid, response))[0]
  96. title = video_data['title']
  97. formats = []
  98. # vvvvid embed_info decryption algorithm is reverse engineered from function $ds(h) at vvvvid.js
  99. def ds(h):
  100. g = "MNOPIJKL89+/4567UVWXQRSTEFGHABCDcdefYZabstuvopqr0123wxyzklmnghij"
  101. def f(m):
  102. l = []
  103. o = 0
  104. b = False
  105. m_len = len(m)
  106. while ((not b) and o < m_len):
  107. n = m[o] << 2
  108. o += 1
  109. k = -1
  110. j = -1
  111. if o < m_len:
  112. n += m[o] >> 4
  113. o += 1
  114. if o < m_len:
  115. k = (m[o - 1] << 4) & 255
  116. k += m[o] >> 2
  117. o += 1
  118. if o < m_len:
  119. j = (m[o - 1] << 6) & 255
  120. j += m[o]
  121. o += 1
  122. else:
  123. b = True
  124. else:
  125. b = True
  126. else:
  127. b = True
  128. l.append(n)
  129. if k != -1:
  130. l.append(k)
  131. if j != -1:
  132. l.append(j)
  133. return l
  134. c = []
  135. for e in h:
  136. c.append(g.index(e))
  137. c_len = len(c)
  138. for e in range(c_len * 2 - 1, -1, -1):
  139. a = c[e % c_len] ^ c[(e + 1) % c_len]
  140. c[e % c_len] = a
  141. c = f(c)
  142. d = ''
  143. for e in c:
  144. d += chr(e)
  145. return d
  146. info = {}
  147. def metadata_from_url(r_url):
  148. if not info and r_url:
  149. mobj = re.search(r'_(?:S(\d+))?Ep(\d+)', r_url)
  150. if mobj:
  151. info['episode_number'] = int(mobj.group(2))
  152. season_number = mobj.group(1)
  153. if season_number:
  154. info['season_number'] = int(season_number)
  155. video_type = video_data.get('video_type')
  156. is_youtube = False
  157. for quality in ('', '_sd'):
  158. embed_code = video_data.get('embed_info' + quality)
  159. if not embed_code:
  160. continue
  161. embed_code = ds(embed_code)
  162. if video_type in ('video/rcs', 'video/kenc'):
  163. if video_type == 'video/kenc':
  164. kenc = self._download_json(
  165. 'https://www.vvvvid.it/kenc', video_id, query={
  166. 'action': 'kt',
  167. 'conn_id': self._conn_id,
  168. 'url': embed_code,
  169. }, fatal=False) or {}
  170. kenc_message = kenc.get('message')
  171. if kenc_message:
  172. embed_code += '?' + ds(kenc_message)
  173. formats.extend(self._extract_akamai_formats(embed_code, video_id))
  174. elif video_type == 'video/youtube':
  175. info.update({
  176. '_type': 'url_transparent',
  177. 'ie_key': YoutubeIE.ie_key(),
  178. 'url': embed_code,
  179. })
  180. is_youtube = True
  181. break
  182. else:
  183. formats.extend(self._extract_wowza_formats(
  184. 'http://sb.top-ix.org/videomg/_definst_/mp4:%s/playlist.m3u8' % embed_code, video_id))
  185. metadata_from_url(embed_code)
  186. if not is_youtube:
  187. self._sort_formats(formats)
  188. info['formats'] = formats
  189. metadata_from_url(video_data.get('thumbnail'))
  190. info.update(self._extract_common_video_info(video_data))
  191. info.update({
  192. 'id': video_id,
  193. 'title': title,
  194. 'duration': int_or_none(video_data.get('length')),
  195. 'series': video_data.get('show_title'),
  196. 'season_id': season_id,
  197. 'episode': title,
  198. 'view_count': int_or_none(video_data.get('views')),
  199. 'like_count': int_or_none(video_data.get('video_likes')),
  200. 'repost_count': int_or_none(video_data.get('video_shares')),
  201. })
  202. return info
  203. class VVVVIDShowIE(VVVVIDIE):
  204. _VALID_URL = r'(?P<base_url>%s(?P<id>\d+)(?:/(?P<show_title>[^/?&#]+))?)/?(?:[?#&]|$)' % VVVVIDIE._VALID_URL_BASE
  205. _TESTS = [{
  206. 'url': 'https://www.vvvvid.it/show/156/psyco-pass',
  207. 'info_dict': {
  208. 'id': '156',
  209. 'title': 'Psycho-Pass',
  210. 'description': 'md5:94d572c0bd85894b193b8aebc9a3a806',
  211. },
  212. 'playlist_count': 46,
  213. }, {
  214. 'url': 'https://www.vvvvid.it/show/156',
  215. 'only_matching': True,
  216. }]
  217. def _real_extract(self, url):
  218. base_url, show_id, show_title = re.match(self._VALID_URL, url).groups()
  219. seasons = self._download_info(
  220. show_id, 'seasons/', show_title)
  221. show_info = self._download_info(
  222. show_id, 'info/', show_title, fatal=False)
  223. if not show_title:
  224. base_url += "/title"
  225. entries = []
  226. for season in (seasons or []):
  227. episodes = season.get('episodes') or []
  228. playlist_title = season.get('name') or show_info.get('title')
  229. for episode in episodes:
  230. if episode.get('playable') is False:
  231. continue
  232. season_id = str_or_none(episode.get('season_id'))
  233. video_id = str_or_none(episode.get('video_id'))
  234. if not (season_id and video_id):
  235. continue
  236. info = self._extract_common_video_info(episode)
  237. info.update({
  238. '_type': 'url_transparent',
  239. 'ie_key': VVVVIDIE.ie_key(),
  240. 'url': '/'.join([base_url, season_id, video_id]),
  241. 'title': episode.get('title'),
  242. 'description': episode.get('description'),
  243. 'season_id': season_id,
  244. 'playlist_title': playlist_title,
  245. })
  246. entries.append(info)
  247. return self.playlist_result(
  248. entries, show_id, show_info.get('title'), show_info.get('description'))