yt-dlp/yt_dlp/extractor/tvplayer.py

80 lines
2.6 KiB
Python
Raw Normal View History

2017-02-09 17:07:43 -05:00
from .common import InfoExtractor
from ..networking.exceptions import HTTPError
2017-02-09 17:07:43 -05:00
from ..utils import (
ExtractorError,
2017-02-09 17:07:43 -05:00
extract_attributes,
try_get,
2017-02-09 17:07:43 -05:00
urlencode_postdata,
)
class TVPlayerIE(InfoExtractor):
_VALID_URL = r'https?://(?:www\.)?tvplayer\.com/watch/(?P<id>[^/?#]+)'
_TEST = {
'url': 'http://tvplayer.com/watch/bbcone',
'info_dict': {
'id': '89',
'ext': 'mp4',
'title': r're:^BBC One [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
},
'params': {
# m3u8 download
'skip_download': True,
},
2017-02-09 17:07:43 -05:00
}
def _real_extract(self, url):
display_id = self._match_id(url)
webpage = self._download_webpage(url, display_id)
current_channel = extract_attributes(self._search_regex(
r'(<div[^>]+class="[^"]*current-channel[^"]*"[^>]*>)',
webpage, 'channel element'))
title = current_channel['data-name']
resource_id = current_channel['data-id']
2017-02-09 17:07:43 -05:00
token = self._search_regex(
r'data-token=(["\'])(?P<token>(?!\1).+)\1', webpage,
'token', group='token')
context = self._download_json(
'https://tvplayer.com/watch/context', display_id,
'Downloading JSON context', query={
'resource': resource_id,
'gen': token,
})
validate = context['validate']
platform = try_get(
context, lambda x: x['platform']['key'], str) or 'firefox'
2017-02-09 17:07:43 -05:00
try:
response = self._download_json(
'http://api.tvplayer.com/api/v2/stream/live',
display_id, 'Downloading JSON stream', headers={
2017-02-09 17:07:43 -05:00
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
}, data=urlencode_postdata({
'id': resource_id,
2017-02-09 17:07:43 -05:00
'service': 1,
'platform': platform,
'validate': validate,
}))['tvplayer']['response']
except ExtractorError as e:
if isinstance(e.cause, HTTPError):
2017-02-09 17:07:43 -05:00
response = self._parse_json(
e.cause.response.read().decode(), resource_id)['tvplayer']['response']
2017-02-09 17:07:43 -05:00
raise ExtractorError(
'{} said: {}'.format(self.IE_NAME, response['error']), expected=True)
2017-02-09 17:07:43 -05:00
raise
formats = self._extract_m3u8_formats(response['stream'], display_id, 'mp4')
2017-02-09 17:07:43 -05:00
return {
'id': resource_id,
'display_id': display_id,
2021-12-15 11:00:46 -05:00
'title': title,
2017-02-09 17:07:43 -05:00
'formats': formats,
'is_live': True,
}