]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/onet.py
[Onet,ClipRs] Add new extractor for onet.tv and use it for clip.rs
[yt-dlp.git] / youtube_dl / extractor / onet.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 determine_ext,
9 ExtractorError,
10 float_or_none,
11 get_element_by_class,
12 int_or_none,
13 js_to_json,
14 parse_iso8601,
15 remove_start,
16 strip_or_none,
17 url_basename,
18 )
19
20
21 class OnetBaseIE(InfoExtractor):
22 def _search_mvp_id(self, webpage):
23 return self._search_regex(
24 r'id=(["\'])mvp:(?P<id>.+?)\1', webpage, 'mvp id', group='id')
25
26 def _extract_from_id(self, video_id, webpage):
27 response = self._download_json(
28 'http://qi.ckm.onetapi.pl/', video_id,
29 query={
30 'body[id]': video_id,
31 'body[jsonrpc]': '2.0',
32 'body[method]': 'get_asset_detail',
33 'body[params][ID_Publikacji]': video_id,
34 'body[params][Service]': 'www.onet.pl',
35 'content-type': 'application/jsonp',
36 'x-onet-app': 'player.front.onetapi.pl',
37 })
38
39 error = response.get('error')
40 if error:
41 raise ExtractorError(
42 '%s said: %s' % (self.IE_NAME, error['message']), expected=True)
43
44 video = response['result'].get('0')
45
46 formats = []
47 for _, formats_dict in video['formats'].items():
48 if not isinstance(formats_dict, dict):
49 continue
50 for format_id, format_list in formats_dict.items():
51 if not isinstance(format_list, list):
52 continue
53 for f in format_list:
54 video_url = f.get('url')
55 if not video_url:
56 continue
57 ext = determine_ext(video_url)
58 if format_id == 'ism':
59 # TODO: Support Microsoft Smooth Streaming
60 continue
61 elif ext == 'mpd':
62 # TODO: Current DASH formats are broken - $Time$ pattern in
63 # <SegmentTemplate> not implemented yet
64 # formats.extend(self._extract_mpd_formats(
65 # video_url, video_id, mpd_id='dash', fatal=False))
66 continue
67 else:
68 formats.append({
69 'url': video_url,
70 'format_id': format_id,
71 'height': int_or_none(f.get('vertical_resolution')),
72 'width': int_or_none(f.get('horizontal_resolution')),
73 'abr': float_or_none(f.get('audio_bitrate')),
74 'vbr': float_or_none(f.get('video_bitrate')),
75 })
76 self._sort_formats(formats)
77
78 meta = video.get('meta', {})
79
80 title = self._og_search_title(webpage, default=None) or meta['title']
81 description = self._og_search_description(webpage, default=None) or meta.get('description')
82 duration = meta.get('length') or meta.get('lenght')
83 timestamp = parse_iso8601(meta.get('addDate'), ' ')
84
85 return {
86 'id': video_id,
87 'title': title,
88 'description': description,
89 'duration': duration,
90 'timestamp': timestamp,
91 'formats': formats,
92 }
93
94
95 class OnetIE(OnetBaseIE):
96 _VALID_URL = 'https?://(?:www\.)?onet\.tv/[a-z]/[a-z]+/(?P<display_id>[0-9a-z-]+)/(?P<id>[0-9a-z]+)'
97 IE_NAME = 'onet.tv'
98
99 _TEST = {
100 'url': 'http://onet.tv/k/openerfestival/open-er-festival-2016-najdziwniejsze-wymagania-gwiazd/qbpyqc',
101 'info_dict': {
102 'id': 'qbpyqc',
103 'display_id': 'open-er-festival-2016-najdziwniejsze-wymagania-gwiazd',
104 'ext': 'mp4',
105 'title': 'Open\'er Festival 2016: najdziwniejsze wymagania gwiazd',
106 'description': 'Trzy samochody, których nigdy nie użyto, prywatne spa, hotel dekorowany czarnym suknem czy nielegalne używki. Organizatorzy koncertów i festiwali muszą stawać przed nie lada wyzwaniem zapraszając gwia...',
107 'upload_date': '20160705',
108 'timestamp': 1467721580,
109 },
110 }
111
112 def _real_extract(self, url):
113 mobj = re.match(self._VALID_URL, url)
114 display_id, video_id = mobj.group('display_id', 'id')
115
116 webpage = self._download_webpage(url, display_id)
117
118 mvp_id = self._search_mvp_id(webpage)
119
120 info_dict = self._extract_from_id(mvp_id, webpage)
121 info_dict.update({
122 'id': video_id,
123 'display_id': display_id,
124 })
125
126 return info_dict
127
128
129 class OnetChannelIE(OnetBaseIE):
130 _VALID_URL = r'https?://(?:www\.)?onet\.tv/[a-z]/(?P<id>[a-z]+)(?:[?#]|$)'
131 IE_NAME = 'onet.tv:channel'
132
133 _TEST = {
134 'url': 'http://onet.tv/k/openerfestival',
135 'info_dict': {
136 'id': 'openerfestival',
137 'title': 'Open\'er Festival Live',
138 'description': 'Dziękujemy, że oglądaliście transmisje. Zobaczcie nasze relacje i wywiady z artystami.',
139 },
140 'playlist_mincount': 46,
141 }
142
143 def _real_extract(self, url):
144 channel_id = self._match_id(url)
145
146 webpage = self._download_webpage(url, channel_id)
147
148 current_clip_info = self._parse_json(self._search_regex(
149 r'var\s+currentClip\s*=\s*({[^}]+})', webpage, 'video info'), channel_id,
150 transform_source=lambda s: js_to_json(re.sub(r'\'\s*\+\s*\'', '', s)))
151 video_id = remove_start(current_clip_info['ckmId'], 'mvp:')
152 video_name = url_basename(current_clip_info['url'])
153
154 if self._downloader.params.get('noplaylist'):
155 self.to_screen(
156 'Downloading just video %s because of --no-playlist' % video_name)
157 return self._extract_from_id(video_id, webpage)
158
159 self.to_screen(
160 'Downloading channel %s - add --no-playlist to just download video %s' % (
161 channel_id, video_name))
162 matches = re.findall(
163 r'<a[^>]+href=[\'"](https?://(?:www\.)?onet\.tv/[a-z]/[a-z]+/[0-9a-z-]+/[0-9a-z]+)',
164 webpage)
165 entries = [
166 self.url_result(video_link, OnetIE.ie_key())
167 for video_link in matches]
168
169 channel_title = strip_or_none(get_element_by_class('o_channelName', webpage))
170 channel_description = strip_or_none(get_element_by_class('o_channelDesc', webpage))
171 return self.playlist_result(entries, channel_id, channel_title, channel_description)