logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

amp.py (4088B)


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