Merge pull request #9288 from reyyed/issue#9063fix
[youtube-dl] / youtube_dl / extractor / mgtv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import int_or_none
6
7
8 class MGTVIE(InfoExtractor):
9     _VALID_URL = r'https?://www\.mgtv\.com/v/(?:[^/]+/)*(?P<id>\d+)\.html'
10     IE_DESC = '芒果TV'
11
12     _TEST = {
13         'url': 'http://www.mgtv.com/v/1/290525/f/3116640.html',
14         'md5': '1bdadcf760a0b90946ca68ee9a2db41a',
15         'info_dict': {
16             'id': '3116640',
17             'ext': 'mp4',
18             'title': '我是歌手第四季双年巅峰会:韩红李玟“双王”领军对抗',
19             'description': '我是歌手第四季双年巅峰会',
20             'duration': 7461,
21             'thumbnail': 're:^https?://.*\.jpg$',
22         },
23     }
24
25     def _real_extract(self, url):
26         video_id = self._match_id(url)
27         api_data = self._download_json(
28             'http://v.api.mgtv.com/player/video', video_id,
29             query={'video_id': video_id},
30             headers=self.geo_verification_headers())['data']
31         info = api_data['info']
32
33         formats = []
34         for idx, stream in enumerate(api_data['stream']):
35             stream_url = stream.get('url')
36             if not stream_url:
37                 continue
38             tbr = int_or_none(self._search_regex(
39                 r'(\d+)\.mp4', stream_url, 'tbr', default=None))
40
41             def extract_format(stream_url, format_id, idx, query={}):
42                 format_info = self._download_json(
43                     stream_url, video_id,
44                     note='Download video info for format %s' % format_id or '#%d' % idx, query=query)
45                 return {
46                     'format_id': format_id,
47                     'url': format_info['info'],
48                     'ext': 'mp4',
49                     'tbr': tbr,
50                 }
51
52             formats.append(extract_format(
53                 stream_url, 'hls-%d' % tbr if tbr else None, idx * 2))
54             formats.append(extract_format(stream_url.replace(
55                 '/playlist.m3u8', ''), 'http-%d' % tbr if tbr else None, idx * 2 + 1, {'pno': 1031}))
56         self._sort_formats(formats)
57
58         return {
59             'id': video_id,
60             'title': info['title'].strip(),
61             'formats': formats,
62             'description': info.get('desc'),
63             'duration': int_or_none(info.get('duration')),
64             'thumbnail': info.get('thumb'),
65         }