[laola1tv:embed] Fix tests
[youtube-dl] / youtube_dl / extractor / laola1tv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     unified_strdate,
10     urlencode_postdata,
11     xpath_element,
12     xpath_text,
13     urljoin,
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 Laola1TvIE(Laola1TvEmbedIE):
124     IE_NAME = 'laola1tv'
125     _VALID_URL = r'https?://(?:www\.)?laola1\.tv/[a-z]+-[a-z]+/[^/]+/(?P<id>[^/?#&]+)'
126     _TESTS = [{
127         'url': 'http://www.laola1.tv/de-de/video/straubing-tigers-koelner-haie/227883.html',
128         'info_dict': {
129             'id': '227883',
130             'display_id': 'straubing-tigers-koelner-haie',
131             'ext': 'flv',
132             'title': 'Straubing Tigers - Kölner Haie',
133             'upload_date': '20140912',
134             'is_live': False,
135             'categories': ['Eishockey'],
136         },
137         'params': {
138             'skip_download': True,
139         },
140     }, {
141         'url': 'http://www.laola1.tv/de-de/video/straubing-tigers-koelner-haie',
142         'info_dict': {
143             'id': '464602',
144             'display_id': 'straubing-tigers-koelner-haie',
145             'ext': 'flv',
146             'title': 'Straubing Tigers - Kölner Haie',
147             'upload_date': '20160129',
148             'is_live': False,
149             'categories': ['Eishockey'],
150         },
151         'params': {
152             'skip_download': True,
153         },
154     }, {
155         'url': 'http://www.laola1.tv/de-de/livestream/2016-03-22-belogorie-belgorod-trentino-diatec-lde',
156         'info_dict': {
157             'id': '487850',
158             'display_id': '2016-03-22-belogorie-belgorod-trentino-diatec-lde',
159             'ext': 'flv',
160             'title': 'Belogorie BELGOROD - TRENTINO Diatec',
161             'upload_date': '20160322',
162             'uploader': 'CEV - Europäischer Volleyball Verband',
163             'is_live': True,
164             'categories': ['Volleyball'],
165         },
166         'params': {
167             'skip_download': True,
168         },
169         'skip': 'This live stream has already finished.',
170     }]
171
172     def _real_extract(self, url):
173         display_id = self._match_id(url)
174
175         webpage = self._download_webpage(url, display_id)
176
177         if 'Dieser Livestream ist bereits beendet.' in webpage:
178             raise ExtractorError('This live stream has already finished.', expected=True)
179
180         conf = self._parse_json(self._search_regex(
181             r'(?s)conf\s*=\s*({.+?});', webpage, 'conf'),
182             display_id, js_to_json)
183
184         video_id = conf['videoid']
185
186         config = self._download_json(conf['configUrl'], video_id, query={
187             'videoid': video_id,
188             'partnerid': conf['partnerid'],
189             'language': conf.get('language', ''),
190             'portal': conf.get('portalid', ''),
191         })
192         error = config.get('error')
193         if error:
194             raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True)
195
196         video_data = config['video']
197         title = video_data['title']
198         is_live = video_data.get('isLivestream') and video_data.get('isLive')
199         meta = video_data.get('metaInformation')
200         sports = meta.get('sports')
201         categories = sports.split(',') if sports else []
202
203         token_url = self._extract_token_url(
204             video_data['streamAccess'], video_id,
205             video_data['abo']['required'])
206
207         formats = self._extract_formats(token_url, video_id)
208
209         return {
210             'id': video_id,
211             'display_id': display_id,
212             'title': self._live_title(title) if is_live else title,
213             'description': video_data.get('description'),
214             'thumbnail': video_data.get('image'),
215             'categories': categories,
216             'formats': formats,
217             'is_live': is_live,
218         }