]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/tvp.py
Completely change project name to yt-dlp (#85)
[yt-dlp.git] / yt_dlp / extractor / tvp.py
CommitLineData
6f8cb242 1# coding: utf-8
24144e3b 2from __future__ import unicode_literals
5137ebac 3
d9308378 4import itertools
29f400b9
TF
5import re
6
5137ebac 7from .common import InfoExtractor
6e3c2047 8from ..utils import (
6e3c2047 9 clean_html,
d9308378 10 determine_ext,
6e3c2047 11 ExtractorError,
d9308378
S
12 get_element_by_attribute,
13 orderedSet,
6e3c2047 14)
c3a3028f 15
5137ebac 16
6f8cb242
S
17class TVPIE(InfoExtractor):
18 IE_NAME = 'tvp'
19 IE_DESC = 'Telewizja Polska'
4e599194 20 _VALID_URL = r'https?://[^/]+\.tvp\.(?:pl|info)/(?:video/(?:[^,\s]*,)*|(?:(?!\d+/)[^/]+/)*)(?P<id>\d+)'
fb4b030a
PH
21
22 _TESTS = [{
4e599194 23 'url': 'https://vod.tvp.pl/video/czas-honoru,i-seria-odc-13,194536',
3c964737 24 'md5': 'a21eb0aa862f25414430f15fdfb9e76c',
fb4b030a
PH
25 'info_dict': {
26 'id': '194536',
27 'ext': 'mp4',
3c964737
S
28 'title': 'Czas honoru, odc. 13 – Władek',
29 'description': 'md5:437f48b93558370b031740546b696e24',
fb4b030a
PH
30 },
31 }, {
32 'url': 'http://www.tvp.pl/there-can-be-anything-so-i-shortened-it/17916176',
6e3c2047 33 'md5': 'b0005b542e5b4de643a9690326ab1257',
fb4b030a
PH
34 'info_dict': {
35 'id': '17916176',
36 'ext': 'mp4',
37 'title': 'TVP Gorzów pokaże filmy studentów z podroży dookoła świata',
fdd0b8f8
RA
38 'description': 'TVP Gorzów pokaże filmy studentów z podroży dookoła świata',
39 },
40 }, {
41 # page id is not the same as video id(#7799)
4e599194
RB
42 'url': 'https://wiadomosci.tvp.pl/33908820/28092017-1930',
43 'md5': '84cd3c8aec4840046e5ab712416b73d0',
fdd0b8f8 44 'info_dict': {
4e599194 45 'id': '33908820',
fdd0b8f8 46 'ext': 'mp4',
4e599194
RB
47 'title': 'Wiadomości, 28.09.2017, 19:30',
48 'description': 'Wydanie główne codziennego serwisu informacyjnego.'
fb4b030a 49 },
3c964737 50 'skip': 'HTTP Error 404: Not Found',
fb4b030a
PH
51 }, {
52 'url': 'http://vod.tvp.pl/seriale/obyczajowe/na-sygnale/sezon-2-27-/odc-39/17834272',
6f8cb242
S
53 'only_matching': True,
54 }, {
55 'url': 'http://wiadomosci.tvp.pl/25169746/24052016-1200',
56 'only_matching': True,
57 }, {
58 'url': 'http://krakow.tvp.pl/25511623/25lecie-mck-wyjatkowe-miejsce-na-mapie-krakowa',
59 'only_matching': True,
60 }, {
61 'url': 'http://teleexpress.tvp.pl/25522307/wierni-wzieli-udzial-w-procesjach',
62 'only_matching': True,
63 }, {
64 'url': 'http://sport.tvp.pl/25522165/krychowiak-uspokaja-w-sprawie-kontuzji-dwa-tygodnie-to-maksimum',
65 'only_matching': True,
66 }, {
67 'url': 'http://www.tvp.info/25511919/trwa-rewolucja-wladza-zdecydowala-sie-na-pogwalcenie-konstytucji',
68 'only_matching': True,
fb4b030a 69 }]
5137ebac 70
fdd0b8f8
RA
71 def _real_extract(self, url):
72 page_id = self._match_id(url)
73 webpage = self._download_webpage(url, page_id)
74 video_id = self._search_regex([
75 r'<iframe[^>]+src="[^"]*?object_id=(\d+)',
3d8d44c7
RA
76 r"object_id\s*:\s*'(\d+)'",
77 r'data-video-id="(\d+)"'], webpage, 'video id', default=page_id)
fdd0b8f8
RA
78 return {
79 '_type': 'url_transparent',
80 'url': 'tvp:' + video_id,
3c964737
S
81 'description': self._og_search_description(
82 webpage, default=None) or self._html_search_meta(
83 'description', webpage, default=None),
84 'thumbnail': self._og_search_thumbnail(webpage, default=None),
fdd0b8f8
RA
85 'ie_key': 'TVPEmbed',
86 }
87
88
89class TVPEmbedIE(InfoExtractor):
90 IE_NAME = 'tvp:embed'
91 IE_DESC = 'Telewizja Polska'
92 _VALID_URL = r'(?:tvp:|https?://[^/]+\.tvp\.(?:pl|info)/sess/tvplayer\.php\?.*?object_id=)(?P<id>\d+)'
93
94 _TESTS = [{
3c964737
S
95 'url': 'tvp:194536',
96 'md5': 'a21eb0aa862f25414430f15fdfb9e76c',
97 'info_dict': {
98 'id': '194536',
99 'ext': 'mp4',
100 'title': 'Czas honoru, odc. 13 – Władek',
101 },
102 }, {
34568dc2 103 # not available
fdd0b8f8
RA
104 'url': 'http://www.tvp.pl/sess/tvplayer.php?object_id=22670268',
105 'md5': '8c9cd59d16edabf39331f93bf8a766c7',
106 'info_dict': {
107 'id': '22670268',
108 'ext': 'mp4',
109 'title': 'Panorama, 07.12.2015, 15:40',
110 },
34568dc2 111 'skip': 'Transmisja została zakończona lub materiał niedostępny',
fdd0b8f8
RA
112 }, {
113 'url': 'tvp:22670268',
114 'only_matching': True,
115 }]
116
5137ebac 117 def _real_extract(self, url):
fb4b030a 118 video_id = self._match_id(url)
030aa5d9 119
29f400b9
TF
120 webpage = self._download_webpage(
121 'http://www.tvp.pl/sess/tvplayer.php?object_id=%s' % video_id, video_id)
fb4b030a 122
34568dc2
S
123 error = self._html_search_regex(
124 r'(?s)<p[^>]+\bclass=["\']notAvailable__text["\'][^>]*>(.+?)</p>',
125 webpage, 'error', default=None) or clean_html(
126 get_element_by_attribute('class', 'msg error', webpage))
127 if error:
6e3c2047 128 raise ExtractorError('%s said: %s' % (
34568dc2 129 self.IE_NAME, clean_html(error)), expected=True)
6e3c2047 130
030aa5d9
S
131 title = self._search_regex(
132 r'name\s*:\s*([\'"])Title\1\s*,\s*value\s*:\s*\1(?P<title>.+?)\1',
133 webpage, 'title', group='title')
134 series_title = self._search_regex(
135 r'name\s*:\s*([\'"])SeriesTitle\1\s*,\s*value\s*:\s*\1(?P<series>.+?)\1',
29f400b9 136 webpage, 'series', group='series', default=None)
030aa5d9
S
137 if series_title:
138 title = '%s, %s' % (series_title, title)
139
140 thumbnail = self._search_regex(
141 r"poster\s*:\s*'([^']+)'", webpage, 'thumbnail', default=None)
fb4b030a 142
29f400b9 143 video_url = self._search_regex(
6e3c2047
RA
144 r'0:{src:([\'"])(?P<url>.*?)\1', webpage,
145 'formats', group='url', default=None)
146 if not video_url or 'material_niedostepny.mp4' in video_url:
29f400b9
TF
147 video_url = self._download_json(
148 'http://www.tvp.pl/pub/stat/videofileinfo?video_id=%s' % video_id,
149 video_id)['video_url']
150
6e3c2047
RA
151 formats = []
152 video_url_base = self._search_regex(
153 r'(https?://.+?/video)(?:\.(?:ism|f4m|m3u8)|-\d+\.mp4)',
154 video_url, 'video base url', default=None)
155 if video_url_base:
9513c1eb
S
156 # TODO: <Group> found instead of <AdaptationSet> in MPD manifest.
157 # It's not mentioned in MPEG-DASH standard. Figure that out.
6e3c2047
RA
158 # formats.extend(self._extract_mpd_formats(
159 # video_url_base + '.ism/video.mpd',
160 # video_id, mpd_id='dash', fatal=False))
639e3b5c
RA
161 formats.extend(self._extract_ism_formats(
162 video_url_base + '.ism/Manifest',
163 video_id, 'mss', fatal=False))
6e3c2047
RA
164 formats.extend(self._extract_f4m_formats(
165 video_url_base + '.ism/video.f4m',
166 video_id, f4m_id='hds', fatal=False))
167 m3u8_formats = self._extract_m3u8_formats(
168 video_url_base + '.ism/video.m3u8', video_id,
169 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
170 self._sort_formats(m3u8_formats)
171 m3u8_formats = list(filter(
ff99fe52 172 lambda f: f.get('vcodec') != 'none', m3u8_formats))
6e3c2047
RA
173 formats.extend(m3u8_formats)
174 for i, m3u8_format in enumerate(m3u8_formats, 2):
175 http_url = '%s-%d.mp4' % (video_url_base, i)
176 if self._is_valid_url(http_url, video_id):
177 f = m3u8_format.copy()
178 f.update({
179 'url': http_url,
180 'format_id': f['format_id'].replace('hls', 'http'),
181 'protocol': 'http',
182 })
183 formats.append(f)
184 else:
fb4b030a
PH
185 formats = [{
186 'format_id': 'direct',
29f400b9 187 'url': video_url,
6e3c2047 188 'ext': determine_ext(video_url, 'mp4'),
fb4b030a 189 }]
fb4b030a
PH
190
191 self._sort_formats(formats)
192
193 return {
194 'id': video_id,
195 'title': title,
030aa5d9 196 'thumbnail': thumbnail,
fb4b030a
PH
197 'formats': formats,
198 }
6ce2c678
TF
199
200
388cfbd3 201class TVPWebsiteIE(InfoExtractor):
6f8cb242 202 IE_NAME = 'tvp:series'
388cfbd3 203 _VALID_URL = r'https?://vod\.tvp\.pl/website/(?P<display_id>[^,]+),(?P<id>\d+)'
6ce2c678 204
fb4b030a 205 _TESTS = [{
388cfbd3 206 # series
d9308378 207 'url': 'https://vod.tvp.pl/website/lzy-cennet,38678312/video',
fb4b030a 208 'info_dict': {
d9308378 209 'id': '38678312',
fb4b030a 210 },
d9308378 211 'playlist_count': 115,
388cfbd3
S
212 }, {
213 # film
214 'url': 'https://vod.tvp.pl/website/gloria,35139666',
215 'info_dict': {
216 'id': '36637049',
217 'ext': 'mp4',
218 'title': 'Gloria, Gloria',
219 },
220 'params': {
221 'skip_download': True,
222 },
223 'add_ie': ['TVPEmbed'],
224 }, {
225 'url': 'https://vod.tvp.pl/website/lzy-cennet,38678312',
226 'only_matching': True,
fb4b030a 227 }]
6ce2c678 228
388cfbd3
S
229 def _entries(self, display_id, playlist_id):
230 url = 'https://vod.tvp.pl/website/%s,%s/video' % (display_id, playlist_id)
d9308378
S
231 for page_num in itertools.count(1):
232 page = self._download_webpage(
233 url, display_id, 'Downloading page %d' % page_num,
234 query={'page': page_num})
fb4b030a 235
d9308378
S
236 video_ids = orderedSet(re.findall(
237 r'<a[^>]+\bhref=["\']/video/%s,[^,]+,(\d+)' % display_id,
238 page))
239
240 if not video_ids:
241 break
242
243 for video_id in video_ids:
244 yield self.url_result(
245 'tvp:%s' % video_id, ie=TVPEmbedIE.ie_key(),
246 video_id=video_id)
247
248 def _real_extract(self, url):
249 mobj = re.match(self._VALID_URL, url)
250 display_id, playlist_id = mobj.group('display_id', 'id')
388cfbd3
S
251 return self.playlist_result(
252 self._entries(display_id, playlist_id), playlist_id)