[tvp] extract video id from the webpage(fixes #7799)
[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)/(?:(?!\d+/)[^/]+/)*(?P<id>\d+)'
19
20     _TESTS = [{
21         'url': 'http://vod.tvp.pl/194536/i-seria-odc-13',
22         'md5': '8aa518c15e5cc32dfe8db400dc921fbb',
23         'info_dict': {
24             'id': '194536',
25             'ext': 'mp4',
26             'title': 'Czas honoru, I seria – odc. 13',
27             'description': 'md5:76649d2014f65c99477be17f23a4dead',
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': 'http://vod.tvp.pl/22704887/08122015-1500',
41         'md5': 'cf6a4705dfd1489aef8deb168d6ba742',
42         'info_dict': {
43             'id': '22680786',
44             'ext': 'mp4',
45             'title': 'Wiadomości, 08.12.2015, 15:00',
46         },
47     }, {
48         'url': 'http://vod.tvp.pl/seriale/obyczajowe/na-sygnale/sezon-2-27-/odc-39/17834272',
49         'only_matching': True,
50     }, {
51         'url': 'http://wiadomosci.tvp.pl/25169746/24052016-1200',
52         'only_matching': True,
53     }, {
54         'url': 'http://krakow.tvp.pl/25511623/25lecie-mck-wyjatkowe-miejsce-na-mapie-krakowa',
55         'only_matching': True,
56     }, {
57         'url': 'http://teleexpress.tvp.pl/25522307/wierni-wzieli-udzial-w-procesjach',
58         'only_matching': True,
59     }, {
60         'url': 'http://sport.tvp.pl/25522165/krychowiak-uspokaja-w-sprawie-kontuzji-dwa-tygodnie-to-maksimum',
61         'only_matching': True,
62     }, {
63         'url': 'http://www.tvp.info/25511919/trwa-rewolucja-wladza-zdecydowala-sie-na-pogwalcenie-konstytucji',
64         'only_matching': True,
65     }]
66
67     def _real_extract(self, url):
68         page_id = self._match_id(url)
69         webpage = self._download_webpage(url, page_id)
70         video_id = self._search_regex([
71             r'<iframe[^>]+src="[^"]*?object_id=(\d+)',
72             "object_id\s*:\s*'(\d+)'"], webpage, 'video id')
73         return {
74             '_type': 'url_transparent',
75             'url': 'tvp:' + video_id,
76             'description': self._og_search_description(webpage, default=None),
77             'thumbnail': self._og_search_thumbnail(webpage),
78             'ie_key': 'TVPEmbed',
79         }
80
81
82 class TVPEmbedIE(InfoExtractor):
83     IE_NAME = 'tvp:embed'
84     IE_DESC = 'Telewizja Polska'
85     _VALID_URL = r'(?:tvp:|https?://[^/]+\.tvp\.(?:pl|info)/sess/tvplayer\.php\?.*?object_id=)(?P<id>\d+)'
86
87     _TESTS = [{
88         'url': 'http://www.tvp.pl/sess/tvplayer.php?object_id=22670268',
89         'md5': '8c9cd59d16edabf39331f93bf8a766c7',
90         'info_dict': {
91             'id': '22670268',
92             'ext': 'mp4',
93             'title': 'Panorama, 07.12.2015, 15:40',
94         },
95     }, {
96         'url': 'tvp:22670268',
97         'only_matching': True,
98     }]
99
100     def _real_extract(self, url):
101         video_id = self._match_id(url)
102
103         webpage = self._download_webpage(
104             'http://www.tvp.pl/sess/tvplayer.php?object_id=%s' % video_id, video_id)
105
106         error_massage = get_element_by_attribute('class', 'msg error', webpage)
107         if error_massage:
108             raise ExtractorError('%s said: %s' % (
109                 self.IE_NAME, clean_html(error_massage)), expected=True)
110
111         title = self._search_regex(
112             r'name\s*:\s*([\'"])Title\1\s*,\s*value\s*:\s*\1(?P<title>.+?)\1',
113             webpage, 'title', group='title')
114         series_title = self._search_regex(
115             r'name\s*:\s*([\'"])SeriesTitle\1\s*,\s*value\s*:\s*\1(?P<series>.+?)\1',
116             webpage, 'series', group='series', default=None)
117         if series_title:
118             title = '%s, %s' % (series_title, title)
119
120         thumbnail = self._search_regex(
121             r"poster\s*:\s*'([^']+)'", webpage, 'thumbnail', default=None)
122
123         video_url = self._search_regex(
124             r'0:{src:([\'"])(?P<url>.*?)\1', webpage,
125             'formats', group='url', default=None)
126         if not video_url or 'material_niedostepny.mp4' in video_url:
127             video_url = self._download_json(
128                 'http://www.tvp.pl/pub/stat/videofileinfo?video_id=%s' % video_id,
129                 video_id)['video_url']
130
131         formats = []
132         video_url_base = self._search_regex(
133             r'(https?://.+?/video)(?:\.(?:ism|f4m|m3u8)|-\d+\.mp4)',
134             video_url, 'video base url', default=None)
135         if video_url_base:
136             # TODO: <Group> found instead of <AdaptationSet> in MPD manifest.
137             # It's not mentioned in MPEG-DASH standard. Figure that out.
138             # formats.extend(self._extract_mpd_formats(
139             #     video_url_base + '.ism/video.mpd',
140             #     video_id, mpd_id='dash', fatal=False))
141             formats.extend(self._extract_f4m_formats(
142                 video_url_base + '.ism/video.f4m',
143                 video_id, f4m_id='hds', fatal=False))
144             m3u8_formats = self._extract_m3u8_formats(
145                 video_url_base + '.ism/video.m3u8', video_id,
146                 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
147             self._sort_formats(m3u8_formats)
148             m3u8_formats = list(filter(
149                 lambda f: f.get('vcodec') != 'none' and f.get('resolution') != 'multiple',
150                 m3u8_formats))
151             formats.extend(m3u8_formats)
152             for i, m3u8_format in enumerate(m3u8_formats, 2):
153                 http_url = '%s-%d.mp4' % (video_url_base, i)
154                 if self._is_valid_url(http_url, video_id):
155                     f = m3u8_format.copy()
156                     f.update({
157                         'url': http_url,
158                         'format_id': f['format_id'].replace('hls', 'http'),
159                         'protocol': 'http',
160                     })
161                     formats.append(f)
162         else:
163             formats = [{
164                 'format_id': 'direct',
165                 'url': video_url,
166                 'ext': determine_ext(video_url, 'mp4'),
167             }]
168
169         self._sort_formats(formats)
170
171         return {
172             'id': video_id,
173             'title': title,
174             'thumbnail': thumbnail,
175             'formats': formats,
176         }
177
178
179 class TVPSeriesIE(InfoExtractor):
180     IE_NAME = 'tvp:series'
181     _VALID_URL = r'https?://vod\.tvp\.pl/(?:[^/]+/){2}(?P<id>[^/]+)/?$'
182
183     _TESTS = [{
184         'url': 'http://vod.tvp.pl/filmy-fabularne/filmy-za-darmo/ogniem-i-mieczem',
185         'info_dict': {
186             'title': 'Ogniem i mieczem',
187             'id': '4278026',
188         },
189         'playlist_count': 4,
190     }, {
191         'url': 'http://vod.tvp.pl/audycje/podroze/boso-przez-swiat',
192         'info_dict': {
193             'title': 'Boso przez świat',
194             'id': '9329207',
195         },
196         'playlist_count': 86,
197     }]
198
199     def _real_extract(self, url):
200         display_id = self._match_id(url)
201         webpage = self._download_webpage(url, display_id, tries=5)
202
203         title = self._html_search_regex(
204             r'(?s) id=[\'"]path[\'"]>(?:.*? / ){2}(.*?)</span>', webpage, 'series')
205         playlist_id = self._search_regex(r'nodeId:\s*(\d+)', webpage, 'playlist id')
206         playlist = self._download_webpage(
207             'http://vod.tvp.pl/vod/seriesAjax?type=series&nodeId=%s&recommend'
208             'edId=0&sort=&page=0&pageSize=10000' % playlist_id, display_id, tries=5,
209             note='Downloading playlist')
210
211         videos_paths = re.findall(
212             '(?s)class="shortTitle">.*?href="(/[^"]+)', playlist)
213         entries = [
214             self.url_result('http://vod.tvp.pl%s' % v_path, ie=TVPIE.ie_key())
215             for v_path in videos_paths]
216
217         return {
218             '_type': 'playlist',
219             'id': playlist_id,
220             'display_id': display_id,
221             'title': title,
222             'entries': entries,
223         }