]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/amp.py
8545681bead617b2ffedeee04212e032bdc3406e
[youtubedl] / youtube_dl / extractor / amp.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6 int_or_none,
7 parse_iso8601,
8 )
9
10
11 class AMPIE(InfoExtractor):
12 # parse Akamai Adaptive Media Player feed
13 def _extract_feed_info(self, url):
14 item = self._download_json(
15 url, None, 'Downloading Akamai AMP feed',
16 'Unable to download Akamai AMP feed')['channel']['item']
17
18 video_id = item['guid']
19
20 def get_media_node(name, default=None):
21 media_name = 'media-%s' % name
22 media_group = item.get('media-group') or item
23 return media_group.get(media_name) or item.get(media_name) or item.get(name, default)
24
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['@attributes']
32 thumbnails.append({
33 'url': self._proto_relative_url(thumbnail['url'], 'http:'),
34 'width': int_or_none(thumbnail.get('width')),
35 'height': int_or_none(thumbnail.get('height')),
36 })
37
38 subtitles = {}
39 media_subtitle = get_media_node('subTitle')
40 if media_subtitle:
41 if isinstance(media_subtitle, dict):
42 media_subtitle = [media_subtitle]
43 for subtitle_data in media_subtitle:
44 subtitle = subtitle_data['@attributes']
45 lang = subtitle.get('lang') or 'en'
46 subtitles[lang] = [{'url': subtitle['href']}]
47
48 formats = []
49 media_content = get_media_node('content')
50 if isinstance(media_content, dict):
51 media_content = [media_content]
52 for media_data in media_content:
53 media = media_data['@attributes']
54 media_type = media['type']
55 if media_type in ('video/f4m', 'application/f4m+xml'):
56 formats.extend(self._extract_f4m_formats(
57 media['url'] + '?hdcore=3.4.0&plugin=aasp-3.4.0.132.124',
58 video_id, f4m_id='hds', fatal=False))
59 elif media_type == 'application/x-mpegURL':
60 formats.extend(self._extract_m3u8_formats(
61 media['url'], video_id, 'mp4', m3u8_id='hls', fatal=False))
62 else:
63 formats.append({
64 'format_id': media_data.get('media-category', {}).get('@attributes', {}).get('label'),
65 'url': media['url'],
66 'tbr': int_or_none(media.get('bitrate')),
67 'filesize': int_or_none(media.get('fileSize')),
68 })
69
70 self._sort_formats(formats)
71
72 timestamp = parse_iso8601(item.get('pubDate'), ' ') or parse_iso8601(item.get('dc-date'))
73
74 return {
75 'id': video_id,
76 'title': get_media_node('title'),
77 'description': get_media_node('description'),
78 'thumbnails': thumbnails,
79 'timestamp': timestamp,
80 'duration': int_or_none(media_content[0].get('@attributes', {}).get('duration')),
81 'subtitles': subtitles,
82 'formats': formats,
83 }