]>
Commit | Line | Data |
---|---|---|
1 | from .common import InfoExtractor | |
2 | from ..networking.exceptions import HTTPError | |
3 | from ..utils import ( | |
4 | ExtractorError, | |
5 | extract_attributes, | |
6 | try_get, | |
7 | urlencode_postdata, | |
8 | ) | |
9 | ||
10 | ||
11 | class TVPlayerIE(InfoExtractor): | |
12 | _VALID_URL = r'https?://(?:www\.)?tvplayer\.com/watch/(?P<id>[^/?#]+)' | |
13 | _TEST = { | |
14 | 'url': 'http://tvplayer.com/watch/bbcone', | |
15 | 'info_dict': { | |
16 | 'id': '89', | |
17 | 'ext': 'mp4', | |
18 | 'title': r're:^BBC One [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', | |
19 | }, | |
20 | 'params': { | |
21 | # m3u8 download | |
22 | 'skip_download': True, | |
23 | }, | |
24 | } | |
25 | ||
26 | def _real_extract(self, url): | |
27 | display_id = self._match_id(url) | |
28 | webpage = self._download_webpage(url, display_id) | |
29 | ||
30 | current_channel = extract_attributes(self._search_regex( | |
31 | r'(<div[^>]+class="[^"]*current-channel[^"]*"[^>]*>)', | |
32 | webpage, 'channel element')) | |
33 | title = current_channel['data-name'] | |
34 | ||
35 | resource_id = current_channel['data-id'] | |
36 | ||
37 | token = self._search_regex( | |
38 | r'data-token=(["\'])(?P<token>(?!\1).+)\1', webpage, | |
39 | 'token', group='token') | |
40 | ||
41 | context = self._download_json( | |
42 | 'https://tvplayer.com/watch/context', display_id, | |
43 | 'Downloading JSON context', query={ | |
44 | 'resource': resource_id, | |
45 | 'gen': token, | |
46 | }) | |
47 | ||
48 | validate = context['validate'] | |
49 | platform = try_get( | |
50 | context, lambda x: x['platform']['key'], str) or 'firefox' | |
51 | ||
52 | try: | |
53 | response = self._download_json( | |
54 | 'http://api.tvplayer.com/api/v2/stream/live', | |
55 | display_id, 'Downloading JSON stream', headers={ | |
56 | 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | |
57 | }, data=urlencode_postdata({ | |
58 | 'id': resource_id, | |
59 | 'service': 1, | |
60 | 'platform': platform, | |
61 | 'validate': validate, | |
62 | }))['tvplayer']['response'] | |
63 | except ExtractorError as e: | |
64 | if isinstance(e.cause, HTTPError): | |
65 | response = self._parse_json( | |
66 | e.cause.response.read().decode(), resource_id)['tvplayer']['response'] | |
67 | raise ExtractorError( | |
68 | '{} said: {}'.format(self.IE_NAME, response['error']), expected=True) | |
69 | raise | |
70 | ||
71 | formats = self._extract_m3u8_formats(response['stream'], display_id, 'mp4') | |
72 | ||
73 | return { | |
74 | 'id': resource_id, | |
75 | 'display_id': display_id, | |
76 | 'title': title, | |
77 | 'formats': formats, | |
78 | 'is_live': True, | |
79 | } |