Merge remote-tracking branch 'upstream/master' into bliptv
[youtube-dl] / youtube_dl / extractor / audimedia.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     sanitized_Request,
9 )
10
11
12 class AudiMediaIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?audimedia\.tv/(?:en|de)/vid/(?P<id>[^/?#]+)'
14     _TEST = {
15         'url': 'https://audimedia.tv/en/vid/60-seconds-of-audi-sport-104-2015-wec-bahrain-rookie-test',
16         'md5': '79a8b71c46d49042609795ab59779b66',
17         'info_dict': {
18             'id': '1564',
19             'ext': 'mp4',
20             'title': '60 Seconds of Audi Sport 104/2015 - WEC Bahrain, Rookie Test',
21             'description': 'md5:60e5d30a78ced725f7b8d34370762941',
22             'upload_date': '20151124',
23             'timestamp': 1448354940,
24             'duration': 74022,
25             'view_count': int,
26         }
27     }
28     # extracted from https://audimedia.tv/assets/embed/embedded-player.js (dataSourceAuthToken)
29     _AUTH_TOKEN = 'e25b42847dba18c6c8816d5d8ce94c326e06823ebf0859ed164b3ba169be97f2'
30
31     def _real_extract(self, url):
32         display_id = self._match_id(url)
33         webpage = self._download_webpage(url, display_id)
34         
35         raw_payload = self._search_regex(r'<script[^>]+class="amtv-embed"[^>]+id="([^"]+)"', webpage, 'raw payload');
36         _, stage_mode, video_id, lang = raw_payload.split('-')
37
38         # TODO: handle s and e stage_mode (live streams and ended live streams)
39         if stage_mode not in ('s', 'e'):
40             request = sanitized_Request(
41                 'https://audimedia.tv/api/video/v1/videos/%s?embed[]=video_versions&embed[]=thumbnail_image&where[content_language_iso]=%s' % (video_id, lang),
42                 headers={'X-Auth-Token': self._AUTH_TOKEN})
43             json_data = self._download_json(request, video_id)['results']
44             formats = []
45
46             stream_url_hls = json_data.get('stream_url_hls')
47             if stream_url_hls:
48                 m3u8_formats = self._extract_m3u8_formats(stream_url_hls, video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
49                 if m3u8_formats:
50                     formats.extend(m3u8_formats)
51
52             stream_url_hds = json_data.get('stream_url_hds')
53             if stream_url_hds:
54                 f4m_formats = self._extract_f4m_formats(json_data.get('stream_url_hds') + '?hdcore=3.4.0', video_id, -1, f4m_id='hds', fatal=False)
55                 if f4m_formats:
56                     formats.extend(f4m_formats)
57
58             for video_version in json_data.get('video_versions'):
59                 video_version_url = video_version.get('download_url') or video_version.get('stream_url')
60                 if not video_version_url:
61                     continue
62                 formats.append({
63                     'url': video_version_url,
64                     'width': int_or_none(video_version.get('width')),
65                     'height': int_or_none(video_version.get('height')),
66                     'abr': int_or_none(video_version.get('audio_bitrate')),
67                     'vbr': int_or_none(video_version.get('video_bitrate')),
68                 })
69             self._sort_formats(formats)
70
71             return {
72                 'id': video_id,
73                 'title': json_data['title'],
74                 'description': json_data.get('subtitle'),
75                 'thumbnail': json_data.get('thumbnail_image', {}).get('file'),
76                 'timestamp': parse_iso8601(json_data.get('publication_date')),
77                 'duration': int_or_none(json_data.get('duration')),
78                 'view_count': int_or_none(json_data.get('view_count')),
79                 'formats': formats,
80             }