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