]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/mgtv.py
Prepare to upload.
[youtubedl] / youtube_dl / 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 )
17
18
19 class MGTVIE(InfoExtractor):
20 _VALID_URL = r'https?://(?:www\.)?mgtv\.com/(v|b)/(?:[^/]+/)*(?P<id>\d+)\.html'
21 IE_DESC = '芒果TV'
22 _GEO_COUNTRIES = ['CN']
23
24 _TESTS = [{
25 'url': 'http://www.mgtv.com/v/1/290525/f/3116640.html',
26 'info_dict': {
27 'id': '3116640',
28 'ext': 'mp4',
29 'title': '我是歌手 第四季',
30 'description': '我是歌手第四季双年巅峰会',
31 'duration': 7461,
32 'thumbnail': r're:^https?://.*\.jpg$',
33 },
34 }, {
35 'url': 'http://www.mgtv.com/b/301817/3826653.html',
36 'only_matching': True,
37 }]
38
39 def _real_extract(self, url):
40 video_id = self._match_id(url)
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
54 info = api_data['info']
55 title = info['title'].strip()
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]
62
63 formats = []
64 for idx, stream in enumerate(stream_data['stream']):
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:
73 continue
74 tbr = int_or_none(stream.get('filebitrate') or self._search_regex(
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',
82 'http_headers': {
83 'Referer': url,
84 },
85 'format_note': stream.get('name'),
86 })
87 self._sort_formats(formats)
88
89 return {
90 'id': video_id,
91 'title': title,
92 'formats': formats,
93 'description': info.get('desc'),
94 'duration': int_or_none(info.get('duration')),
95 'thumbnail': info.get('thumb'),
96 }