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.

101 lines
3.9 KiB

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