You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.3 KiB

8 years ago
8 years ago
8 years ago
  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?://(?:www\.)?mgtv\.com/(v|b)/(?:[^/]+/)*(?P<id>\d+)\.html'
  17. IE_DESC = '芒果TV'
  18. _GEO_COUNTRIES = ['CN']
  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': 'http://www.mgtv.com/b/301817/3826653.html',
  31. 'only_matching': True,
  32. }]
  33. def _real_extract(self, url):
  34. video_id = self._match_id(url)
  35. try:
  36. api_data = self._download_json(
  37. 'https://pcweb.api.mgtv.com/player/video', video_id, query={
  38. 'tk2': base64.urlsafe_b64encode(b'did=%s|pno=1030|ver=0.3.0301|clit=%d' % (compat_str(uuid.uuid4()).encode(), time.time()))[::-1],
  39. 'video_id': video_id,
  40. }, headers=self.geo_verification_headers())['data']
  41. except ExtractorError as e:
  42. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
  43. error = self._parse_json(e.cause.read().decode(), None)
  44. if error.get('code') == 40005:
  45. self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
  46. raise ExtractorError(error['msg'], expected=True)
  47. raise
  48. info = api_data['info']
  49. title = info['title'].strip()
  50. stream_data = self._download_json(
  51. 'https://pcweb.api.mgtv.com/player/getSource', video_id, query={
  52. 'pm2': api_data['atc']['pm2'],
  53. 'video_id': video_id,
  54. }, headers=self.geo_verification_headers())['data']
  55. stream_domain = stream_data['stream_domain'][0]
  56. formats = []
  57. for idx, stream in enumerate(stream_data['stream']):
  58. stream_path = stream.get('url')
  59. if not stream_path:
  60. continue
  61. format_data = self._download_json(
  62. stream_domain + stream_path, video_id,
  63. note='Download video info for format #%d' % idx)
  64. format_url = format_data.get('info')
  65. if not format_url:
  66. continue
  67. tbr = int_or_none(stream.get('filebitrate') or self._search_regex(
  68. r'_(\d+)_mp4/', format_url, 'tbr', default=None))
  69. formats.append({
  70. 'format_id': compat_str(tbr or idx),
  71. 'url': format_url,
  72. 'ext': 'mp4',
  73. 'tbr': tbr,
  74. 'protocol': 'm3u8_native',
  75. 'http_headers': {
  76. 'Referer': url,
  77. },
  78. 'format_note': stream.get('name'),
  79. })
  80. self._sort_formats(formats)
  81. return {
  82. 'id': video_id,
  83. 'title': title,
  84. 'formats': formats,
  85. 'description': info.get('desc'),
  86. 'duration': int_or_none(info.get('duration')),
  87. 'thumbnail': info.get('thumb'),
  88. }