From dc3230ca32535bdfcd75ab2727c2c5cd25691282 Mon Sep 17 00:00:00 2001 From: Anton Tykhyy Date: Thu, 25 Jan 2018 11:48:44 +0200 Subject: [PATCH] Add ok.ru/live plugin --- src/streamlink/plugins/ok_live.py | 41 +++++++++++++++++++++++++++++++ tests/test_plugin_ok_live.py | 15 +++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/streamlink/plugins/ok_live.py create mode 100644 tests/test_plugin_ok_live.py diff --git a/src/streamlink/plugins/ok_live.py b/src/streamlink/plugins/ok_live.py new file mode 100644 index 00000000..32edad59 --- /dev/null +++ b/src/streamlink/plugins/ok_live.py @@ -0,0 +1,41 @@ +import urllib3 +import re + +from streamlink.plugin import Plugin +from streamlink.plugin.api import http, validate +from streamlink.plugin.api import useragents +from streamlink.stream import HLSStream + +_url_re = re.compile(r"https?://(www\.)?ok\.ru/live/\d+") +_vod_re = re.compile(r";(?P[^;]+video\.m3u8)") + +_schema = validate.Schema( + validate.transform(_vod_re.search), + validate.any( + None, + validate.all( + validate.get("hlsurl"), + validate.url() + ) + ) +) + +class OK_live(Plugin): + """ + Support for ok.ru live stream: http://www.ok.ru/live/ + """ + @classmethod + def can_handle_url(cls, url): + return _url_re.match(url) is not None + + def _get_streams(self): + headers = { + 'User-Agent': useragents.CHROME, + 'Referer': self.url + } + + hls = http.get(self.url, headers=headers, schema=_schema) + return HLSStream.parse_variant_playlist(self.session, hls, headers=headers) + + +__plugin__ = OK_live \ No newline at end of file diff --git a/tests/test_plugin_ok_live.py b/tests/test_plugin_ok_live.py new file mode 100644 index 00000000..60a02bcc --- /dev/null +++ b/tests/test_plugin_ok_live.py @@ -0,0 +1,15 @@ +import unittest + +from streamlink.plugins.ok_live import OK_live + + +class TestPluginOK_live(unittest.TestCase): + def test_can_handle_url(self): + # should match + self.assertTrue(OK_live.can_handle_url("https://ok.ru/live/12345")) + self.assertTrue(OK_live.can_handle_url("http://ok.ru/live/12345")) + self.assertTrue(OK_live.can_handle_url("http://www.ok.ru/live/12345")) + + # shouldn't match + self.assertFalse(OK_live.can_handle_url("http://www.tvcatchup.com/")) + self.assertFalse(OK_live.can_handle_url("http://www.youtube.com/"))