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