[eagleplatform] extract mp4 url and fix thumbnail url
[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     def _handle_error(self, response):
52         status = int_or_none(response.get('status', 200))
53         if status != 200:
54             raise ExtractorError(' '.join(response['errors']), expected=True)
55
56     def _get_video_url(self, url_or_request, video_id, note='Downloading JSON metadata'):
57         response = self._download_json(url_or_request, video_id, note)
58         self._handle_error(response)
59         return response['data'][0]
60
61     def _real_extract(self, url):
62         mobj = re.match(self._VALID_URL, url)
63         host, video_id = mobj.group('custom_host') or mobj.group('host'), mobj.group('id')
64
65         player_data = self._download_json(
66             'http://%s/api/player_data?id=%s' % (host, video_id), video_id)
67
68         media = player_data['data']['playlist']['viewports'][0]['medialist'][0]
69
70         title = media['title']
71         description = media.get('description')
72         thumbnail = self._proto_relative_url(media.get('snapshot'))
73         duration = int_or_none(media.get('duration'))
74         view_count = int_or_none(media.get('views'))
75
76         age_restriction = media.get('age_restriction')
77         age_limit = None
78         if age_restriction:
79             age_limit = 0 if age_restriction == 'allow_all' else 18
80
81         secure_m3u8 = self._proto_relative_url(media['sources']['secure_m3u8']['auto'])
82
83         m3u8_url = self._get_video_url(secure_m3u8, video_id, 'Downloading m3u8 JSON')
84         formats = self._extract_m3u8_formats(
85             m3u8_url, video_id,
86             'mp4', entry_protocol='m3u8_native')
87
88         mp4_url = self._get_video_url(
89             secure_m3u8.replace("m3u8", "mp4").replace("hlsvod", "mp4").replace("hls", "mp4"),
90             video_id, 'Downloading mp4 JSON')
91         formats.append({'url': mp4_url, 'format_id': 'mp4'})
92
93         self._sort_formats(formats)
94
95         return {
96             'id': video_id,
97             'title': title,
98             'description': description,
99             'thumbnail': thumbnail,
100             'duration': duration,
101             'view_count': view_count,
102             'age_limit': age_limit,
103             'formats': formats,
104         }