Repository: streamlink/streamlink
Pull request #2248 (2019-01-13 18:51:57)

plugins.kanal7: Fixed iframe_re/stream_re, added new domain
closes https://github.com/streamlink/streamlink/issues/2215


Patch:
diff --git a/docs/plugin_matrix.rst b/docs/plugin_matrix.rst
index c8318fcb..79115c30 100644
--- a/docs/plugin_matrix.rst
+++ b/docs/plugin_matrix.rst
@@ -101,7 +101,8 @@ huya                    huya.com             Yes   No    Temporarily only HLS st
 idf1                    idf1.fr              Yes   Yes
 ine                     ine.com              ---   Yes
 itvplayer               itv.com/itvplayer    Yes   Yes   Streams may be geo-restricted to Great Britain.
-kanal7                  kanal7.com           Yes   No
+kanal7                  - kanal7.com         Yes   No
+                        - tvt.tv.tr
 kingkong                kingkong.com.tw      Yes   --
 live_russia_tv          live.russia.tv       Yes   --
 liveedu                 - liveedu.tv         Yes   --    Some streams require a login.
diff --git a/src/streamlink/plugins/kanal7.py b/src/streamlink/plugins/kanal7.py
index 3d1e5103..29d0f463 100644
--- a/src/streamlink/plugins/kanal7.py
+++ b/src/streamlink/plugins/kanal7.py
@@ -1,17 +1,18 @@
-from __future__ import print_function
+import logging
 import re
 
+from streamlink.compat import urljoin, urlparse
 from streamlink.plugin import Plugin
-from streamlink.plugin.api import useragents
-from streamlink.plugin.api import validate
 from streamlink.stream import HLSStream
 from streamlink.utils import update_scheme
 
+log = logging.getLogger(__name__)
+
 
 class Kanal7(Plugin):
-    url_re = re.compile(r"https?://(?:www.)?kanal7.com/canli-izle")
-    iframe_re = re.compile(r'iframe .*?src="((?:http:)?//[^"]*?)"')
-    stream_re = re.compile(r'''video-source\s*=\s*['"](http[^"']*?)['"]''')
+    url_re = re.compile(r"https?://(?:www.)?(?:kanal7\.com|tvt\.tv\.tr)/canli-(?:izle|yayin)")
+    iframe_re = re.compile(r'iframe .*?src="([^"]*?iframe\.php)"')
+    stream_re = re.compile(r'''video-source(?:\s*=\s*|["'],)['"](http[^"']*?)['"]''')
 
     @classmethod
     def can_handle_url(cls, url):
@@ -23,24 +24,23 @@ class Kanal7(Plugin):
         iframe = self.iframe_re.search(res.text)
         iframe_url = iframe and iframe.group(1)
         if iframe_url:
+            parsed = urlparse(iframe_url)
+            if not parsed.netloc:
+                iframe_url = urljoin(self.url, iframe_url)
             iframe_url = update_scheme(self.url, iframe_url)
-            self.logger.debug("Found iframe: {}", iframe_url)
+            log.debug("Found iframe: {0}".format(iframe_url))
         return iframe_url
 
     def _get_streams(self):
         iframe1 = self.find_iframe(self.url)
         if iframe1:
-            iframe2 = self.find_iframe(iframe1)
-            if iframe2:
-                ires = self.session.http.get(iframe2)
-                stream_m = self.stream_re.search(ires.text)
-                stream_url = stream_m and stream_m.group(1)
-                if stream_url:
-                    yield "live", HLSStream(self.session, stream_url, headers={"Referer": iframe2})
-            else:
-                self.logger.error("Could not find second iframe, has the page layout changed?")
+            ires = self.session.http.get(iframe1)
+            stream_m = self.stream_re.search(ires.text)
+            stream_url = stream_m and stream_m.group(1)
+            if stream_url:
+                yield "live", HLSStream(self.session, stream_url, headers={"Referer": iframe1})
         else:
-            self.logger.error("Could not find iframe, has the page layout changed?")
+            log.error("Could not find iframe, has the page layout changed?")
 
 
 __plugin__ = Kanal7


Test patch:
diff --git a/tests/plugins/test_kanal7.py b/tests/plugins/test_kanal7.py
index b54a7ac7..8d539625 100644
--- a/tests/plugins/test_kanal7.py
+++ b/tests/plugins/test_kanal7.py
@@ -7,6 +7,7 @@ class TestPluginKanal7(unittest.TestCase):
     def test_can_handle_url(self):
         should_match = [
             'http://www.kanal7.com/canli-izle',
+            'https://www.tvt.tv.tr/canli-yayin',
         ]
         for url in should_match:
             self.assertTrue(Kanal7.can_handle_url(url))
@@ -17,3 +18,31 @@ class TestPluginKanal7(unittest.TestCase):
         ]
         for url in should_not_match:
             self.assertFalse(Kanal7.can_handle_url(url))
+
+    def test_iframe_re(self):
+        test_list = [
+            # http://www.kanal7.com/canli-izle
+            ('<iframe id="selectFrame" src="https://www.kanal7.com/canli-yayin-iframe.php" scrolling="no"></iframe>',
+             'https://www.kanal7.com/canli-yayin-iframe.php'),
+            # https://www.tvt.tv.tr/canli-yayin
+            ('<iframe id="selectFrame" src="/canli-yayin-iframe.php" height="650"></iframe>',
+             '/canli-yayin-iframe.php'),
+        ]
+        for text, url in test_list:
+            m = Kanal7.iframe_re.search(text)
+            self.assertIsNotNone(m, url)
+            self.assertEqual(m.group(1), url, url)
+
+    def test_stream_re(self):
+        test_list = [
+            # http://www.kanal7.com/canli-izle
+            ('''script.setAttribute('video-source','https://live/kanal7LiveDesktop/index.m3u8');''',
+             'https://live/kanal7LiveDesktop/index.m3u8'),
+            # https://www.tvt.tv.tr/canli-yayin
+            ('''video-source="https://live/tvtLiveStream/index.m3u8"''',
+             'https://live/tvtLiveStream/index.m3u8'),
+        ]
+        for text, url in test_list:
+            m = Kanal7.stream_re.search(text)
+            self.assertIsNotNone(m, url)
+            self.assertEqual(m.group(1), url, url)