[laola1tv] Fixes for changed site layout.
[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 ..utils import (
8     compat_urlparse,
9     ExtractorError,
10     sanitized_Request,
11     xpath_element,
12     xpath_text,
13     unified_strdate,
14     urlencode_postdata,
15 )
16
17
18 class Laola1TvIE(InfoExtractor):
19     _VALID_URL = r'https?://(?:www\.)?laola1\.tv/(?P<lang>[a-z]+)-(?P<portal>[a-z]+)/.*?/(?P<slug>[\w-]+)'
20     _TESTS = [{
21         'url': 'http://www.laola1.tv/de-de/video/straubing-tigers-koelner-haie/227883.html',
22         'info_dict': {
23             'categories': ['Eishockey'],
24             'ext': 'flv',
25             'id': '227883',
26             'is_live': False,
27             'title': 'Straubing Tigers - Kölner Haie',
28             'upload_date': '20140912',
29         },
30         'params': {
31             'skip_download': True,
32         }
33     }, {
34         'url': 'http://www.laola1.tv/de-de/video/straubing-tigers-koelner-haie',
35         'info_dict': {
36             'categories': ['Eishockey'],
37             'ext': 'flv',
38             'id': '464602',
39             'is_live': False,
40             'title': 'Straubing Tigers - Kölner Haie',
41             'upload_date': '20160129',
42         },
43         'params': {
44             'skip_download': True,
45         }
46     }]
47
48     def _real_extract(self, url):
49         mobj = re.match(self._VALID_URL, url)
50         lang = mobj.group('lang')
51         portal = mobj.group('portal')
52
53         webpage = self._download_webpage(url, mobj.group('slug'))
54         iframe_url = self._search_regex(
55             r'<iframe[^>]*?id="videoplayer"[^>]*?src="([^"]+)"',
56             webpage, 'iframe URL')
57
58         video_id = self._search_regex(
59             r'videoid=(\d+)', iframe_url, 'video ID')
60
61         iframe = self._download_webpage(compat_urlparse.urljoin(
62             url, iframe_url), video_id, note='Downloading iframe')
63
64         partner_id = self._search_regex(
65             r'partnerid\s*:\s*"([^"]+)"', iframe, 'partner ID')
66
67         xml_url = ('http://www.laola1.tv/server/hd_video.php?' +
68                    'play=%s&partner=%s&portal=%s&v5ident=&lang=%s' % (
69                        video_id, partner_id, portal, lang))
70         hd_doc = self._download_xml(xml_url, video_id)
71
72         _v = lambda x, **k: xpath_text(hd_doc, './/video/' + x, **k)
73         title = _v('title', fatal=True)
74
75         categories = _v('meta_sports')
76         if categories:
77             categories = categories.split(',')
78
79         time_date = _v('time_date')
80         time_start = _v('time_start')
81         upload_date = None
82         if time_date and time_start:
83             upload_date = unified_strdate(time_date + ' ' + time_start)
84
85         json_url = ('https://club.laola1.tv/sp/laola1/api/v3/user/session' +
86                     '/premium/player/stream-access?videoId=%s&target=2' +
87                     '&label=laola1tv&area=%s') % (video_id, _v('area'))
88         req = sanitized_Request(json_url, urlencode_postdata(
89             dict((i, v) for i, v in enumerate(_v('req_liga_abos').split(',')))))
90
91         token_url = self._download_json(req, video_id)['data']['stream-access'][0]
92         token_doc = self._download_xml(
93             token_url, video_id, note='Downloading token')
94
95         token_attrib = xpath_element(token_doc, './/token').attrib
96         token_auth = token_attrib['auth']
97
98         if token_auth in ('blocked', 'restricted'):
99             raise ExtractorError(
100                 'Token error: %s' % token_attrib['comment'], expected=True)
101
102         video_url = '%s?hdnea=%s&hdcore=3.2.0' % (token_attrib['url'], token_auth)
103
104         return {
105             'categories': categories,
106             'formats': self._extract_f4m_formats(
107                 video_url, video_id, f4m_id='hds'),
108             'id': video_id,
109             'is_live': _v('islive') == 'true',
110             'title': title,
111             'upload_date': upload_date,
112             'uploader': _v('meta_organisation'),
113         }