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