]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/mxplayer.py
[southpark] Fix SouthParkDE (#812)
[yt-dlp.git] / yt_dlp / extractor / mxplayer.py
1 from __future__ import unicode_literals
2
3
4 from .common import InfoExtractor
5 from ..compat import compat_str
6 from ..utils import (
7 ExtractorError,
8 js_to_json,
9 qualities,
10 try_get,
11 url_or_none,
12 urljoin,
13 )
14
15
16 class MxplayerIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:www\.)?mxplayer\.in/(?:movie|show/[-\w]+/[-\w]+)/(?P<display_id>[-\w]+)-(?P<id>\w+)'
18 _TESTS = [{
19 'url': 'https://www.mxplayer.in/movie/watch-knock-knock-hindi-dubbed-movie-online-b9fa28df3bfb8758874735bbd7d2655a?watch=true',
20 'info_dict': {
21 'id': 'b9fa28df3bfb8758874735bbd7d2655a',
22 'ext': 'mp4',
23 'title': 'Knock Knock (Hindi Dubbed)',
24 'description': 'md5:b195ba93ff1987309cfa58e2839d2a5b'
25 },
26 'params': {
27 'skip_download': True,
28 'format': 'bestvideo'
29 }
30 }, {
31 'url': 'https://www.mxplayer.in/show/watch-shaitaan/season-1/the-infamous-taxi-gang-of-meerut-online-45055d5bcff169ad48f2ad7552a83d6c',
32 'info_dict': {
33 'id': '45055d5bcff169ad48f2ad7552a83d6c',
34 'ext': 'm3u8',
35 'title': 'The infamous taxi gang of Meerut',
36 'description': 'md5:033a0a7e3fd147be4fb7e07a01a3dc28',
37 'season': 'Season 1',
38 'series': 'Shaitaan'
39 },
40 'params': {
41 'skip_download': True,
42 }
43 }, {
44 'url': 'https://www.mxplayer.in/show/watch-aashram/chapter-1/duh-swapna-online-d445579792b0135598ba1bc9088a84cb',
45 'info_dict': {
46 'id': 'd445579792b0135598ba1bc9088a84cb',
47 'ext': 'mp4',
48 'title': 'Duh Swapna',
49 'description': 'md5:35ff39c4bdac403c53be1e16a04192d8',
50 'season': 'Chapter 1',
51 'series': 'Aashram'
52 },
53 'expected_warnings': ['Unknown MIME type application/mp4 in DASH manifest'],
54 'params': {
55 'skip_download': True,
56 'format': 'bestvideo'
57 }
58 }]
59
60 def _get_stream_urls(self, video_dict):
61 stream_provider_dict = try_get(
62 video_dict,
63 lambda x: x['stream'][x['stream']['provider']])
64 if not stream_provider_dict:
65 raise ExtractorError('No stream provider found', expected=True)
66
67 for stream_name, stream in stream_provider_dict.items():
68 if stream_name in ('hls', 'dash', 'hlsUrl', 'dashUrl'):
69 stream_type = stream_name.replace('Url', '')
70 if isinstance(stream, dict):
71 for quality, stream_url in stream.items():
72 if stream_url:
73 yield stream_type, quality, stream_url
74 else:
75 yield stream_type, 'base', stream
76
77 def _real_extract(self, url):
78 display_id, video_id = self._match_valid_url(url).groups()
79 webpage = self._download_webpage(url, video_id)
80
81 source = self._parse_json(
82 js_to_json(self._html_search_regex(
83 r'(?s)<script>window\.state\s*[:=]\s(\{.+\})\n(\w+).*(</script>).*',
84 webpage, 'WindowState')),
85 video_id)
86 if not source:
87 raise ExtractorError('Cannot find source', expected=True)
88
89 config_dict = source['config']
90 video_dict = source['entities'][video_id]
91
92 thumbnails = []
93 for i in video_dict.get('imageInfo') or []:
94 thumbnails.append({
95 'url': urljoin(config_dict['imageBaseUrl'], i['url']),
96 'width': i['width'],
97 'height': i['height'],
98 })
99
100 formats = []
101 get_quality = qualities(['main', 'base', 'high'])
102 for stream_type, quality, stream_url in self._get_stream_urls(video_dict):
103 format_url = url_or_none(urljoin(config_dict['videoCdnBaseUrl'], stream_url))
104 if not format_url:
105 continue
106 if stream_type == 'dash':
107 dash_formats = self._extract_mpd_formats(
108 format_url, video_id, mpd_id='dash-%s' % quality, headers={'Referer': url})
109 for frmt in dash_formats:
110 frmt['quality'] = get_quality(quality)
111 formats.extend(dash_formats)
112 dash_formats_h265 = self._extract_mpd_formats(
113 format_url.replace('h264_high', 'h265_main'), video_id, mpd_id='dash-%s' % quality, headers={'Referer': url}, fatal=False)
114 for frmt in dash_formats_h265:
115 frmt['quality'] = get_quality(quality)
116 formats.extend(dash_formats_h265)
117 elif stream_type == 'hls':
118 formats.extend(self._extract_m3u8_formats(
119 format_url, video_id, fatal=False,
120 m3u8_id='hls-%s' % quality, quality=get_quality(quality), ext='mp4'))
121
122 self._sort_formats(formats)
123 return {
124 'id': video_id,
125 'display_id': display_id,
126 'title': video_dict['title'] or self._og_search_title(webpage),
127 'formats': formats,
128 'description': video_dict.get('description'),
129 'season': try_get(video_dict, lambda x: x['container']['title']),
130 'series': try_get(video_dict, lambda x: x['container']['container']['title']),
131 'thumbnails': thumbnails,
132 }
133
134
135 class MxplayerShowIE(InfoExtractor):
136 _VALID_URL = r'(?:https?://)(?:www\.)?mxplayer\.in/show/(?P<display_id>[-\w]+)-(?P<id>\w+)/?(?:$|[#?])'
137 _TESTS = [{
138 'url': 'https://www.mxplayer.in/show/watch-chakravartin-ashoka-samrat-series-online-a8f44e3cc0814b5601d17772cedf5417',
139 'playlist_mincount': 440,
140 'info_dict': {
141 'id': 'a8f44e3cc0814b5601d17772cedf5417',
142 'title': 'Watch Chakravartin Ashoka Samrat Series Online',
143 }
144 }]
145
146 _API_SHOW_URL = "https://api.mxplay.com/v1/web/detail/tab/tvshowseasons?type=tv_show&id={}&device-density=2&platform=com.mxplay.desktop&content-languages=hi,en"
147 _API_EPISODES_URL = "https://api.mxplay.com/v1/web/detail/tab/tvshowepisodes?type=season&id={}&device-density=1&platform=com.mxplay.desktop&content-languages=hi,en&{}"
148
149 def _entries(self, show_id):
150 show_json = self._download_json(
151 self._API_SHOW_URL.format(show_id),
152 video_id=show_id, headers={'Referer': 'https://mxplayer.in'})
153 page_num = 0
154 for season in show_json.get('items') or []:
155 season_id = try_get(season, lambda x: x['id'], compat_str)
156 next_url = ''
157 while next_url is not None:
158 page_num += 1
159 season_json = self._download_json(
160 self._API_EPISODES_URL.format(season_id, next_url),
161 video_id=season_id,
162 headers={'Referer': 'https://mxplayer.in'},
163 note='Downloading JSON metadata page %d' % page_num)
164 for episode in season_json.get('items') or []:
165 video_url = episode['webUrl']
166 yield self.url_result(
167 'https://mxplayer.in%s' % video_url,
168 ie=MxplayerIE.ie_key(), video_id=video_url.split('-')[-1])
169 next_url = season_json.get('next')
170
171 def _real_extract(self, url):
172 display_id, show_id = self._match_valid_url(url).groups()
173 return self.playlist_result(
174 self._entries(show_id), playlist_id=show_id,
175 playlist_title=display_id.replace('-', ' ').title())