]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/mgtv.py
Tidy up cleanup rule and keep lintian happy.
[youtubedl] / 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 _TESTS = [{
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 # no tbr extracted from stream_url
25 'url': 'http://www.mgtv.com/v/1/1/f/3324755.html',
26 'only_matching': True,
27 }]
28
29 def _real_extract(self, url):
30 video_id = self._match_id(url)
31 api_data = self._download_json(
32 'http://v.api.mgtv.com/player/video', video_id,
33 query={'video_id': video_id},
34 headers=self.geo_verification_headers())['data']
35 info = api_data['info']
36
37 formats = []
38 for idx, stream in enumerate(api_data['stream']):
39 stream_url = stream.get('url')
40 if not stream_url:
41 continue
42 tbr = int_or_none(self._search_regex(
43 r'(\d+)\.mp4', stream_url, 'tbr', default=None))
44
45 def extract_format(stream_url, format_id, idx, query={}):
46 format_info = self._download_json(
47 stream_url, video_id,
48 note='Download video info for format %s' % (format_id or '#%d' % idx),
49 query=query)
50 return {
51 'format_id': format_id,
52 'url': format_info['info'],
53 'ext': 'mp4',
54 'tbr': tbr,
55 }
56
57 formats.append(extract_format(
58 stream_url, 'hls-%d' % tbr if tbr else None, idx * 2))
59 formats.append(extract_format(stream_url.replace(
60 '/playlist.m3u8', ''), 'http-%d' % tbr if tbr else None, idx * 2 + 1, {'pno': 1031}))
61 self._sort_formats(formats)
62
63 return {
64 'id': video_id,
65 'title': info['title'].strip(),
66 'formats': formats,
67 'description': info.get('desc'),
68 'duration': int_or_none(info.get('duration')),
69 'thumbnail': info.get('thumb'),
70 }