]>
Commit | Line | Data |
---|---|---|
4d32b638 | 1 | from .common import InfoExtractor |
3d2623a8 | 2 | from ..networking.exceptions import HTTPError |
4d32b638 | 3 | from ..utils import ( |
e897bd82 | 4 | ExtractorError, |
4d32b638 | 5 | extract_attributes, |
b364c87c | 6 | try_get, |
4d32b638 | 7 | urlencode_postdata, |
4d32b638 RA |
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, | |
add96eb9 | 23 | }, |
4d32b638 RA |
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 | ||
b364c87c S |
35 | resource_id = current_channel['data-id'] |
36 | ||
4d32b638 | 37 | token = self._search_regex( |
b364c87c S |
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, | |
e1db730d | 45 | 'gen': token, |
b364c87c S |
46 | }) |
47 | ||
48 | validate = context['validate'] | |
49 | platform = try_get( | |
add96eb9 | 50 | context, lambda x: x['platform']['key'], str) or 'firefox' |
4d32b638 RA |
51 | |
52 | try: | |
53 | response = self._download_json( | |
54 | 'http://api.tvplayer.com/api/v2/stream/live', | |
b364c87c | 55 | display_id, 'Downloading JSON stream', headers={ |
4d32b638 RA |
56 | 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', |
57 | }, data=urlencode_postdata({ | |
b364c87c | 58 | 'id': resource_id, |
4d32b638 RA |
59 | 'service': 1, |
60 | 'platform': platform, | |
4d32b638 RA |
61 | 'validate': validate, |
62 | }))['tvplayer']['response'] | |
63 | except ExtractorError as e: | |
3d2623a8 | 64 | if isinstance(e.cause, HTTPError): |
4d32b638 | 65 | response = self._parse_json( |
3d2623a8 | 66 | e.cause.response.read().decode(), resource_id)['tvplayer']['response'] |
4d32b638 | 67 | raise ExtractorError( |
add96eb9 | 68 | '{} said: {}'.format(self.IE_NAME, response['error']), expected=True) |
4d32b638 RA |
69 | raise |
70 | ||
b364c87c | 71 | formats = self._extract_m3u8_formats(response['stream'], display_id, 'mp4') |
4d32b638 RA |
72 | |
73 | return { | |
74 | 'id': resource_id, | |
75 | 'display_id': display_id, | |
39ca3b5c | 76 | 'title': title, |
4d32b638 RA |
77 | 'formats': formats, |
78 | 'is_live': True, | |
79 | } |