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