[youtube] fix extraction for embed restricted live streams(fixes #16433)
[youtube-dl] / youtube_dl / extractor / rentv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_str
6 from ..utils import (
7     determine_ext,
8     int_or_none,
9 )
10
11
12 class RENTVIE(InfoExtractor):
13     _VALID_URL = r'(?:rentv:|https?://(?:www\.)?ren\.tv/(?:player|video/epizod)/)(?P<id>\d+)'
14     _TESTS = [{
15         'url': 'http://ren.tv/video/epizod/118577',
16         'md5': 'd91851bf9af73c0ad9b2cdf76c127fbb',
17         'info_dict': {
18             'id': '118577',
19             'ext': 'mp4',
20             'title': 'Документальный спецпроект: "Промывка мозгов. Технологии XXI века"',
21             'timestamp': 1472230800,
22             'upload_date': '20160826',
23         }
24     }, {
25         'url': 'http://ren.tv/player/118577',
26         'only_matching': True,
27     }, {
28         'url': 'rentv:118577',
29         'only_matching': True,
30     }]
31
32     def _real_extract(self, url):
33         video_id = self._match_id(url)
34         webpage = self._download_webpage('http://ren.tv/player/' + video_id, video_id)
35         config = self._parse_json(self._search_regex(
36             r'config\s*=\s*({.+})\s*;', webpage, 'config'), video_id)
37         title = config['title']
38         formats = []
39         for video in config['src']:
40             src = video.get('src')
41             if not src or not isinstance(src, compat_str):
42                 continue
43             ext = determine_ext(src)
44             if ext == 'm3u8':
45                 formats.extend(self._extract_m3u8_formats(
46                     src, video_id, 'mp4', entry_protocol='m3u8_native',
47                     m3u8_id='hls', fatal=False))
48             else:
49                 formats.append({
50                     'url': src,
51                 })
52         self._sort_formats(formats)
53         return {
54             'id': video_id,
55             'title': title,
56             'description': config.get('description'),
57             'thumbnail': config.get('image'),
58             'duration': int_or_none(config.get('duration')),
59             'timestamp': int_or_none(config.get('date')),
60             'formats': formats,
61         }
62
63
64 class RENTVArticleIE(InfoExtractor):
65     _VALID_URL = r'https?://(?:www\.)?ren\.tv/novosti/\d{4}-\d{2}-\d{2}/(?P<id>[^/?#]+)'
66     _TESTS = [{
67         'url': 'http://ren.tv/novosti/2016-10-26/video-mikroavtobus-popavshiy-v-dtp-s-gruzovikami-v-podmoskove-prevratilsya-v',
68         'md5': 'ebd63c4680b167693745ab91343df1d6',
69         'info_dict': {
70             'id': '136472',
71             'ext': 'mp4',
72             'title': 'Видео: микроавтобус, попавший в ДТП с грузовиками в Подмосковье, превратился в груду металла',
73             'description': 'Жертвами столкновения двух фур и микроавтобуса, по последним данным, стали семь человек.',
74         }
75     }, {
76         # TODO: invalid m3u8
77         'url': 'http://ren.tv/novosti/2015-09-25/sluchaynyy-prohozhiy-poymal-avtougonshchika-v-murmanske-video',
78         'info_dict': {
79             'id': 'playlist',
80             'ext': 'mp4',
81             'title': 'Случайный прохожий поймал автоугонщика в Мурманске. ВИДЕО | РЕН ТВ',
82             'uploader': 'ren.tv',
83         },
84         'params': {
85             # m3u8 downloads
86             'skip_download': True,
87         },
88         'skip': True,
89     }]
90
91     def _real_extract(self, url):
92         display_id = self._match_id(url)
93         webpage = self._download_webpage(url, display_id)
94         drupal_settings = self._parse_json(self._search_regex(
95             r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);',
96             webpage, 'drupal settings'), display_id)
97
98         entries = []
99         for config_profile in drupal_settings.get('ren_jwplayer', {}).values():
100             media_id = config_profile.get('mediaid')
101             if not media_id:
102                 continue
103             media_id = compat_str(media_id)
104             entries.append(self.url_result('rentv:' + media_id, 'RENTV', media_id))
105         return self.playlist_result(entries, display_id)