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.

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