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