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.

102 lines
4.0 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. determine_ext,
  6. ExtractorError,
  7. int_or_none,
  8. mimetype2ext,
  9. parse_iso8601,
  10. url_or_none,
  11. )
  12. class AMPIE(InfoExtractor):
  13. # parse Akamai Adaptive Media Player feed
  14. def _extract_feed_info(self, url):
  15. feed = self._download_json(
  16. url, None, 'Downloading Akamai AMP feed',
  17. 'Unable to download Akamai AMP feed')
  18. item = feed.get('channel', {}).get('item')
  19. if not item:
  20. raise ExtractorError('%s said: %s' % (self.IE_NAME, feed['error']))
  21. video_id = item['guid']
  22. def get_media_node(name, default=None):
  23. media_name = 'media-%s' % name
  24. media_group = item.get('media-group') or item
  25. return media_group.get(media_name) or item.get(media_name) or item.get(name, default)
  26. thumbnails = []
  27. media_thumbnail = get_media_node('thumbnail')
  28. if media_thumbnail:
  29. if isinstance(media_thumbnail, dict):
  30. media_thumbnail = [media_thumbnail]
  31. for thumbnail_data in media_thumbnail:
  32. thumbnail = thumbnail_data.get('@attributes', {})
  33. thumbnail_url = url_or_none(thumbnail.get('url'))
  34. if not thumbnail_url:
  35. continue
  36. thumbnails.append({
  37. 'url': self._proto_relative_url(thumbnail_url, 'http:'),
  38. 'width': int_or_none(thumbnail.get('width')),
  39. 'height': int_or_none(thumbnail.get('height')),
  40. })
  41. subtitles = {}
  42. media_subtitle = get_media_node('subTitle')
  43. if media_subtitle:
  44. if isinstance(media_subtitle, dict):
  45. media_subtitle = [media_subtitle]
  46. for subtitle_data in media_subtitle:
  47. subtitle = subtitle_data.get('@attributes', {})
  48. subtitle_href = url_or_none(subtitle.get('href'))
  49. if not subtitle_href:
  50. continue
  51. subtitles.setdefault(subtitle.get('lang') or 'en', []).append({
  52. 'url': subtitle_href,
  53. 'ext': mimetype2ext(subtitle.get('type')) or determine_ext(subtitle_href),
  54. })
  55. formats = []
  56. media_content = get_media_node('content')
  57. if isinstance(media_content, dict):
  58. media_content = [media_content]
  59. for media_data in media_content:
  60. media = media_data.get('@attributes', {})
  61. media_url = url_or_none(media.get('url'))
  62. if not media_url:
  63. continue
  64. ext = mimetype2ext(media.get('type')) or determine_ext(media_url)
  65. if ext == 'f4m':
  66. formats.extend(self._extract_f4m_formats(
  67. media_url + '?hdcore=3.4.0&plugin=aasp-3.4.0.132.124',
  68. video_id, f4m_id='hds', fatal=False))
  69. elif ext == 'm3u8':
  70. formats.extend(self._extract_m3u8_formats(
  71. media_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
  72. else:
  73. formats.append({
  74. 'format_id': media_data.get('media-category', {}).get('@attributes', {}).get('label'),
  75. 'url': media_url,
  76. 'tbr': int_or_none(media.get('bitrate')),
  77. 'filesize': int_or_none(media.get('fileSize')),
  78. 'ext': ext,
  79. })
  80. self._sort_formats(formats)
  81. timestamp = parse_iso8601(item.get('pubDate'), ' ') or parse_iso8601(item.get('dc-date'))
  82. return {
  83. 'id': video_id,
  84. 'title': get_media_node('title'),
  85. 'description': get_media_node('description'),
  86. 'thumbnails': thumbnails,
  87. 'timestamp': timestamp,
  88. 'duration': int_or_none(media_content[0].get('@attributes', {}).get('duration')),
  89. 'subtitles': subtitles,
  90. 'formats': formats,
  91. }