mgtv.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import base64
  2. import time
  3. import uuid
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_HTTPError,
  7. compat_str,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. int_or_none,
  12. try_get,
  13. url_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. IE_NAME = 'MangoTV'
  19. _TESTS = [{
  20. 'url': 'http://www.mgtv.com/v/1/290525/f/3116640.html',
  21. 'info_dict': {
  22. 'id': '3116640',
  23. 'ext': 'mp4',
  24. 'title': '我是歌手 第四季',
  25. 'description': '我是歌手第四季双年巅峰会',
  26. 'duration': 7461,
  27. 'thumbnail': r're:^https?://.*\.jpg$',
  28. },
  29. }, {
  30. 'url': 'https://w.mgtv.com/b/427837/15588271.html',
  31. 'info_dict': {
  32. 'id': '15588271',
  33. 'ext': 'mp4',
  34. 'title': '春日迟迟再出发 沉浸版',
  35. 'description': 'md5:a7a05a05b1aa87bd50cae619b19bbca6',
  36. 'thumbnail': r're:^https?://.+\.jpg',
  37. 'duration': 4026,
  38. },
  39. }, {
  40. 'url': 'https://w.mgtv.com/b/333652/7329822.html',
  41. 'info_dict': {
  42. 'id': '7329822',
  43. 'ext': 'mp4',
  44. 'title': '拜托,请你爱我',
  45. 'description': 'md5:cd81be6499bafe32e4d143abd822bf9c',
  46. 'thumbnail': r're:^https?://.+\.jpg',
  47. 'duration': 2656,
  48. },
  49. }, {
  50. 'url': 'https://w.mgtv.com/b/427837/15591647.html',
  51. 'only_matching': True,
  52. }, {
  53. 'url': 'https://w.mgtv.com/b/388252/15634192.html?fpa=33318&fpos=4&lastp=ch_home',
  54. 'only_matching': True,
  55. }, {
  56. 'url': 'http://www.mgtv.com/b/301817/3826653.html',
  57. 'only_matching': True,
  58. }, {
  59. 'url': 'https://w.mgtv.com/b/301817/3826653.html',
  60. 'only_matching': True,
  61. }]
  62. def _real_extract(self, url):
  63. video_id = self._match_id(url)
  64. tk2 = base64.urlsafe_b64encode(
  65. f'did={str(uuid.uuid4())}|pno=1030|ver=0.3.0301|clit={int(time.time())}'.encode())[::-1]
  66. try:
  67. api_data = self._download_json(
  68. 'https://pcweb.api.mgtv.com/player/video', video_id, query={
  69. 'tk2': tk2,
  70. 'video_id': video_id,
  71. 'type': 'pch5'
  72. }, headers=self.geo_verification_headers())['data']
  73. except ExtractorError as e:
  74. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
  75. error = self._parse_json(e.cause.read().decode(), None)
  76. if error.get('code') == 40005:
  77. self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
  78. raise ExtractorError(error['msg'], expected=True)
  79. raise
  80. info = api_data['info']
  81. title = info['title'].strip()
  82. stream_data = self._download_json(
  83. 'https://pcweb.api.mgtv.com/player/getSource', video_id, query={
  84. 'pm2': api_data['atc']['pm2'],
  85. 'tk2': tk2,
  86. 'video_id': video_id,
  87. 'src': 'intelmgtv',
  88. }, headers=self.geo_verification_headers())['data']
  89. stream_domain = stream_data['stream_domain'][0]
  90. formats = []
  91. for idx, stream in enumerate(stream_data['stream']):
  92. stream_path = stream.get('url')
  93. if not stream_path:
  94. continue
  95. format_data = self._download_json(
  96. stream_domain + stream_path, video_id,
  97. note=f'Download video info for format #{idx}')
  98. format_url = format_data.get('info')
  99. if not format_url:
  100. continue
  101. tbr = int_or_none(stream.get('filebitrate') or self._search_regex(
  102. r'_(\d+)_mp4/', format_url, 'tbr', default=None))
  103. formats.append({
  104. 'format_id': compat_str(tbr or idx),
  105. 'url': url_or_none(format_url),
  106. 'ext': 'mp4',
  107. 'tbr': tbr,
  108. 'protocol': 'm3u8_native',
  109. 'http_headers': {
  110. 'Referer': url,
  111. },
  112. 'format_note': stream.get('name'),
  113. })
  114. return {
  115. 'id': video_id,
  116. 'title': title,
  117. 'formats': formats,
  118. 'description': info.get('desc'),
  119. 'duration': int_or_none(info.get('duration')),
  120. 'thumbnail': info.get('thumb'),
  121. 'subtitles': self.extract_subtitles(video_id, stream_domain),
  122. }
  123. def _get_subtitles(self, video_id, domain):
  124. info = self._download_json(f'https://pcweb.api.mgtv.com/video/title?videoId={video_id}',
  125. video_id, fatal=False) or {}
  126. subtitles = {}
  127. for sub in try_get(info, lambda x: x['data']['title']) or []:
  128. url_sub = sub.get('url')
  129. if not url_sub:
  130. continue
  131. locale = sub.get('captionSimpleName') or 'en'
  132. sub = self._download_json(f'{domain}{url_sub}', video_id, fatal=False,
  133. note=f'Download subtitle for locale {sub.get("name")} ({locale})') or {}
  134. sub_url = url_or_none(sub.get('info'))
  135. if not sub_url:
  136. continue
  137. subtitles.setdefault(locale.lower(), []).append({
  138. 'url': sub_url,
  139. 'name': sub.get('name'),
  140. 'ext': 'srt'
  141. })
  142. return subtitles