[amp] Add generic extractor for Akamai AMP feeds and use it in dramafever and foxnews...
[youtube-dl] / 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     def _get_media_node(self, item, name, default=None):
13         media_name = 'media-%s' % name
14         media_group = item.get('media-group') or item
15         return media_group.get(media_name) or item.get(media_name) or item.get(name, default)
16
17     # parse Akamai Adaptive Media Player feed
18     def _extract_feed_info(self, url):
19         item = self._download_json(
20             url, None,
21             'Downloading Akamai AMP feed',
22             'Unable to download Akamai AMP feed'
23             )['channel']['item']
24
25         video_id = item['guid']
26         
27         thumbnails = []
28         media_thumbnail = self._get_media_node(item, '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 = self._get_media_node(item, '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 = self._get_media_node(item, 'content')
52         if isinstance(media_content, dict):
53             media_content = [media_content]
54         for media_data in media_content:
55             media = media_data['@attributes']
56             media_type = media['type']
57             if media_type == 'video/f4m':
58                 f4m_formats = self._extract_f4m_formats(media['url'] + '?hdcore=3.4.0&plugin=aasp-3.4.0.132.124', video_id, f4m_id='hds', fatal=False)
59                 if f4m_formats:
60                     formats.extend(f4m_formats)
61             elif media_type == 'application/x-mpegURL':
62                 m3u8_formats = self._extract_m3u8_formats(media['url'], video_id, m3u8_id='hls', fatal=False)
63                 if m3u8_formats:
64                     formats.extend(m3u8_formats)
65             else:
66                 formats.append({
67                     'format_id': media_data['media-category']['@attributes']['label'],
68                     'url': media['url'],
69                     'preference': 1,
70                     'vbr': int_or_none(media.get('bitrate')),
71                     'filesize': int_or_none(media.get('fileSize')),
72                 })
73
74         self._sort_formats(formats)
75
76         return {
77             'id': video_id,
78             'title': self._get_media_node(item, 'title'),
79             'description': self._get_media_node(item, 'description'),
80             'thumbnails': thumbnails,
81             'timestamp': parse_iso8601(item.get('pubDate'), ' '),
82             'duration': int_or_none(media_content[0].get('@attributes', {}).get('duration')),
83             'formats': formats,
84         }