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