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