gamespot.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from __future__ import unicode_literals
  2. import re
  3. from .once import OnceIE
  4. from ..compat import (
  5. compat_urllib_parse_unquote,
  6. )
  7. from ..utils import (
  8. unescapeHTML,
  9. url_basename,
  10. dict_get,
  11. )
  12. class GameSpotIE(OnceIE):
  13. _VALID_URL = r'https?://(?:www\.)?gamespot\.com/(?:video|article)s/(?:[^/]+/\d+-|embed/)(?P<id>\d+)'
  14. _TESTS = [{
  15. 'url': 'http://www.gamespot.com/videos/arma-3-community-guide-sitrep-i/2300-6410818/',
  16. 'md5': 'b2a30deaa8654fcccd43713a6b6a4825',
  17. 'info_dict': {
  18. 'id': 'gs-2300-6410818',
  19. 'ext': 'mp4',
  20. 'title': 'Arma 3 - Community Guide: SITREP I',
  21. 'description': 'Check out this video where some of the basics of Arma 3 is explained.',
  22. },
  23. }, {
  24. 'url': 'http://www.gamespot.com/videos/the-witcher-3-wild-hunt-xbox-one-now-playing/2300-6424837/',
  25. 'info_dict': {
  26. 'id': 'gs-2300-6424837',
  27. 'ext': 'mp4',
  28. 'title': 'Now Playing - The Witcher 3: Wild Hunt',
  29. 'description': 'Join us as we take a look at the early hours of The Witcher 3: Wild Hunt and more.',
  30. },
  31. 'params': {
  32. 'skip_download': True, # m3u8 downloads
  33. },
  34. }, {
  35. 'url': 'https://www.gamespot.com/videos/embed/6439218/',
  36. 'only_matching': True,
  37. }]
  38. def _real_extract(self, url):
  39. page_id = self._match_id(url)
  40. webpage = self._download_webpage(url, page_id)
  41. data_video_json = self._search_regex(
  42. r'data-video=["\'](.*?)["\']', webpage, 'data video')
  43. data_video = self._parse_json(unescapeHTML(data_video_json), page_id)
  44. streams = data_video['videoStreams']
  45. manifest_url = None
  46. formats = []
  47. f4m_url = streams.get('f4m_stream')
  48. if f4m_url:
  49. manifest_url = f4m_url
  50. formats.extend(self._extract_f4m_formats(
  51. f4m_url + '?hdcore=3.7.0', page_id, f4m_id='hds', fatal=False))
  52. m3u8_url = dict_get(streams, ('m3u8_stream', 'adaptive_stream'))
  53. if m3u8_url:
  54. manifest_url = m3u8_url
  55. m3u8_formats = self._extract_m3u8_formats(
  56. m3u8_url, page_id, 'mp4', 'm3u8_native',
  57. m3u8_id='hls', fatal=False)
  58. formats.extend(m3u8_formats)
  59. progressive_url = dict_get(
  60. streams, ('progressive_hd', 'progressive_high', 'progressive_low', 'other_lr'))
  61. if progressive_url and manifest_url:
  62. qualities_basename = self._search_regex(
  63. r'/([^/]+)\.csmil/',
  64. manifest_url, 'qualities basename', default=None)
  65. if qualities_basename:
  66. QUALITIES_RE = r'((,\d+)+,?)'
  67. qualities = self._search_regex(
  68. QUALITIES_RE, qualities_basename,
  69. 'qualities', default=None)
  70. if qualities:
  71. qualities = list(map(lambda q: int(q), qualities.strip(',').split(',')))
  72. qualities.sort()
  73. http_template = re.sub(QUALITIES_RE, r'%d', qualities_basename)
  74. http_url_basename = url_basename(progressive_url)
  75. if m3u8_formats:
  76. self._sort_formats(m3u8_formats)
  77. m3u8_formats = list(filter(
  78. lambda f: f.get('vcodec') != 'none', m3u8_formats))
  79. if len(qualities) == len(m3u8_formats):
  80. for q, m3u8_format in zip(qualities, m3u8_formats):
  81. f = m3u8_format.copy()
  82. f.update({
  83. 'url': progressive_url.replace(
  84. http_url_basename, http_template % q),
  85. 'format_id': f['format_id'].replace('hls', 'http'),
  86. 'protocol': 'http',
  87. })
  88. formats.append(f)
  89. else:
  90. for q in qualities:
  91. formats.append({
  92. 'url': progressive_url.replace(
  93. http_url_basename, http_template % q),
  94. 'ext': 'mp4',
  95. 'format_id': 'http-%d' % q,
  96. 'tbr': q,
  97. })
  98. onceux_json = self._search_regex(
  99. r'data-onceux-options=["\'](.*?)["\']', webpage, 'data video', default=None)
  100. if onceux_json:
  101. onceux_url = self._parse_json(unescapeHTML(onceux_json), page_id).get('metadataUri')
  102. if onceux_url:
  103. formats.extend(self._extract_once_formats(re.sub(
  104. r'https?://[^/]+', 'http://once.unicornmedia.com', onceux_url),
  105. http_formats_preference=-1))
  106. if not formats:
  107. for quality in ['sd', 'hd']:
  108. # It's actually a link to a flv file
  109. flv_url = streams.get('f4m_{0}'.format(quality))
  110. if flv_url is not None:
  111. formats.append({
  112. 'url': flv_url,
  113. 'ext': 'flv',
  114. 'format_id': quality,
  115. })
  116. self._sort_formats(formats)
  117. return {
  118. 'id': data_video['guid'],
  119. 'display_id': page_id,
  120. 'title': compat_urllib_parse_unquote(data_video['title']),
  121. 'formats': formats,
  122. 'description': self._html_search_meta('description', webpage),
  123. 'thumbnail': self._og_search_thumbnail(webpage),
  124. }