]> jfr.im git - yt-dlp.git/blame - youtube_dlc/extractor/mgtv.py
Merge pull request #187 from pukkandan/break-on-existing
[yt-dlp.git] / youtube_dlc / extractor / mgtv.py
CommitLineData
1da19488
YCH
1# coding: utf-8
2from __future__ import unicode_literals
3
dc27fd8b
RA
4import base64
5import time
6import uuid
7
1da19488 8from .common import InfoExtractor
dc27fd8b
RA
9from ..compat import (
10 compat_HTTPError,
11 compat_str,
12)
13from ..utils import (
14 ExtractorError,
15 int_or_none,
16)
1da19488
YCH
17
18
19class MGTVIE(InfoExtractor):
30eaa3a7 20 _VALID_URL = r'https?://(?:www\.)?mgtv\.com/(v|b)/(?:[^/]+/)*(?P<id>\d+)\.html'
a292cba2 21 IE_DESC = '芒果TV'
dc27fd8b 22 _GEO_COUNTRIES = ['CN']
1da19488 23
dc35ba0e 24 _TESTS = [{
1da19488 25 'url': 'http://www.mgtv.com/v/1/290525/f/3116640.html',
1da19488
YCH
26 'info_dict': {
27 'id': '3116640',
28 'ext': 'mp4',
dc27fd8b 29 'title': '我是歌手 第四季',
1da19488
YCH
30 'description': '我是歌手第四季双年巅峰会',
31 'duration': 7461,
ec85ded8 32 'thumbnail': r're:^https?://.*\.jpg$',
1da19488 33 },
dc35ba0e 34 }, {
30eaa3a7 35 'url': 'http://www.mgtv.com/b/301817/3826653.html',
dc35ba0e
S
36 'only_matching': True,
37 }]
1da19488
YCH
38
39 def _real_extract(self, url):
40 video_id = self._match_id(url)
dc27fd8b
RA
41 try:
42 api_data = self._download_json(
43 'https://pcweb.api.mgtv.com/player/video', video_id, query={
44 'tk2': base64.urlsafe_b64encode(b'did=%s|pno=1030|ver=0.3.0301|clit=%d' % (compat_str(uuid.uuid4()).encode(), time.time()))[::-1],
45 'video_id': video_id,
46 }, headers=self.geo_verification_headers())['data']
47 except ExtractorError as e:
48 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
49 error = self._parse_json(e.cause.read().decode(), None)
50 if error.get('code') == 40005:
51 self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
52 raise ExtractorError(error['msg'], expected=True)
53 raise
1da19488 54 info = api_data['info']
30eaa3a7 55 title = info['title'].strip()
dc27fd8b
RA
56 stream_data = self._download_json(
57 'https://pcweb.api.mgtv.com/player/getSource', video_id, query={
58 'pm2': api_data['atc']['pm2'],
59 'video_id': video_id,
60 }, headers=self.geo_verification_headers())['data']
61 stream_domain = stream_data['stream_domain'][0]
1da19488
YCH
62
63 formats = []
dc27fd8b 64 for idx, stream in enumerate(stream_data['stream']):
30eaa3a7
RA
65 stream_path = stream.get('url')
66 if not stream_path:
67 continue
68 format_data = self._download_json(
69 stream_domain + stream_path, video_id,
70 note='Download video info for format #%d' % idx)
71 format_url = format_data.get('info')
72 if not format_url:
b9e7bc55 73 continue
dc27fd8b 74 tbr = int_or_none(stream.get('filebitrate') or self._search_regex(
30eaa3a7
RA
75 r'_(\d+)_mp4/', format_url, 'tbr', default=None))
76 formats.append({
77 'format_id': compat_str(tbr or idx),
78 'url': format_url,
79 'ext': 'mp4',
80 'tbr': tbr,
81 'protocol': 'm3u8_native',
4dcd4b7b
S
82 'http_headers': {
83 'Referer': url,
84 },
9a37ff82 85 'format_note': stream.get('name'),
30eaa3a7 86 })
1da19488
YCH
87 self._sort_formats(formats)
88
89 return {
90 'id': video_id,
30eaa3a7 91 'title': title,
1da19488
YCH
92 'formats': formats,
93 'description': info.get('desc'),
94 'duration': int_or_none(info.get('duration')),
95 'thumbnail': info.get('thumb'),
96 }