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.

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