youtube-dl/youtube_dl/extractor/ninjastream.py

70 lines
2.1 KiB
Python
Raw Normal View History

2021-05-01 00:36:47 +09:00
# coding: utf-8
from __future__ import unicode_literals
import json
import posixpath
2021-05-01 00:36:47 +09:00
from .common import InfoExtractor
2021-05-01 01:07:44 +09:00
from ..utils import urljoin
2021-05-01 00:36:47 +09:00
class NinjaStreamIE(InfoExtractor):
"""
Handles downloading video from ninjastream.to
"""
2021-05-01 00:40:27 +09:00
_VALID_URL = r'https?://(?:www\.)?ninjastream\.to/(?:download|watch)/(?P<id>[^/?#]+)'
2021-05-01 00:36:47 +09:00
_TESTS = [
{
2021-05-01 01:13:10 +09:00
'url': 'https://ninjastream.to/watch/GbJQP8rawQ7rw',
2021-05-01 00:36:47 +09:00
'info_dict': {
2021-05-01 01:13:10 +09:00
'id': 'GbJQP8rawQ7rw',
2021-05-01 00:36:47 +09:00
'ext': 'mp4',
2021-05-01 01:13:10 +09:00
'title': 'Big Buck Bunny 360 10s 5MB'
2021-05-01 00:36:47 +09:00
},
}
]
def _real_extract(self, url):
2021-05-01 00:40:27 +09:00
video_id = self._match_id(url)
2021-05-01 00:36:47 +09:00
# Get the hosted webpage
webpage = self._download_webpage(url, video_id)
# The v-bind:file will give us the correct title for the video
file_meta = self._parse_json(
self._html_search_regex(r'v-bind:file="([^"]*)"', webpage,
video_id),
video_id, fatal=False) or {}
try:
filename = posixpath.splitext(file_meta['name'])[0]
except KeyError:
filename = 'ninjastream.to'
2021-05-01 01:07:44 +09:00
thumbnail = file_meta.get('poster_id')
if thumbnail:
thumbnail = urljoin('https://cdn.ninjastream.to/', thumbnail)
data = {'id': video_id}
headers = {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json',
'Referer': url,
}
data = json.dumps(data, separators=(',', ':')).encode('utf-8')
stream_meta = self._download_json('https://ninjastream.to/api/video/get',
video_id, data=data, headers=headers)
2021-05-01 00:36:47 +09:00
# Get and parse the m3u8 information
2021-05-01 01:07:44 +09:00
stream_url = stream_meta['result']['playlist']
2021-05-01 00:36:47 +09:00
formats = self._extract_m3u8_formats(
2021-05-01 01:07:44 +09:00
stream_url, video_id, 'mp4', entry_protocol='m3u8_native',
m3u8_id='hls')
2021-05-01 00:36:47 +09:00
return {
'formats': formats,
'id': video_id,
2021-05-01 01:07:44 +09:00
'thumbnail': thumbnail,
2021-05-01 00:36:47 +09:00
'title': filename,
}