[youtube] Fix extraction.
[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 )
9
10
11 class AudiMediaIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?audi-mediacenter\.com/(?:en|de)/audimediatv/(?:video/)?(?P<id>[^/?#]+)'
13     _TESTS = [{
14         'url': 'https://www.audi-mediacenter.com/en/audimediatv/60-seconds-of-audi-sport-104-2015-wec-bahrain-rookie-test-1467',
15         'md5': '79a8b71c46d49042609795ab59779b66',
16         'info_dict': {
17             'id': '1565',
18             'ext': 'mp4',
19             'title': '60 Seconds of Audi Sport 104/2015 - WEC Bahrain, Rookie Test',
20             'description': 'md5:60e5d30a78ced725f7b8d34370762941',
21             'upload_date': '20151124',
22             'timestamp': 1448354940,
23             'duration': 74022,
24             'view_count': int,
25         }
26     }, {
27         'url': 'https://www.audi-mediacenter.com/en/audimediatv/video/60-seconds-of-audi-sport-104-2015-wec-bahrain-rookie-test-2991',
28         'only_matching': True,
29     }]
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([
36             r'class="amtv-embed"[^>]+id="([0-9a-z-]+)"',
37             r'id="([0-9a-z-]+)"[^>]+class="amtv-embed"',
38             r'class=\\"amtv-embed\\"[^>]+id=\\"([0-9a-z-]+)\\"',
39             r'id=\\"([0-9a-z-]+)\\"[^>]+class=\\"amtv-embed\\"',
40             r'id=(?:\\)?"(amtve-[a-z]-\d+-[a-z]{2})',
41         ], webpage, 'raw payload')
42         _, stage_mode, video_id, _ = raw_payload.split('-')
43
44         # TODO: handle s and e stage_mode (live streams and ended live streams)
45         if stage_mode not in ('s', 'e'):
46             video_data = self._download_json(
47                 'https://www.audimedia.tv/api/video/v1/videos/' + video_id,
48                 video_id, query={
49                     'embed[]': ['video_versions', 'thumbnail_image'],
50                 })['results']
51             formats = []
52
53             stream_url_hls = video_data.get('stream_url_hls')
54             if stream_url_hls:
55                 formats.extend(self._extract_m3u8_formats(
56                     stream_url_hls, video_id, 'mp4',
57                     entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
58
59             stream_url_hds = video_data.get('stream_url_hds')
60             if stream_url_hds:
61                 formats.extend(self._extract_f4m_formats(
62                     stream_url_hds + '?hdcore=3.4.0',
63                     video_id, f4m_id='hds', fatal=False))
64
65             for video_version in video_data.get('video_versions', []):
66                 video_version_url = video_version.get('download_url') or video_version.get('stream_url')
67                 if not video_version_url:
68                     continue
69                 f = {
70                     'url': video_version_url,
71                     'width': int_or_none(video_version.get('width')),
72                     'height': int_or_none(video_version.get('height')),
73                     'abr': int_or_none(video_version.get('audio_bitrate')),
74                     'vbr': int_or_none(video_version.get('video_bitrate')),
75                 }
76                 bitrate = self._search_regex(r'(\d+)k', video_version_url, 'bitrate', default=None)
77                 if bitrate:
78                     f.update({
79                         'format_id': 'http-%s' % bitrate,
80                     })
81                 formats.append(f)
82             self._sort_formats(formats)
83
84             return {
85                 'id': video_id,
86                 'title': video_data['title'],
87                 'description': video_data.get('subtitle'),
88                 'thumbnail': video_data.get('thumbnail_image', {}).get('file'),
89                 'timestamp': parse_iso8601(video_data.get('publication_date')),
90                 'duration': int_or_none(video_data.get('duration')),
91                 'view_count': int_or_none(video_data.get('view_count')),
92                 'formats': formats,
93             }