]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/mgtv.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / mgtv.py
CommitLineData
dc27fd8b
RA
1import base64
2import time
3import uuid
4
1da19488 5from .common import InfoExtractor
dc27fd8b
RA
6from ..compat import (
7 compat_HTTPError,
8 compat_str,
9)
10from ..utils import (
11 ExtractorError,
12 int_or_none,
33b8c411
HTL
13 try_get,
14 url_or_none,
dc27fd8b 15)
1da19488
YCH
16
17
18class MGTVIE(InfoExtractor):
8bdd16b4 19 _VALID_URL = r'https?://(?:w(?:ww)?\.)?mgtv\.com/(v|b)/(?:[^/]+/)*(?P<id>\d+)\.html'
a292cba2 20 IE_DESC = '芒果TV'
33b8c411 21 IE_NAME = 'MangoTV'
1da19488 22
dc35ba0e 23 _TESTS = [{
1da19488 24 'url': 'http://www.mgtv.com/v/1/290525/f/3116640.html',
1da19488
YCH
25 'info_dict': {
26 'id': '3116640',
27 'ext': 'mp4',
dc27fd8b 28 'title': '我是歌手 第四季',
1da19488
YCH
29 'description': '我是歌手第四季双年巅峰会',
30 'duration': 7461,
ec85ded8 31 'thumbnail': r're:^https?://.*\.jpg$',
1da19488 32 },
33b8c411
HTL
33 }, {
34 'url': 'https://w.mgtv.com/b/427837/15588271.html',
35 'info_dict': {
36 'id': '15588271',
37 'ext': 'mp4',
38 'title': '春日迟迟再出发 沉浸版',
39 'description': 'md5:a7a05a05b1aa87bd50cae619b19bbca6',
40 'thumbnail': r're:^https?://.+\.jpg',
41 'duration': 4026,
42 },
43 }, {
44 'url': 'https://w.mgtv.com/b/333652/7329822.html',
45 'info_dict': {
46 'id': '7329822',
47 'ext': 'mp4',
48 'title': '拜托,请你爱我',
49 'description': 'md5:cd81be6499bafe32e4d143abd822bf9c',
50 'thumbnail': r're:^https?://.+\.jpg',
51 'duration': 2656,
52 },
53 }, {
54 'url': 'https://w.mgtv.com/b/427837/15591647.html',
55 'only_matching': True,
56 }, {
57 'url': 'https://w.mgtv.com/b/388252/15634192.html?fpa=33318&fpos=4&lastp=ch_home',
58 'only_matching': True,
dc35ba0e 59 }, {
30eaa3a7 60 'url': 'http://www.mgtv.com/b/301817/3826653.html',
dc35ba0e 61 'only_matching': True,
8bdd16b4 62 }, {
63 'url': 'https://w.mgtv.com/b/301817/3826653.html',
64 'only_matching': True,
dc35ba0e 65 }]
1da19488
YCH
66
67 def _real_extract(self, url):
68 video_id = self._match_id(url)
33b8c411 69 tk2 = base64.urlsafe_b64encode(
c6e07cf1 70 f'did={str(uuid.uuid4())}|pno=1030|ver=0.3.0301|clit={int(time.time())}'.encode())[::-1]
dc27fd8b
RA
71 try:
72 api_data = self._download_json(
73 'https://pcweb.api.mgtv.com/player/video', video_id, query={
8bdd16b4 74 'tk2': tk2,
dc27fd8b 75 'video_id': video_id,
33b8c411 76 'type': 'pch5'
dc27fd8b
RA
77 }, headers=self.geo_verification_headers())['data']
78 except ExtractorError as e:
79 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
80 error = self._parse_json(e.cause.read().decode(), None)
81 if error.get('code') == 40005:
82 self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
83 raise ExtractorError(error['msg'], expected=True)
84 raise
1da19488 85 info = api_data['info']
30eaa3a7 86 title = info['title'].strip()
dc27fd8b
RA
87 stream_data = self._download_json(
88 'https://pcweb.api.mgtv.com/player/getSource', video_id, query={
89 'pm2': api_data['atc']['pm2'],
8bdd16b4 90 'tk2': tk2,
dc27fd8b 91 'video_id': video_id,
33b8c411 92 'src': 'intelmgtv',
dc27fd8b
RA
93 }, headers=self.geo_verification_headers())['data']
94 stream_domain = stream_data['stream_domain'][0]
1da19488
YCH
95
96 formats = []
dc27fd8b 97 for idx, stream in enumerate(stream_data['stream']):
30eaa3a7
RA
98 stream_path = stream.get('url')
99 if not stream_path:
100 continue
101 format_data = self._download_json(
102 stream_domain + stream_path, video_id,
33b8c411 103 note=f'Download video info for format #{idx}')
30eaa3a7
RA
104 format_url = format_data.get('info')
105 if not format_url:
b9e7bc55 106 continue
dc27fd8b 107 tbr = int_or_none(stream.get('filebitrate') or self._search_regex(
30eaa3a7
RA
108 r'_(\d+)_mp4/', format_url, 'tbr', default=None))
109 formats.append({
110 'format_id': compat_str(tbr or idx),
33b8c411 111 'url': url_or_none(format_url),
30eaa3a7
RA
112 'ext': 'mp4',
113 'tbr': tbr,
114 'protocol': 'm3u8_native',
4dcd4b7b
S
115 'http_headers': {
116 'Referer': url,
117 },
9a37ff82 118 'format_note': stream.get('name'),
30eaa3a7 119 })
1da19488
YCH
120
121 return {
122 'id': video_id,
30eaa3a7 123 'title': title,
1da19488
YCH
124 'formats': formats,
125 'description': info.get('desc'),
126 'duration': int_or_none(info.get('duration')),
127 'thumbnail': info.get('thumb'),
33b8c411 128 'subtitles': self.extract_subtitles(video_id, stream_domain),
1da19488 129 }
33b8c411
HTL
130
131 def _get_subtitles(self, video_id, domain):
132 info = self._download_json(f'https://pcweb.api.mgtv.com/video/title?videoId={video_id}',
133 video_id, fatal=False) or {}
134 subtitles = {}
135 for sub in try_get(info, lambda x: x['data']['title']) or []:
136 url_sub = sub.get('url')
137 if not url_sub:
138 continue
1765c603 139 locale = sub.get('captionSimpleName') or 'en'
33b8c411
HTL
140 sub = self._download_json(f'{domain}{url_sub}', video_id, fatal=False,
141 note=f'Download subtitle for locale {sub.get("name")} ({locale})') or {}
142 sub_url = url_or_none(sub.get('info'))
143 if not sub_url:
144 continue
1765c603 145 subtitles.setdefault(locale.lower(), []).append({
33b8c411 146 'url': sub_url,
1765c603 147 'name': sub.get('name'),
33b8c411
HTL
148 'ext': 'srt'
149 })
150 return subtitles