Merge pull request #9195 from remitamine/ffmpeg-pipe
[youtube-dl] / youtube_dl / extractor / eagleplatform.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_HTTPError
8 from ..utils import (
9     ExtractorError,
10     int_or_none,
11 )
12
13
14 class EaglePlatformIE(InfoExtractor):
15     _VALID_URL = r'''(?x)
16                     (?:
17                         eagleplatform:(?P<custom_host>[^/]+):|
18                         https?://(?P<host>.+?\.media\.eagleplatform\.com)/index/player\?.*\brecord_id=
19                     )
20                     (?P<id>\d+)
21                 '''
22     _TESTS = [{
23         # http://lenta.ru/news/2015/03/06/navalny/
24         'url': 'http://lentaru.media.eagleplatform.com/index/player?player=new&record_id=227304&player_template_id=5201',
25         'md5': '70f5187fb620f2c1d503b3b22fd4efe3',
26         'info_dict': {
27             'id': '227304',
28             'ext': 'mp4',
29             'title': 'Навальный вышел на свободу',
30             'description': 'md5:d97861ac9ae77377f3f20eaf9d04b4f5',
31             'thumbnail': 're:^https?://.*\.jpg$',
32             'duration': 87,
33             'view_count': int,
34             'age_limit': 0,
35         },
36     }, {
37         # http://muz-tv.ru/play/7129/
38         # http://media.clipyou.ru/index/player?record_id=12820&width=730&height=415&autoplay=true
39         'url': 'eagleplatform:media.clipyou.ru:12820',
40         'md5': '90b26344ba442c8e44aa4cf8f301164a',
41         'info_dict': {
42             'id': '12820',
43             'ext': 'mp4',
44             'title': "'O Sole Mio",
45             'thumbnail': 're:^https?://.*\.jpg$',
46             'duration': 216,
47             'view_count': int,
48         },
49         'skip': 'Georestricted',
50     }]
51
52     @staticmethod
53     def _handle_error(response):
54         status = int_or_none(response.get('status', 200))
55         if status != 200:
56             raise ExtractorError(' '.join(response['errors']), expected=True)
57
58     def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'):
59         try:
60             response = super(EaglePlatformIE, self)._download_json(url_or_request, video_id, note)
61         except ExtractorError as ee:
62             if isinstance(ee.cause, compat_HTTPError):
63                 response = self._parse_json(ee.cause.read().decode('utf-8'), video_id)
64                 self._handle_error(response)
65             raise
66         return response
67
68     def _get_video_url(self, url_or_request, video_id, note='Downloading JSON metadata'):
69         return self._download_json(url_or_request, video_id, note)['data'][0]
70
71     def _real_extract(self, url):
72         mobj = re.match(self._VALID_URL, url)
73         host, video_id = mobj.group('custom_host') or mobj.group('host'), mobj.group('id')
74
75         player_data = self._download_json(
76             'http://%s/api/player_data?id=%s' % (host, video_id), video_id)
77
78         media = player_data['data']['playlist']['viewports'][0]['medialist'][0]
79
80         title = media['title']
81         description = media.get('description')
82         thumbnail = self._proto_relative_url(media.get('snapshot'), 'http:')
83         duration = int_or_none(media.get('duration'))
84         view_count = int_or_none(media.get('views'))
85
86         age_restriction = media.get('age_restriction')
87         age_limit = None
88         if age_restriction:
89             age_limit = 0 if age_restriction == 'allow_all' else 18
90
91         secure_m3u8 = self._proto_relative_url(media['sources']['secure_m3u8']['auto'], 'http:')
92
93         m3u8_url = self._get_video_url(secure_m3u8, video_id, 'Downloading m3u8 JSON')
94         formats = self._extract_m3u8_formats(
95             m3u8_url, video_id,
96             'mp4', entry_protocol='m3u8_native', m3u8_id='hls')
97
98         mp4_url = self._get_video_url(
99             # Secure mp4 URL is constructed according to Player.prototype.mp4 from
100             # http://lentaru.media.eagleplatform.com/player/player.js
101             re.sub(r'm3u8|hlsvod|hls|f4m', 'mp4', secure_m3u8),
102             video_id, 'Downloading mp4 JSON')
103         formats.append({'url': mp4_url, 'format_id': 'mp4'})
104
105         self._sort_formats(formats)
106
107         return {
108             'id': video_id,
109             'title': title,
110             'description': description,
111             'thumbnail': thumbnail,
112             'duration': duration,
113             'view_count': view_count,
114             'age_limit': age_limit,
115             'formats': formats,
116         }