logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

mgtv.py (3548B)


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