[ehftv] Add extractor (closes #15408)
[youtube-dl] / youtube_dl / extractor / laola1tv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import (
9     ExtractorError,
10     unified_strdate,
11     urlencode_postdata,
12     xpath_element,
13     xpath_text,
14     update_url_query,
15     js_to_json,
16 )
17
18
19 class Laola1TvEmbedIE(InfoExtractor):
20     IE_NAME = 'laola1tv:embed'
21     _VALID_URL = r'https?://(?:www\.)?laola1\.tv/titanplayer\.php\?.*?\bvideoid=(?P<id>\d+)'
22     _TESTS = [{
23         # flashvars.premium = "false";
24         'url': 'https://www.laola1.tv/titanplayer.php?videoid=708065&type=V&lang=en&portal=int&customer=1024',
25         'info_dict': {
26             'id': '708065',
27             'ext': 'mp4',
28             'title': 'MA Long CHN - FAN Zhendong CHN',
29             'uploader': 'ITTF - International Table Tennis Federation',
30             'upload_date': '20161211',
31         },
32     }]
33
34     def _extract_token_url(self, stream_access_url, video_id, data):
35         return self._download_json(
36             stream_access_url, video_id, headers={
37                 'Content-Type': 'application/json',
38             }, data=json.dumps(data).encode())['data']['stream-access'][0]
39
40     def _extract_formats(self, token_url, video_id):
41         token_doc = self._download_xml(
42             token_url, video_id, 'Downloading token',
43             headers=self.geo_verification_headers())
44
45         token_attrib = xpath_element(token_doc, './/token').attrib
46
47         if token_attrib['status'] != '0':
48             raise ExtractorError(
49                 'Token error: %s' % token_attrib['comment'], expected=True)
50
51         formats = self._extract_akamai_formats(
52             '%s?hdnea=%s' % (token_attrib['url'], token_attrib['auth']),
53             video_id)
54         self._sort_formats(formats)
55         return formats
56
57     def _real_extract(self, url):
58         video_id = self._match_id(url)
59         webpage = self._download_webpage(url, video_id)
60         flash_vars = self._search_regex(
61             r'(?s)flashvars\s*=\s*({.+?});', webpage, 'flash vars')
62
63         def get_flashvar(x, *args, **kwargs):
64             flash_var = self._search_regex(
65                 r'%s\s*:\s*"([^"]+)"' % x,
66                 flash_vars, x, default=None)
67             if not flash_var:
68                 flash_var = self._search_regex([
69                     r'flashvars\.%s\s*=\s*"([^"]+)"' % x,
70                     r'%s\s*=\s*"([^"]+)"' % x],
71                     webpage, x, *args, **kwargs)
72             return flash_var
73
74         hd_doc = self._download_xml(
75             'http://www.laola1.tv/server/hd_video.php', video_id, query={
76                 'play': get_flashvar('streamid'),
77                 'partner': get_flashvar('partnerid'),
78                 'portal': get_flashvar('portalid'),
79                 'lang': get_flashvar('sprache'),
80                 'v5ident': '',
81             })
82
83         _v = lambda x, **k: xpath_text(hd_doc, './/video/' + x, **k)
84         title = _v('title', fatal=True)
85
86         token_url = None
87         premium = get_flashvar('premium', default=None)
88         if premium:
89             token_url = update_url_query(
90                 _v('url', fatal=True), {
91                     'timestamp': get_flashvar('timestamp'),
92                     'auth': get_flashvar('auth'),
93                 })
94         else:
95             data_abo = urlencode_postdata(
96                 dict((i, v) for i, v in enumerate(_v('req_liga_abos').split(','))))
97             stream_access_url = update_url_query(
98                 'https://club.laola1.tv/sp/laola1/api/v3/user/session/premium/player/stream-access', {
99                     'videoId': _v('id'),
100                     'target': self._search_regex(r'vs_target = (\d+);', webpage, 'vs target'),
101                     'label': _v('label'),
102                     'area': _v('area'),
103                 })
104             token_url = self._extract_token_url(stream_access_url, video_id, data_abo)
105
106         formats = self._extract_formats(token_url, video_id)
107
108         categories_str = _v('meta_sports')
109         categories = categories_str.split(',') if categories_str else []
110         is_live = _v('islive') == 'true'
111
112         return {
113             'id': video_id,
114             'title': self._live_title(title) if is_live else title,
115             'upload_date': unified_strdate(_v('time_date')),
116             'uploader': _v('meta_organisation'),
117             'categories': categories,
118             'is_live': is_live,
119             'formats': formats,
120         }
121
122
123 class Laola1TvBaseIE(Laola1TvEmbedIE):
124     def _extract_video(self, url):
125         display_id = self._match_id(url)
126         webpage = self._download_webpage(url, display_id)
127
128         if 'Dieser Livestream ist bereits beendet.' in webpage:
129             raise ExtractorError('This live stream has already finished.', expected=True)
130
131         conf = self._parse_json(self._search_regex(
132             r'(?s)conf\s*=\s*({.+?});', webpage, 'conf'),
133             display_id,
134             transform_source=lambda s: js_to_json(re.sub(r'shareurl:.+,', '', s)))
135         video_id = conf['videoid']
136
137         config = self._download_json(conf['configUrl'], video_id, query={
138             'videoid': video_id,
139             'partnerid': conf['partnerid'],
140             'language': conf.get('language', ''),
141             'portal': conf.get('portalid', ''),
142         })
143         error = config.get('error')
144         if error:
145             raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True)
146
147         video_data = config['video']
148         title = video_data['title']
149         is_live = video_data.get('isLivestream') and video_data.get('isLive')
150         meta = video_data.get('metaInformation')
151         sports = meta.get('sports')
152         categories = sports.split(',') if sports else []
153
154         token_url = self._extract_token_url(
155             video_data['streamAccess'], video_id,
156             video_data['abo']['required'])
157
158         formats = self._extract_formats(token_url, video_id)
159
160         return {
161             'id': video_id,
162             'display_id': display_id,
163             'title': self._live_title(title) if is_live else title,
164             'description': video_data.get('description'),
165             'thumbnail': video_data.get('image'),
166             'categories': categories,
167             'formats': formats,
168             'is_live': is_live,
169         }
170
171
172 class Laola1TvIE(Laola1TvBaseIE):
173     IE_NAME = 'laola1tv'
174     _VALID_URL = r'https?://(?:www\.)?laola1\.tv/[a-z]+-[a-z]+/[^/]+/(?P<id>[^/?#&]+)'
175
176     _TESTS = [{
177         'url': 'http://www.laola1.tv/de-de/video/straubing-tigers-koelner-haie/227883.html',
178         'info_dict': {
179             'id': '227883',
180             'display_id': 'straubing-tigers-koelner-haie',
181             'ext': 'flv',
182             'title': 'Straubing Tigers - Kölner Haie',
183             'upload_date': '20140912',
184             'is_live': False,
185             'categories': ['Eishockey'],
186         },
187         'params': {
188             'skip_download': True,
189         },
190     }, {
191         'url': 'http://www.laola1.tv/de-de/video/straubing-tigers-koelner-haie',
192         'info_dict': {
193             'id': '464602',
194             'display_id': 'straubing-tigers-koelner-haie',
195             'ext': 'flv',
196             'title': 'Straubing Tigers - Kölner Haie',
197             'upload_date': '20160129',
198             'is_live': False,
199             'categories': ['Eishockey'],
200         },
201         'params': {
202             'skip_download': True,
203         },
204     }, {
205         'url': 'http://www.laola1.tv/de-de/livestream/2016-03-22-belogorie-belgorod-trentino-diatec-lde',
206         'info_dict': {
207             'id': '487850',
208             'display_id': '2016-03-22-belogorie-belgorod-trentino-diatec-lde',
209             'ext': 'flv',
210             'title': 'Belogorie BELGOROD - TRENTINO Diatec',
211             'upload_date': '20160322',
212             'uploader': 'CEV - Europäischer Volleyball Verband',
213             'is_live': True,
214             'categories': ['Volleyball'],
215         },
216         'params': {
217             'skip_download': True,
218         },
219         'skip': 'This live stream has already finished.',
220     }]
221
222     def _real_extract(self, url):
223         return self._extract_video(url)
224
225
226 class EHFTVIE(Laola1TvBaseIE):
227     IE_NAME = 'ehftv'
228     _VALID_URL = r'https?://(?:www\.)?ehftv\.com/[a-z]+(-[a-z]+)?/[^/]+/(?P<id>[^/?#&]+)'
229
230     _TESTS = [{
231         'url': 'https://www.ehftv.com/int/video/paris-saint-germain-handball-pge-vive-kielce/1166761',
232         'info_dict': {
233             'id': '1166761',
234             'display_id': 'paris-saint-germain-handball-pge-vive-kielce',
235             'ext': 'mp4',
236             'title': 'Paris Saint-Germain Handball - PGE Vive Kielce',
237             'is_live': False,
238             'categories': ['Handball'],
239         },
240         'params': {
241             'skip_download': True,
242         },
243     }]
244
245     def _real_extract(self, url):
246         return self._extract_video(url)
247
248
249 class ITTFIE(InfoExtractor):
250     _VALID_URL = r'https?://tv\.ittf\.com/video/[^/]+/(?P<id>\d+)'
251     _TEST = {
252         'url': 'https://tv.ittf.com/video/peng-wang-wei-matsudaira-kenta/951802',
253         'only_matching': True,
254     }
255
256     def _real_extract(self, url):
257         return self.url_result(
258             update_url_query('https://www.laola1.tv/titanplayer.php', {
259                 'videoid': self._match_id(url),
260                 'type': 'V',
261                 'lang': 'en',
262                 'portal': 'int',
263                 'customer': 1024,
264             }), Laola1TvEmbedIE.ie_key())