Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin czech tv #1508

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/plugin_matrix.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ chaturbate chaturbate.com Yes No
connectcast connectcast.tv Yes Yes
crunchyroll crunchyroll.com -- Yes
cybergame cybergame.tv Yes Yes
czech_tv ceskatelevize.cz Yes Yes Streams may be geo-restricted
to the Czech Republic
dailymotion dailymotion.com Yes Yes
disney_de - video.disney.de Yes Yes Streams may be geo-restricted to Germany.
- disneychannel.de
Expand Down
131 changes: 131 additions & 0 deletions src/livestreamer/plugins/czech_tv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"""
Plugin for Czech TV (Ceska televize).

Following channels are working:
* CT1 - http://www.ceskatelevize.cz/ct1/zive/
* CT2 - http://www.ceskatelevize.cz/ct2/zive/
* CT24 - http://www.ceskatelevize.cz/ct24/
* CT sport - http://www.ceskatelevize.cz/sport/zive-vysilani/
* CT Decko - http://decko.ceskatelevize.cz/zive/
* CT Art - http://www.ceskatelevize.cz/art/zive/

Additionaly, videos from iVysilani archive should work as well.
"""
import re

from livestreamer.plugin import Plugin
from livestreamer.plugin.api import http, validate
from livestreamer.stream import HLSStream
from livestreamer.exceptions import PluginError

_url_re = re.compile(
r'http(s)?://([^.]*.)?ceskatelevize.cz'
)
_player_re = re.compile(
r'ivysilani/embed/iFramePlayer[^"]+'
)
_hash_re = re.compile(
r'hash:"([0-9a-z]+)"'
)
_playlist_info_re = re.compile(
r'{"type":"([a-z]+)","id":"([0-9]+)"'
)
_playlist_url_schema = validate.Schema({
"url": validate.url()
})
_playlist_schema = validate.Schema({
"playlist": [{
"streamUrls": {
"main": validate.url(),
}
}]
})


def _find_playlist_info(response):
"""
Finds playlist info (type, id) in HTTP response.

:param response: Response object.
:returns: Dictionary with type and id.
"""
values = {}
matches = _playlist_info_re.search(response.text)
if matches:
values['type'] = matches.group(1)
values['id'] = matches.group(2)

return values


def _find_player_url(response):
"""
Finds embedded player url in HTTP response.

:param response: Response object.
:returns: Player url (str).
"""
url = ''
matches = _player_re.search(response.text)
if matches:
tmp_url = matches.group(0).replace('&', '&')
if 'hash' not in tmp_url:
# there's no hash in the URL, try to find it
matches = _hash_re.search(response.text)
if matches:
url = tmp_url+'&hash='+matches.group(1)
else:
url = tmp_url

return 'http://ceskatelevize.cz/'+url

class CzechTV(Plugin):

@classmethod
def can_handle_url(cls, url):
return _url_re.match(url)

def _get_streams(self):
# fetch requested url and find playlist info
response = http.get(self.url)
info = _find_playlist_info(response)

if not info:
# playlist info not found, let's try to find player url
player_url = _find_player_url(response)
if not player_url:
raise PluginError('Cannot find playlist info or player url!')

# get player url and try to find playlist info in it
response = http.get(player_url)
info = _find_playlist_info(response)
if not info:
raise PluginError('Cannot find playlist info in the player url!')

data = {
'playlist[0][type]': info['type'],
'playlist[0][id]': info['id'],
'requestUrl': '/ivysilani/embed/iFramePlayerCT24.php',
'requestSource': 'iVysilani',
'type': 'html'
}
headers = {
'x-addr': '127.0.0.1',
}

# fetch playlist url
response = http.post(
'http://www.ceskatelevize.cz/ivysilani/ajax/get-client-playlist',
data=data,
headers=headers
)
json_data = http.json(response, schema=_playlist_url_schema)

# fetch playlist
response = http.post(json_data['url'])
json_data = http.json(response, schema=_playlist_schema)
playlist = json_data['playlist'][0]['streamUrls']['main']
return HLSStream.parse_variant_playlist(self.session, playlist)


__plugin__ = CzechTV