summaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/daisuki.py
blob: 5c9ac68a02590b3eccea83cd4d9a49ffca47a2e8 (plain)
    1 from __future__ import unicode_literals
    2 
    3 import base64
    4 import json
    5 import random
    6 import re
    7 
    8 from .common import InfoExtractor
    9 from ..aes import (
   10     aes_cbc_decrypt,
   11     aes_cbc_encrypt,
   12 )
   13 from ..utils import (
   14     bytes_to_intlist,
   15     bytes_to_long,
   16     extract_attributes,
   17     ExtractorError,
   18     intlist_to_bytes,
   19     js_to_json,
   20     int_or_none,
   21     long_to_bytes,
   22     pkcs1pad,
   23 )
   24 
   25 
   26 class DaisukiMottoIE(InfoExtractor):
   27     _VALID_URL = r'https?://motto\.daisuki\.net/framewatch/embed/[^/]+/(?P<id>[0-9a-zA-Z]{3})'
   28 
   29     _TEST = {
   30         'url': 'http://motto.daisuki.net/framewatch/embed/embedDRAGONBALLSUPERUniverseSurvivalsaga/V2e/760/428',
   31         'info_dict': {
   32             'id': 'V2e',
   33             'ext': 'mp4',
   34             'title': '#117 SHOWDOWN OF LOVE! ANDROIDS VS UNIVERSE 2!!',
   35             'subtitles': {
   36                 'mul': [{
   37                     'ext': 'ttml',
   38                 }],
   39             },
   40         },
   41         'params': {
   42             'skip_download': True,  # AES-encrypted HLS stream
   43         },
   44     }
   45 
   46     # The public key in PEM format can be found in clientlibs_anime_watch.min.js
   47     _RSA_KEY = (0xc5524c25e8e14b366b3754940beeb6f96cb7e2feef0b932c7659a0c5c3bf173d602464c2df73d693b513ae06ff1be8f367529ab30bf969c5640522181f2a0c51ea546ae120d3d8d908595e4eff765b389cde080a1ef7f1bbfb07411cc568db73b7f521cedf270cbfbe0ddbc29b1ac9d0f2d8f4359098caffee6d07915020077d, 65537)
   48 
   49     def _real_extract(self, url):
   50         video_id = self._match_id(url)
   51 
   52         webpage = self._download_webpage(url, video_id)
   53 
   54         flashvars = self._parse_json(self._search_regex(
   55             r'(?s)var\s+flashvars\s*=\s*({.+?});', webpage, 'flashvars'),
   56             video_id, transform_source=js_to_json)
   57 
   58         iv = [0] * 16
   59 
   60         data = {}
   61         for key in ('device_cd', 'mv_id', 'ss1_prm', 'ss2_prm', 'ss3_prm', 'ss_id'):
   62             data[key] = flashvars.get(key, '')
   63 
   64         encrypted_rtn = None
   65 
   66         # Some AES keys are rejected. Try it with different AES keys
   67         for idx in range(5):
   68             aes_key = [random.randint(0, 254) for _ in range(32)]
   69             padded_aeskey = intlist_to_bytes(pkcs1pad(aes_key, 128))
   70 
   71             n, e = self._RSA_KEY
   72             encrypted_aeskey = long_to_bytes(pow(bytes_to_long(padded_aeskey), e, n))
   73             init_data = self._download_json(
   74                 'http://motto.daisuki.net/fastAPI/bgn/init/',
   75                 video_id, query={
   76                     's': flashvars.get('s', ''),
   77                     'c': flashvars.get('ss3_prm', ''),
   78                     'e': url,
   79                     'd': base64.b64encode(intlist_to_bytes(aes_cbc_encrypt(
   80                         bytes_to_intlist(json.dumps(data)),
   81                         aes_key, iv))).decode('ascii'),
   82                     'a': base64.b64encode(encrypted_aeskey).decode('ascii'),
   83                 }, note='Downloading JSON metadata' + (' (try #%d)' % (idx + 1) if idx > 0 else ''))
   84 
   85             if 'rtn' in init_data:
   86                 encrypted_rtn = init_data['rtn']
   87                 break
   88 
   89             self._sleep(5, video_id)
   90 
   91         if encrypted_rtn is None:
   92             raise ExtractorError('Failed to fetch init data')
   93 
   94         rtn = self._parse_json(
   95             intlist_to_bytes(aes_cbc_decrypt(bytes_to_intlist(
   96                 base64.b64decode(encrypted_rtn)),
   97                 aes_key, iv)).decode('utf-8').rstrip('\0'),
   98             video_id)
   99 
  100         title = rtn['title_str']
  101 
  102         formats = self._extract_m3u8_formats(
  103             rtn['play_url'], video_id, ext='mp4', entry_protocol='m3u8_native')
  104 
  105         subtitles = {}
  106         caption_url = rtn.get('caption_url')
  107         if caption_url:
  108             # mul: multiple languages
  109             subtitles['mul'] = [{
  110                 'url': caption_url,
  111                 'ext': 'ttml',
  112             }]
  113 
  114         return {
  115             'id': video_id,
  116             'title': title,
  117             'formats': formats,
  118             'subtitles': subtitles,
  119         }
  120 
  121 
  122 class DaisukiMottoPlaylistIE(InfoExtractor):
  123     _VALID_URL = r'https?://motto\.daisuki\.net/(?P<id>information)/'
  124 
  125     _TEST = {
  126         'url': 'http://motto.daisuki.net/information/',
  127         'info_dict': {
  128             'title': 'DRAGON BALL SUPER',
  129         },
  130         'playlist_mincount': 117,
  131     }
  132 
  133     def _real_extract(self, url):
  134         playlist_id = self._match_id(url)
  135 
  136         webpage = self._download_webpage(url, playlist_id)
  137 
  138         entries = []
  139         for li in re.findall(r'(<li[^>]+?data-product_id="[a-zA-Z0-9]{3}"[^>]+>)', webpage):
  140             attr = extract_attributes(li)
  141             ad_id = attr.get('data-ad_id')
  142             product_id = attr.get('data-product_id')
  143             if ad_id and product_id:
  144                 episode_id = attr.get('data-chapter')
  145                 entries.append({
  146                     '_type': 'url_transparent',
  147                     'url': 'http://motto.daisuki.net/framewatch/embed/%s/%s/760/428' % (ad_id, product_id),
  148                     'episode_id': episode_id,
  149                     'episode_number': int_or_none(episode_id),
  150                     'ie_key': 'DaisukiMotto',
  151                 })
  152 
  153         return self.playlist_result(entries, playlist_title='DRAGON BALL SUPER')

Generated by cgit