Repository: streamlink/streamlink
Pull request #2510 (2019-06-25 00:24:44)

plugins.welt: fix plugin
Fixes the plugin after the recent site layout changes. Live TV stream and VODs are now working again.


Patch:
diff --git a/src/streamlink/plugins/welt.py b/src/streamlink/plugins/welt.py
index a159b048..9f95c85b 100644
--- a/src/streamlink/plugins/welt.py
+++ b/src/streamlink/plugins/welt.py
@@ -3,20 +3,20 @@ import re
 from streamlink.compat import quote
 from streamlink.plugin import Plugin
 from streamlink.plugin.api import useragents, validate
+from streamlink.plugin.api.utils import itertags
 from streamlink.stream import HLSStream
 from streamlink.utils import parse_json
 
 
-def _filter_url(url):
-    try:
-        Welt._schema_url.validate(url)
-        return True
-    except:
-        return False
+def get_json(text):
+    for script in itertags(text, "script"):
+        if script.attributes.get("type") == "application/json" \
+                and script.attributes.get("data-content") == "VideoPlayer.Config":
+            return script.text
+    return None
 
 
 class Welt(Plugin):
-    _url_vod = "https://www.welt.de/onward/video/token/{0}"
     _re_url = re.compile(
         r"""https?://(\w+\.)?welt\.de/?""",
         re.IGNORECASE
@@ -25,41 +25,15 @@ class Welt(Plugin):
         r"""mediathek""",
         re.IGNORECASE
     )
-    _re_json = re.compile(
-        r"""
-            <script>\s*
-            var\s+funkotron\s*=\s*
-            \{\s*
-                config\s*:\s*(?P<json>\{.+?\})\s*
-            \}\s*;?\s*
-            </script>
-        """,
-        re.VERBOSE | re.DOTALL | re.IGNORECASE
-    )
+    _url_vod = "https://www.welt.de/onward/video/play/{0}"
     _schema = validate.Schema(
-        validate.transform(_re_json.search),
-        validate.get("json"),
+        validate.transform(get_json),
         validate.transform(parse_json),
-        validate.get("page"),
-        validate.get("content"),
-        validate.get("media"),
-        validate.get(0),
         validate.get("sources"),
-        validate.map(lambda obj: obj["file"]),
-        validate.filter(_filter_url),
+        validate.filter(lambda obj: obj["extension"] == "m3u8"),
+        validate.map(lambda obj: obj["src"]),
         validate.get(0)
     )
-    _schema_url = validate.Schema(
-        validate.url(
-            scheme="https",
-            path=validate.endswith(".m3u8")
-        )
-    )
-    _schema_vod = validate.Schema(
-        validate.transform(parse_json),
-        validate.get("urlWithToken"),
-        _schema_url
-    )
 
     @classmethod
     def can_handle_url(cls, url):
@@ -77,7 +51,7 @@ class Welt(Plugin):
 
         if self.isVod:
             url = self._url_vod.format(quote(hls_url, safe=""))
-            hls_url = self.session.http.get(url, headers=headers, schema=self._schema_vod)
+            hls_url = self.session.http.get(url, headers=headers).url
 
         return HLSStream.parse_variant_playlist(self.session, hls_url, headers=headers)
 


Test patch:
diff --git a/tests/plugins/test_welt.py b/tests/plugins/test_welt.py
index 979872d4..2fc47580 100644
--- a/tests/plugins/test_welt.py
+++ b/tests/plugins/test_welt.py
@@ -25,45 +25,22 @@ class TestPluginWelt(unittest.TestCase):
 
     def test_validate_live(self):
         hls_url = Welt._schema.validate("""
-            foo
-            <script>
-            var funkotron = { config: {
-                "page": {
-                    "content": {
-                        "media": [
-                            {
-                                "sources": [
-                                    {
-                                        "file": "https://foo.bar/baz.mp4?qux"
-                                    },
-                                    {
-                                        "file": "https://foo.bar/baz.m3u8?qux"
-                                    }
-                                ]
-                            },
-                            {
-                                "sources": [
-                                    {
-                                        "file": "https://qux.baz/bar.mp4?foo"
-                                    },
-                                    {
-                                        "file": "https://qux.baz/bar.m3u8?foo"
-                                    }
-                                ]
-                            }
-                        ]
-                    }
+            <!DOCTYPE html><html><body>
+            <script type="application/json" data-content="VideoPlayer.Config">
+                {
+                    "title": "foo",
+                    "sources": [
+                        {
+                            "src": "https://foo.bar/baz.mp4?qux",
+                            "extension": "mp4"
+                        },
+                        {
+                            "src": "https://foo.bar/baz.m3u8?qux",
+                            "extension": "m3u8"
+                        }
+                    ]
                 }
-            }}
             </script>
-            bar
+            </body></html>
         """)
         self.assertEqual(hls_url, "https://foo.bar/baz.m3u8?qux", "Finds the correct HLS live URL")
-
-    def test_validate_vod(self):
-        hls_url = Welt._schema_vod.validate("""
-            {
-                "urlWithToken": "https://foo.bar/baz.m3u8?qux"
-            }
-        """)
-        self.assertEqual(hls_url, "https://foo.bar/baz.m3u8?qux", "Finds the correct HLS VOD URL")