Merge branch 'fstirlitz-filmon'
[youtube-dl] / youtube_dl / extractor / laola1tv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     unified_strdate,
8     urlencode_postdata,
9     xpath_element,
10     xpath_text,
11     urljoin,
12     update_url_query,
13 )
14
15
16 class Laola1TvEmbedIE(InfoExtractor):
17     IE_NAME = 'laola1tv:embed'
18     _VALID_URL = r'https?://(?:www\.)?laola1\.tv/titanplayer\.php\?.*?\bvideoid=(?P<id>\d+)'
19     _TEST = {
20         # flashvars.premium = "false";
21         'url': 'https://www.laola1.tv/titanplayer.php?videoid=708065&type=V&lang=en&portal=int&customer=1024',
22         'info_dict': {
23             'id': '708065',
24             'ext': 'mp4',
25             'title': 'MA Long CHN - FAN Zhendong CHN',
26             'uploader': 'ITTF - International Table Tennis Federation',
27             'upload_date': '20161211',
28         },
29     }
30
31     def _real_extract(self, url):
32         video_id = self._match_id(url)
33         webpage = self._download_webpage(url, video_id)
34         flash_vars = self._search_regex(
35             r'(?s)flashvars\s*=\s*({.+?});', webpage, 'flash vars')
36
37         def get_flashvar(x, *args, **kwargs):
38             flash_var = self._search_regex(
39                 r'%s\s*:\s*"([^"]+)"' % x,
40                 flash_vars, x, default=None)
41             if not flash_var:
42                 flash_var = self._search_regex([
43                     r'flashvars\.%s\s*=\s*"([^"]+)"' % x,
44                     r'%s\s*=\s*"([^"]+)"' % x],
45                     webpage, x, *args, **kwargs)
46             return flash_var
47
48         hd_doc = self._download_xml(
49             'http://www.laola1.tv/server/hd_video.php', video_id, query={
50                 'play': get_flashvar('streamid'),
51                 'partner': get_flashvar('partnerid'),
52                 'portal': get_flashvar('portalid'),
53                 'lang': get_flashvar('sprache'),
54                 'v5ident': '',
55             })
56
57         _v = lambda x, **k: xpath_text(hd_doc, './/video/' + x, **k)
58         title = _v('title', fatal=True)
59
60         token_url = None
61         premium = get_flashvar('premium', default=None)
62         if premium:
63             token_url = update_url_query(
64                 _v('url', fatal=True), {
65                     'timestamp': get_flashvar('timestamp'),
66                     'auth': get_flashvar('auth'),
67                 })
68         else:
69             data_abo = urlencode_postdata(
70                 dict((i, v) for i, v in enumerate(_v('req_liga_abos').split(','))))
71             token_url = self._download_json(
72                 'https://club.laola1.tv/sp/laola1/api/v3/user/session/premium/player/stream-access',
73                 video_id, query={
74                     'videoId': _v('id'),
75                     'target': self._search_regex(r'vs_target = (\d+);', webpage, 'vs target'),
76                     'label': _v('label'),
77                     'area': _v('area'),
78                 }, data=data_abo)['data']['stream-access'][0]
79
80         token_doc = self._download_xml(
81             token_url, video_id, 'Downloading token',
82             headers=self.geo_verification_headers())
83
84         token_attrib = xpath_element(token_doc, './/token').attrib
85
86         if token_attrib['status'] != '0':
87             raise ExtractorError(
88                 'Token error: %s' % token_attrib['comment'], expected=True)
89
90         formats = self._extract_akamai_formats(
91             '%s?hdnea=%s' % (token_attrib['url'], token_attrib['auth']),
92             video_id)
93         self._sort_formats(formats)
94
95         categories_str = _v('meta_sports')
96         categories = categories_str.split(',') if categories_str else []
97         is_live = _v('islive') == 'true'
98
99         return {
100             'id': video_id,
101             'title': self._live_title(title) if is_live else title,
102             'upload_date': unified_strdate(_v('time_date')),
103             'uploader': _v('meta_organisation'),
104             'categories': categories,
105             'is_live': is_live,
106             'formats': formats,
107         }
108
109
110 class Laola1TvIE(InfoExtractor):
111     IE_NAME = 'laola1tv'
112     _VALID_URL = r'https?://(?:www\.)?laola1\.tv/[a-z]+-[a-z]+/[^/]+/(?P<id>[^/?#&]+)'
113     _TESTS = [{
114         'url': 'http://www.laola1.tv/de-de/video/straubing-tigers-koelner-haie/227883.html',
115         'info_dict': {
116             'id': '227883',
117             'display_id': 'straubing-tigers-koelner-haie',
118             'ext': 'flv',
119             'title': 'Straubing Tigers - Kölner Haie',
120             'upload_date': '20140912',
121             'is_live': False,
122             'categories': ['Eishockey'],
123         },
124         'params': {
125             'skip_download': True,
126         },
127     }, {
128         'url': 'http://www.laola1.tv/de-de/video/straubing-tigers-koelner-haie',
129         'info_dict': {
130             'id': '464602',
131             'display_id': 'straubing-tigers-koelner-haie',
132             'ext': 'flv',
133             'title': 'Straubing Tigers - Kölner Haie',
134             'upload_date': '20160129',
135             'is_live': False,
136             'categories': ['Eishockey'],
137         },
138         'params': {
139             'skip_download': True,
140         },
141     }, {
142         'url': 'http://www.laola1.tv/de-de/livestream/2016-03-22-belogorie-belgorod-trentino-diatec-lde',
143         'info_dict': {
144             'id': '487850',
145             'display_id': '2016-03-22-belogorie-belgorod-trentino-diatec-lde',
146             'ext': 'flv',
147             'title': 'Belogorie BELGOROD - TRENTINO Diatec',
148             'upload_date': '20160322',
149             'uploader': 'CEV - Europäischer Volleyball Verband',
150             'is_live': True,
151             'categories': ['Volleyball'],
152         },
153         'params': {
154             'skip_download': True,
155         },
156         'skip': 'This live stream has already finished.',
157     }]
158
159     def _real_extract(self, url):
160         display_id = self._match_id(url)
161
162         webpage = self._download_webpage(url, display_id)
163
164         if 'Dieser Livestream ist bereits beendet.' in webpage:
165             raise ExtractorError('This live stream has already finished.', expected=True)
166
167         iframe_url = urljoin(url, self._search_regex(
168             r'<iframe[^>]*?id="videoplayer"[^>]*?src="([^"]+)"',
169             webpage, 'iframe url'))
170
171         return {
172             '_type': 'url',
173             'display_id': display_id,
174             'url': iframe_url,
175             'ie_key': 'Laola1TvEmbed',
176         }