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