[azmedien:showplaylist] Add support for all episodes playlists
[youtube-dl] / youtube_dl / extractor / azmedien.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from .kaltura import KalturaIE
7 from ..utils import (
8     get_element_by_class,
9     get_element_by_id,
10     strip_or_none,
11     urljoin,
12 )
13
14
15 class AZMedienBaseIE(InfoExtractor):
16     def _kaltura_video(self, partner_id, entry_id):
17         return self.url_result(
18             'kaltura:%s:%s' % (partner_id, entry_id), ie=KalturaIE.ie_key(),
19             video_id=entry_id)
20
21
22 class AZMedienIE(AZMedienBaseIE):
23     IE_DESC = 'AZ Medien videos'
24     _VALID_URL = r'''(?x)
25                     https?://
26                         (?:www\.)?
27                         (?:
28                             telezueri\.ch|
29                             telebaern\.tv|
30                             telem1\.ch
31                         )/
32                         [0-9]+-show-[^/\#]+
33                         (?:
34                             /[0-9]+-episode-[^/\#]+
35                             (?:
36                                 /[0-9]+-segment-(?:[^/\#]+\#)?|
37                                 \#
38                             )|
39                             \#
40                         )
41                         (?P<id>[^\#]+)
42                     '''
43
44     _TESTS = [{
45         # URL with 'segment'
46         'url': 'http://www.telezueri.ch/62-show-zuerinews/13772-episode-sonntag-18-dezember-2016/32419-segment-massenabweisungen-beim-hiltl-club-wegen-pelzboom',
47         'info_dict': {
48             'id': '1_2444peh4',
49             'ext': 'mov',
50             'title': 'Massenabweisungen beim Hiltl Club wegen Pelzboom',
51             'description': 'md5:9ea9dd1b159ad65b36ddcf7f0d7c76a8',
52             'uploader_id': 'TeleZ?ri',
53             'upload_date': '20161218',
54             'timestamp': 1482084490,
55         },
56         'params': {
57             'skip_download': True,
58         },
59     }, {
60         # URL with 'segment' and fragment:
61         'url': 'http://www.telebaern.tv/118-show-news/14240-episode-dienstag-17-januar-2017/33666-segment-achtung-gefahr#zu-wenig-pflegerinnen-und-pfleger',
62         'only_matching': True
63     }, {
64         # URL with 'episode' and fragment:
65         'url': 'http://www.telem1.ch/47-show-sonntalk/13986-episode-soldaten-fuer-grenzschutz-energiestrategie-obama-bilanz#soldaten-fuer-grenzschutz-energiestrategie-obama-bilanz',
66         'only_matching': True
67     }, {
68         # URL with 'show' and fragment:
69         'url': 'http://www.telezueri.ch/66-show-sonntalk#burka-plakate-trump-putin-china-besuch',
70         'only_matching': True
71     }]
72
73     def _real_extract(self, url):
74         video_id = self._match_id(url)
75
76         webpage = self._download_webpage(url, video_id)
77
78         partner_id = self._search_regex(
79             r'<script[^>]+src=["\'](?:https?:)?//(?:[^/]+\.)?kaltura\.com(?:/[^/]+)*/(?:p|partner_id)/([0-9]+)',
80             webpage, 'kaltura partner id')
81         entry_id = self._html_search_regex(
82             r'<a[^>]+data-id=(["\'])(?P<id>(?:(?!\1).)+)\1[^>]+data-slug=["\']%s'
83             % re.escape(video_id), webpage, 'kaltura entry id', group='id')
84
85         return self._kaltura_video(partner_id, entry_id)
86
87
88 class AZMedienPlaylistIE(AZMedienBaseIE):
89     IE_DESC = 'AZ Medien playlists'
90     _VALID_URL = r'''(?x)
91                     https?://
92                         (?:www\.)?
93                         (?:
94                             telezueri\.ch|
95                             telebaern\.tv|
96                             telem1\.ch
97                         )/
98                         (?P<id>[0-9]+-
99                             (?:
100                                 show|
101                                 topic|
102                                 themen
103                             )-[^/\#]+
104                             (?:
105                                 /[0-9]+-episode-[^/\#]+
106                             )?
107                         )$
108                     '''
109
110     _TESTS = [{
111         # URL with 'episode'
112         'url': 'http://www.telebaern.tv/118-show-news/13735-episode-donnerstag-15-dezember-2016',
113         'info_dict': {
114             'id': '118-show-news/13735-episode-donnerstag-15-dezember-2016',
115             'title': 'News - Donnerstag, 15. Dezember 2016',
116         },
117         'playlist_count': 9,
118     }, {
119         # URL with 'themen'
120         'url': 'http://www.telem1.ch/258-themen-tele-m1-classics',
121         'info_dict': {
122             'id': '258-themen-tele-m1-classics',
123             'title': 'Tele M1 Classics',
124         },
125         'playlist_mincount': 15,
126     }, {
127         # URL with 'topic', contains nested playlists
128         'url': 'http://www.telezueri.ch/219-topic-aera-trump-hat-offiziell-begonnen',
129         'only_matching': True,
130     }, {
131         # URL with 'show' only
132         'url': 'http://www.telezueri.ch/86-show-talktaeglich',
133         'only_matching': True
134     }]
135
136     def _real_extract(self, url):
137         show_id = self._match_id(url)
138         webpage = self._download_webpage(url, show_id)
139
140         entries = []
141
142         partner_id = self._search_regex(
143             r'src=["\'](?:https?:)?//(?:[^/]+\.)kaltura\.com/(?:[^/]+/)*(?:p|partner_id)/(\d+)',
144             webpage, 'kaltura partner id', default=None)
145
146         if partner_id:
147             entries = [
148                 self._kaltura_video(partner_id, m.group('id'))
149                 for m in re.finditer(
150                     r'data-id=(["\'])(?P<id>(?:(?!\1).)+)\1', webpage)]
151
152         if not entries:
153             entries = [
154                 self.url_result(m.group('url'), ie=AZMedienIE.ie_key())
155                 for m in re.finditer(
156                     r'<a[^>]+data-real=(["\'])(?P<url>http.+?)\1', webpage)]
157
158         if not entries:
159             entries = [
160                 # May contain nested playlists (e.g. [1]) thus no explicit
161                 # ie_key
162                 # 1. http://www.telezueri.ch/219-topic-aera-trump-hat-offiziell-begonnen)
163                 self.url_result(urljoin(url, m.group('url')))
164                 for m in re.finditer(
165                     r'<a[^>]+name=[^>]+href=(["\'])(?P<url>/.+?)\1', webpage)]
166
167         title = self._search_regex(
168             r'episodeShareTitle\s*=\s*(["\'])(?P<title>(?:(?!\1).)+)\1',
169             webpage, 'title',
170             default=strip_or_none(get_element_by_id(
171                 'video-title', webpage)), group='title')
172
173         return self.playlist_result(entries, show_id, title)
174
175
176 class AZMedienShowPlaylistIE(AZMedienBaseIE):
177     IE_DESC = 'AZ Medien Show playlists'
178     _VALID_URL = r'''(?x)
179                     https?://
180                         (?:www\.)?
181                         (?P<id>
182                         (?:
183                             telezueri\.ch|
184                             telebaern\.tv|
185                             telem1\.ch
186                         )/
187                         (?:
188                             all-episodes|
189                             alle-episoden
190                         )
191                         /[^/]+
192                         )
193                     '''
194
195     _TEST = {
196         'url': 'http://www.telezueri.ch/all-episodes/astrotalk',
197         'info_dict': {
198             'id': 'telezueri.ch/all-episodes/astrotalk',
199             'title': 'TeleZüri: AstroTalk - alle episoden',
200             'description': 'md5:4c0f7e7d741d906004266e295ceb4a26',
201         },
202         'playlist_mincount': 13,
203         'params': {
204             'skip_download': True,
205         }
206     }
207
208     def _real_extract(self, url):
209         playlist_id = self._match_id(url)
210         webpage = self._download_webpage(url, playlist_id)
211         episodes = get_element_by_class('search-mobile-box', webpage)
212         entries = [self.url_result(
213             urljoin(url, m.group('url'))) for m in re.finditer(
214                 r'<a[^>]+href=(["\'])(?P<url>.+?)\1', episodes)]
215         title = self._og_search_title(webpage)
216         description = self._og_search_description(webpage)
217         return self.playlist_result(
218             entries,
219             playlist_id=playlist_id,
220             playlist_title=title,
221             playlist_description=description)