[MXPlayer] Rewrite extractor with show support (#141)

Co-authored-by: Ashish <ashish@pop-os.localdomain>
Co-authored-by: pukkandan <pukkandan.ytdlp@gmail.com>
This commit is contained in:
Ashish 2021-03-06 01:11:02 +05:30 committed by GitHub
parent d069eca7a3
commit 1caaf92d47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,98 +6,122 @@ from .common import InfoExtractor
from ..utils import ( from ..utils import (
ExtractorError, ExtractorError,
js_to_json, js_to_json,
qualities,
try_get,
url_or_none, url_or_none,
urljoin, urljoin,
) )
VALID_STREAMS = ('dash', )
class MxplayerIE(InfoExtractor): class MxplayerIE(InfoExtractor):
_VALID_URL = r'https?://(?:www\.)?mxplayer\.in/movie/(?P<slug>[a-z0-9]+(?:-[a-z0-9]+)*)' _VALID_URL = r'https?://(?:www\.)?mxplayer\.in/(?:show|movie)/(?:(?P<display_id>[-/a-z0-9]+)-)?(?P<id>[a-z0-9]+)'
_TEST = { _TESTS = [{
'url': 'https://www.mxplayer.in/movie/watch-knock-knock-hindi-dubbed-movie-online-b9fa28df3bfb8758874735bbd7d2655a?watch=true', 'url': 'https://www.mxplayer.in/movie/watch-knock-knock-hindi-dubbed-movie-online-b9fa28df3bfb8758874735bbd7d2655a?watch=true',
'info_dict': { 'info_dict': {
'id': 'b9fa28df3bfb8758874735bbd7d2655a', 'id': 'b9fa28df3bfb8758874735bbd7d2655a',
'ext': 'mp4', 'ext': 'mp4',
'title': 'Knock Knock Movie | Watch 2015 Knock Knock Full Movie Online- MX Player', 'title': 'Knock Knock (Hindi Dubbed)',
'description': 'md5:b195ba93ff1987309cfa58e2839d2a5b' 'description': 'md5:b195ba93ff1987309cfa58e2839d2a5b'
}, },
'params': { 'params': {
'skip_download': True, 'skip_download': True,
'format': 'bestvideo' 'format': 'bestvideo'
} }
} }, {
'url': 'https://www.mxplayer.in/show/watch-shaitaan/season-1/the-infamous-taxi-gang-of-meerut-online-45055d5bcff169ad48f2ad7552a83d6c',
def _get_best_stream_url(self, stream): 'info_dict': {
best_stream = list(filter(None, [v for k, v in stream.items()])) 'id': '45055d5bcff169ad48f2ad7552a83d6c',
return best_stream.pop(0) if len(best_stream) else None 'ext': 'm3u8',
'title': 'The infamous taxi gang of Meerut',
'description': 'md5:033a0a7e3fd147be4fb7e07a01a3dc28',
'season': 'Season 1',
'series': 'Shaitaan'
},
'params': {
'skip_download': True,
}
}, {
'url': 'https://www.mxplayer.in/show/watch-aashram/chapter-1/duh-swapna-online-d445579792b0135598ba1bc9088a84cb',
'info_dict': {
'id': 'd445579792b0135598ba1bc9088a84cb',
'ext': 'mp4',
'title': 'Duh Swapna',
'description': 'md5:35ff39c4bdac403c53be1e16a04192d8',
'season': 'Chapter 1',
'series': 'Aashram'
},
'expected_warnings': ['Unknown MIME type application/mp4 in DASH manifest'],
'params': {
'skip_download': True,
'format': 'bestvideo'
}
}]
def _get_stream_urls(self, video_dict): def _get_stream_urls(self, video_dict):
stream_dict = video_dict.get('stream', {'provider': {}}) stream_provider_dict = try_get(
stream_provider = stream_dict.get('provider') video_dict,
lambda x: x['stream'][x['stream']['provider']])
if not stream_provider_dict:
raise ExtractorError('No stream provider found', expected=True)
if not stream_dict[stream_provider]: for stream_name, stream in stream_provider_dict.items():
message = 'No stream provider found' if stream_name in ('hls', 'dash', 'hlsUrl', 'dashUrl'):
raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True) stream_type = stream_name.replace('Url', '')
if isinstance(stream, dict):
streams = [] for quality, stream_url in stream.items():
for stream_name, v in stream_dict[stream_provider].items(): if stream_url:
if stream_name in VALID_STREAMS: yield stream_type, quality, stream_url
stream_url = self._get_best_stream_url(v) else:
if stream_url is None: yield stream_type, 'base', stream
continue
streams.append((stream_name, stream_url))
return streams
def _real_extract(self, url): def _real_extract(self, url):
mobj = re.match(self._VALID_URL, url) display_id, video_id = re.match(self._VALID_URL, url).groups()
video_slug = mobj.group('slug')
video_id = video_slug.split('-')[-1]
webpage = self._download_webpage(url, video_id) webpage = self._download_webpage(url, video_id)
window_state_json = self._html_search_regex( source = self._parse_json(
r'(?s)<script>window\.state\s*[:=]\s(\{.+\})\n(\w+).*(</script>).*', js_to_json(self._html_search_regex(
webpage, 'WindowState') r'(?s)<script>window\.state\s*[:=]\s(\{.+\})\n(\w+).*(</script>).*',
webpage, 'WindowState')),
source = self._parse_json(js_to_json(window_state_json), video_id) video_id)
if not source: if not source:
raise ExtractorError('Cannot find source', expected=True) raise ExtractorError('Cannot find source', expected=True)
config_dict = source['config'] config_dict = source['config']
video_dict = source['entities'][video_id] video_dict = source['entities'][video_id]
stream_urls = self._get_stream_urls(video_dict)
title = self._og_search_title(webpage, fatal=True, default=video_dict['title']) thumbnails = []
for i in video_dict.get('imageInfo') or []:
thumbnails.append({
'url': urljoin(config_dict['imageBaseUrl'], i['url']),
'width': i['width'],
'height': i['height'],
})
formats = [] formats = []
headers = {'Referer': url} get_quality = qualities(['main', 'base', 'high'])
for stream_name, stream_url in stream_urls: for stream_type, quality, stream_url in self._get_stream_urls(video_dict):
if stream_name == 'dash': format_url = url_or_none(urljoin(config_dict['videoCdnBaseUrl'], stream_url))
format_url = url_or_none(urljoin(config_dict['videoCdnBaseUrl'], stream_url)) if not format_url:
if not format_url: continue
continue if stream_type == 'dash':
formats.extend(self._extract_mpd_formats( dash_formats = self._extract_mpd_formats(
format_url, video_id, mpd_id='dash', headers=headers)) format_url, video_id, mpd_id='dash-%s' % quality, headers={'Referer': url})
for frmt in dash_formats:
frmt['quality'] = get_quality(quality)
formats.extend(dash_formats)
elif stream_type == 'hls':
formats.extend(self._extract_m3u8_formats(
format_url, video_id, fatal=False,
m3u8_id='hls-%s' % quality, quality=get_quality(quality)))
self._sort_formats(formats) self._sort_formats(formats)
info = { return {
'id': video_id, 'id': video_id,
'title': title, 'display_id': display_id.replace('/', '-'),
'title': video_dict['title'] or self._og_search_title(webpage),
'formats': formats,
'description': video_dict.get('description'), 'description': video_dict.get('description'),
'formats': formats 'season': try_get(video_dict, lambda x: x['container']['title']),
'series': try_get(video_dict, lambda x: x['container']['container']['title']),
'thumbnails': thumbnails,
} }
if video_dict.get('imageInfo'):
info['thumbnails'] = list(map(lambda i: dict(i, **{
'url': urljoin(config_dict['imageBaseUrl'], i['url'])
}), video_dict['imageInfo']))
if video_dict.get('webUrl'):
last_part = video_dict['webUrl'].split("/")[-1]
info['display_id'] = last_part.replace(video_id, "").rstrip("-")
return info