Remove _sort_formats from _extract_*_formats methods
[youtube-dl] / youtube_dl / extractor / laola1tv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_urllib_parse_urlencode,
9     compat_urlparse,
10 )
11 from ..utils import (
12     ExtractorError,
13     sanitized_Request,
14     unified_strdate,
15     urlencode_postdata,
16     xpath_element,
17     xpath_text,
18 )
19
20
21 class Laola1TvIE(InfoExtractor):
22     _VALID_URL = r'https?://(?:www\.)?laola1\.tv/(?P<lang>[a-z]+)-(?P<portal>[a-z]+)/(?P<kind>[^/]+)/(?P<slug>[^/?#&]+)'
23     _TESTS = [{
24         'url': 'http://www.laola1.tv/de-de/video/straubing-tigers-koelner-haie/227883.html',
25         'info_dict': {
26             'id': '227883',
27             'display_id': 'straubing-tigers-koelner-haie',
28             'ext': 'flv',
29             'title': 'Straubing Tigers - Kölner Haie',
30             'upload_date': '20140912',
31             'is_live': False,
32             'categories': ['Eishockey'],
33         },
34         'params': {
35             'skip_download': True,
36         },
37     }, {
38         'url': 'http://www.laola1.tv/de-de/video/straubing-tigers-koelner-haie',
39         'info_dict': {
40             'id': '464602',
41             'display_id': 'straubing-tigers-koelner-haie',
42             'ext': 'flv',
43             'title': 'Straubing Tigers - Kölner Haie',
44             'upload_date': '20160129',
45             'is_live': False,
46             'categories': ['Eishockey'],
47         },
48         'params': {
49             'skip_download': True,
50         },
51     }, {
52         'url': 'http://www.laola1.tv/de-de/livestream/2016-03-22-belogorie-belgorod-trentino-diatec-lde',
53         'info_dict': {
54             'id': '487850',
55             'display_id': '2016-03-22-belogorie-belgorod-trentino-diatec-lde',
56             'ext': 'flv',
57             'title': 'Belogorie BELGOROD - TRENTINO Diatec',
58             'upload_date': '20160322',
59             'uploader': 'CEV - Europäischer Volleyball Verband',
60             'is_live': True,
61             'categories': ['Volleyball'],
62         },
63         'params': {
64             'skip_download': True,
65         },
66     }]
67
68     def _real_extract(self, url):
69         mobj = re.match(self._VALID_URL, url)
70         display_id = mobj.group('slug')
71         kind = mobj.group('kind')
72         lang = mobj.group('lang')
73         portal = mobj.group('portal')
74
75         webpage = self._download_webpage(url, display_id)
76
77         iframe_url = self._search_regex(
78             r'<iframe[^>]*?id="videoplayer"[^>]*?src="([^"]+)"',
79             webpage, 'iframe url')
80
81         video_id = self._search_regex(
82             r'videoid=(\d+)', iframe_url, 'video id')
83
84         iframe = self._download_webpage(compat_urlparse.urljoin(
85             url, iframe_url), display_id, 'Downloading iframe')
86
87         partner_id = self._search_regex(
88             r'partnerid\s*:\s*(["\'])(?P<partner_id>.+?)\1',
89             iframe, 'partner id', group='partner_id')
90
91         hd_doc = self._download_xml(
92             'http://www.laola1.tv/server/hd_video.php?%s'
93             % compat_urllib_parse_urlencode({
94                 'play': video_id,
95                 'partner': partner_id,
96                 'portal': portal,
97                 'lang': lang,
98                 'v5ident': '',
99             }), display_id)
100
101         _v = lambda x, **k: xpath_text(hd_doc, './/video/' + x, **k)
102         title = _v('title', fatal=True)
103
104         VS_TARGETS = {
105             'video': '2',
106             'livestream': '17',
107         }
108
109         req = sanitized_Request(
110             'https://club.laola1.tv/sp/laola1/api/v3/user/session/premium/player/stream-access?%s' %
111             compat_urllib_parse_urlencode({
112                 'videoId': video_id,
113                 'target': VS_TARGETS.get(kind, '2'),
114                 'label': _v('label'),
115                 'area': _v('area'),
116             }),
117             urlencode_postdata(
118                 dict((i, v) for i, v in enumerate(_v('req_liga_abos').split(',')))))
119
120         token_url = self._download_json(req, display_id)['data']['stream-access'][0]
121         token_doc = self._download_xml(token_url, display_id, 'Downloading token')
122
123         token_attrib = xpath_element(token_doc, './/token').attrib
124         token_auth = token_attrib['auth']
125
126         if token_auth in ('blocked', 'restricted', 'error'):
127             raise ExtractorError(
128                 'Token error: %s' % token_attrib['comment'], expected=True)
129
130         formats = self._extract_f4m_formats(
131             '%s?hdnea=%s&hdcore=3.2.0' % (token_attrib['url'], token_auth),
132             video_id, f4m_id='hds')
133         self._sort_formats(formats)
134
135         categories_str = _v('meta_sports')
136         categories = categories_str.split(',') if categories_str else []
137
138         return {
139             'id': video_id,
140             'display_id': display_id,
141             'title': title,
142             'upload_date': unified_strdate(_v('time_date')),
143             'uploader': _v('meta_organisation'),
144             'categories': categories,
145             'is_live': _v('islive') == 'true',
146             'formats': formats,
147         }