From de8d8ee49a60acd63a388130cae2c11334c0914f Mon Sep 17 00:00:00 2001 From: Beardypig Date: Thu, 3 Nov 2016 16:33:54 +0000 Subject: [PATCH] TVPlayer.com: fix for 400 error, correctly set the platform parameter (#123) * tvplayer: send the user-agent header when making the intial request, so that platform is correctly populated * tvplayer: update the url matching and include some tests --- src/streamlink/plugins/tvplayer.py | 7 ++++--- tests/test_plugin_tvplayer.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 tests/test_plugin_tvplayer.py diff --git a/src/streamlink/plugins/tvplayer.py b/src/streamlink/plugins/tvplayer.py index ffe8f191..a16d6d34 100644 --- a/src/streamlink/plugins/tvplayer.py +++ b/src/streamlink/plugins/tvplayer.py @@ -11,7 +11,7 @@ class TVPlayer(Plugin): "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/43.0.2357.65 Safari/537.36") API_URL = "http://api.tvplayer.com/api/v2/stream/live" - _url_re = re.compile(r"http://(?:www.)?tvplayer.com/watch/(.+)") + _url_re = re.compile(r"https?://(?:www.)?tvplayer.com/(:?watch/?|watch/(.+)?)") _stream_attrs_ = re.compile(r'var\s+(validate|platform|resourceId)\s+=\s*(.*?);', re.S) _stream_schema = validate.Schema({ "tvplayer": validate.Schema({ @@ -25,11 +25,12 @@ class TVPlayer(Plugin): @classmethod def can_handle_url(cls, url): match = TVPlayer._url_re.match(url) - return match + return match is not None def _get_streams(self): # find the list of channels from the html in the page - res = http.get(self.url) + self.url = self.url.replace("https", "http") # https redirects to http + res = http.get(self.url, headers={"User-Agent": TVPlayer._user_agent}) stream_attrs = dict((k, v.strip('"')) for k, v in TVPlayer._stream_attrs_.findall(res.text)) # get the stream urls diff --git a/tests/test_plugin_tvplayer.py b/tests/test_plugin_tvplayer.py new file mode 100644 index 00000000..b3f1a6a3 --- /dev/null +++ b/tests/test_plugin_tvplayer.py @@ -0,0 +1,21 @@ +import unittest + +from streamlink.plugins.tvplayer import TVPlayer + + +class TestPluginTVPlayer(unittest.TestCase): + def test_can_handle_url(self): + # should match + self.assertTrue(TVPlayer.can_handle_url("http://tvplayer.com/watch/")) + self.assertTrue(TVPlayer.can_handle_url("http://www.tvplayer.com/watch/")) + self.assertTrue(TVPlayer.can_handle_url("http://tvplayer.com/watch")) + self.assertTrue(TVPlayer.can_handle_url("http://www.tvplayer.com/watch")) + self.assertTrue(TVPlayer.can_handle_url("http://tvplayer.com/watch/dave")) + self.assertTrue(TVPlayer.can_handle_url("http://www.tvplayer.com/watch/itv")) + self.assertTrue(TVPlayer.can_handle_url("https://www.tvplayer.com/watch/itv")) + self.assertTrue(TVPlayer.can_handle_url("https://tvplayer.com/watch/itv")) + + # shouldn't match + self.assertFalse(TVPlayer.can_handle_url("http://www.tvplayer.com/")) + self.assertFalse(TVPlayer.can_handle_url("http://www.tvcatchup.com/")) + self.assertFalse(TVPlayer.can_handle_url("http://www.youtube.com/"))