Repository: streamlink/streamlink

Issue:
plugins.vk.com: api problem
### Checklist

- [X] This is a [plugin issue](https://streamlink.github.io/plugins.html) and not [a different kind of issue](https://github.com/streamlink/streamlink/issues/new/choose)
- [X] [I have read the contribution guidelines](https://github.com/streamlink/streamlink/blob/master/CONTRIBUTING.md#contributing-to-streamlink)
- [X] [I have checked the list of open and recently closed plugin issues](https://github.com/streamlink/streamlink/issues?q=is%3Aissue+label%3A%22plugin+issue%22)
- [X] [I have checked the commit log of the master branch](https://github.com/streamlink/streamlink/commits/master)

### Collaboration

- [X] [I will provide feedback should a pull request be opened with a fix for the plugin](https://github.com/streamlink/streamlink/blob/master/CONTRIBUTING.md#pull-request-feedback)

### Streamlink version

Streamlink version (6.10.0)

### Description

Live stream not working https://vk.com/video-122792749_456242046?list=ln-YXS9u57K48LhkU6C3X

### Debug log

```text
streamlink -l debug https://vk.com/video-122792749_456242046?list=ln-YXS9u57K48LhkU6C3X
[session][debug] Loading plugin: vk
[cli][debug] OS:         Windows 11
[cli][debug] Python:     3.12.5
[cli][debug] OpenSSL:    OpenSSL 3.0.13 30 Jan 2024
[cli][debug] Streamlink: 6.10.0
[cli][debug] Dependencies:
[cli][debug]  certifi: 2024.8.30
[cli][debug]  isodate: 0.6.1
[cli][debug]  lxml: 5.3.0
[cli][debug]  pycountry: 24.6.1
[cli][debug]  pycryptodome: 3.20.0
[cli][debug]  PySocks: 1.7.1
[cli][debug]  requests: 2.32.3
[cli][debug]  trio: 0.26.2
[cli][debug]  trio-websocket: 0.11.1
[cli][debug]  typing-extensions: 4.12.2
[cli][debug]  urllib3: 2.2.2
[cli][debug]  websocket-client: 1.8.0
[cli][debug] Arguments:
[cli][debug]  url=https://vk.com/video-122792749_456242046?list=ln-YXS9u57K48LhkU6C3X
[cli][debug]  --loglevel=debug
[cli][debug]  --ffmpeg-ffmpeg=C:\Program Files\Streamlink\ffmpeg\ffmpeg.exe
[cli][info] Found matching plugin vk for URL https://vk.com/video-122792749_456242046?list=ln-YXS9u57K48LhkU6C3X
[plugins.vk][debug] Video ID: -122792749_456242046
[plugins.vk][error] Could not parse API response
error: No playable streams found on this URL: https://vk.com/video-122792749_456242046?list=ln-YXS9u57K48LhkU6C3X
```


Discussion:

Patch:
diff --git a/src/streamlink/plugins/vk.py b/src/streamlink/plugins/vk.py
index be5a5cd2..6e594300 100644
--- a/src/streamlink/plugins/vk.py
+++ b/src/streamlink/plugins/vk.py
@@ -25,10 +25,12 @@ log = logging.getLogger(__name__)
 
 
 @pluginmatcher(
-    re.compile(r"https?://(?:\w+\.)?vk\.(?:com|ru)/videos?(?:\?z=video)?(?P<video_id>-?\d+_\d+)"),
+    name="video",
+    pattern=re.compile(r"https?://(?:\w+\.)?vk\.(?:com|ru)/video(?P<id>-?\d+_\d+)"),
 )
 @pluginmatcher(
-    re.compile(r"https?://(\w+\.)?vk\.(?:com|ru)/.+"),
+    name="default",
+    pattern=re.compile(r"https?://(\w+\.)?vk\.(?:com|ru)/(?!video-?\d+_\d+).+"),
 )
 class VK(Plugin):
     API_URL = "https://vk.com/al_video.php"
@@ -50,18 +52,15 @@ class VK(Plugin):
         url = urlunparse(urlparse(self.url)._replace(path="", query="", fragment=""))
         self.session.http.get(url, hooks={"response": on_response})
 
-    def _has_video_id(self):
-        return any(self.matches[:-1])
-
     def follow_vk_redirect(self):
-        if self._has_video_id():
+        if self.matches["video"]:
             return
 
         try:
             parsed_url = urlparse(self.url)
             true_path = next(unquote(v).split("/")[0] for k, v in parse_qsl(parsed_url.query) if k == "z" and len(v) > 0)
             self.url = f"{parsed_url.scheme}://{parsed_url.netloc}/{true_path}"
-            if self._has_video_id():
+            if self.matches["video"]:
                 return
         except StopIteration:
             pass
@@ -77,7 +76,7 @@ class VK(Plugin):
             )
         except PluginError:
             pass
-        if self._has_video_id():
+        if self.matches["video"]:
             return
 
         raise NoStreamsError
@@ -86,50 +85,63 @@ class VK(Plugin):
         self._get_cookies()
         self.follow_vk_redirect()
 
-        self.id = self.match.group("video_id")
+        self.id = self.match["id"]
         if not self.id:
             return
 
-        log.debug(f"Video ID: {self.id}")
-        try:
-            data = self.session.http.post(
-                self.API_URL,
-                params={"act": "show"},
-                data={
-                    "act": "show",
-                    "al": "1",
-                    "video": self.id,
-                },
-                headers={"Referer": self.url},
-                schema=validate.Schema(
-                    validate.transform(lambda text: re.sub(r"^\s*<!--\s*", "", text)),
-                    validate.parse_json(),
-                    {"payload": list},
-                    validate.get(("payload", -1)),
-                    list,
-                    validate.get(-1),
-                    {"player": {"params": [dict]}},
-                    validate.get(("player", "params", 0)),
-                    {
-                        validate.optional("hls"): validate.any(None, validate.url()),
-                        validate.optional("hls_live"): validate.any(None, validate.url()),
-                        validate.optional("dash_live"): validate.any(None, validate.url()),
-                        validate.optional("md_author"): validate.any(None, str),
-                        validate.optional("md_title"): validate.any(None, str),
-                    },
+        qsd = dict(parse_qsl(urlparse(self.url).query or ""))
+        playlist = qsd.get("list", "")
+
+        log.debug(f"{self.id=}, {playlist=}")
+
+        data = self.session.http.post(
+            self.API_URL,
+            params={"act": "show"},
+            data={
+                "act": "show",
+                "al": "1",
+                "list": playlist,
+                "video": self.id,
+            },
+            headers={
+                "Referer": self.url,
+                "X-Requested-With": "XMLHttpRequest",
+            },
+            schema=validate.Schema(
+                validate.parse_json(),
+                {"payload": list},
+                validate.get(("payload", -1)),
+                list,
+                validate.get(-1),
+                validate.any(
+                    str,
+                    validate.all(
+                        {"player": {"params": list}},
+                        validate.get(("player", "params", 0)),
+                        {
+                            validate.optional("hls"): validate.any(None, validate.url()),
+                            validate.optional("hls_live"): validate.any(None, validate.url()),
+                            validate.optional("hls_ondemand"): validate.any(None, validate.url()),
+                            validate.optional("dash_live"): validate.any(None, validate.url()),
+                            validate.optional("dash_ondemand"): validate.any(None, validate.url()),
+                            validate.optional("md_author"): validate.any(None, str),
+                            validate.optional("md_title"): validate.any(None, str),
+                        },
+                    ),
                 ),
-            )
-        except PluginError:
-            log.error("Could not parse API response")
+            ),
+        )
+        if type(data) is not dict:
+            log.error("Video is inaccessible")
             return
 
         self.author = data.get("md_author")
         self.title = data.get("md_title")
 
-        if hls := data.get("hls_live") or data.get("hls"):
+        if hls := data.get("hls_live") or data.get("hls_ondemand") or data.get("hls"):
             return HLSStream.parse_variant_playlist(self.session, hls)
 
-        if dash := data.get("dash_live"):
+        if dash := data.get("dash_live") or data.get("dash_ondemand"):
             return DASHStream.parse_manifest(self.session, dash)
 
 


Test patch:
diff --git a/tests/plugins/test_vk.py b/tests/plugins/test_vk.py
index 9ff82134..6a6a4a16 100644
--- a/tests/plugins/test_vk.py
+++ b/tests/plugins/test_vk.py
@@ -15,21 +15,27 @@ does_not_raise = nullcontext()
 class TestPluginCanHandleUrlVK(PluginCanHandleUrl):
     __plugin__ = VK
 
-    should_match = [
-        "http://vk.com/video-24136539_456239830",
-        "http://vk.ru/video-24136539_456239830",
-        "https://vk.com/video-9944999_456239622",
-        "https://vk.ru/video-9944999_456239622",
-        "https://www.vk.com/video-34453259_456240574",
-        "https://www.vk.ru/video-34453259_456240574",
-        "https://vk.com/videos-24136539?z=video-24136539_456241155%2Fpl_-24136539_-2",
-        "https://vk.com/video?z=video-15755094_456245149%2Fpl_cat_lives",
-        "https://vk.com/video?z=video132886594_167211693%2Fpl_cat_8",
-        "https://vk.com/video132886594_167211693",
-        "https://vk.com/videos132886594?z=video132886594_167211693",
-        "https://vk.com/video-73154028_456239128",
-        "https://vk.com/dota2?w=wall-126093223_1057924",
-        "https://vk.com/search?c%5Bq%5D=dota&c%5Bsection%5D=auto&z=video295508334_171402661",
+    should_match_groups = [
+        # vod
+        (("video", "https://vk.com/video-9944999_456239622"), {"id": "-9944999_456239622"}),
+        (("video", "https://vk.ru/video-9944999_456239622"), {"id": "-9944999_456239622"}),
+        # live
+        (("video", "https://www.vk.com/video-143491903_456240010"), {"id": "-143491903_456240010"}),
+        (("video", "https://www.vk.ru/video-143491903_456240010"), {"id": "-143491903_456240010"}),
+        # video ID without leading dash
+        (("video", "https://vk.com/video132886594_167211693"), {"id": "132886594_167211693"}),
+        # video with required list param
+        (("video", "https://vk.com/video-122792749_456242046?list=ln-YXS9u57K48LhkU6C3X"), {"id": "-122792749_456242046"}),
+        # video/lives
+        (("default", "https://vk.com/video/lives?z=video-143491903_456240010"), {}),
+        # other paths / formats
+        (("default", "https://vk.com/videos-24136539?z=video-24136539_456241155%2Fpl_-24136539_-2"), {}),
+        (("default", "https://vk.com/video/@vesti?z=video-24136539_456241155%2Fpl_-24136539_-2"), {}),
+        (("default", "https://vk.com/video?z=video-15755094_456245149%2Fpl_cat_lives"), {}),
+        (("default", "https://vk.com/video?z=video132886594_167211693%2Fpl_cat_8"), {}),
+        # search
+        (("default", "https://vk.com/dota2?w=wall-126093223_1057924"), {}),
+        (("default", "https://vk.com/search?c%5Bq%5D=dota&c%5Bsection%5D=auto&z=video295508334_171402661"), {}),
     ]
 
     should_not_match = [
@@ -38,50 +44,55 @@ class TestPluginCanHandleUrlVK(PluginCanHandleUrl):
     ]
 
 
-@pytest.mark.parametrize(("url", "newurl", "raises"), [
-    # canonical video URL
-    pytest.param(
-        "https://vk.com/video-24136539_456241176",
-        "https://vk.com/video-24136539_456241176",
-        does_not_raise,
-    ),
+@pytest.mark.parametrize(
+    ("url", "newurl", "raises"),
+    [
+        pytest.param(
+            "https://vk.com/video-24136539_456241176",
+            "https://vk.com/video-24136539_456241176",
+            does_not_raise,
+            id="canonical-video-url",
+        ),
+        pytest.param(
+            "https://vk.com/videos-24136539?z=video-24136539_456241181%2Fpl_-24136539_-2",
+            "https://vk.com/video-24136539_456241181",
+            does_not_raise,
+            id="z-querystring-parameter-with-pl",
+        ),
+        pytest.param(
+            "https://vk.com/videos132886594?z=video132886594_167211693",
+            "https://vk.com/video132886594_167211693",
+            does_not_raise,
+            id="z-querystring-parameter",
+        ),
+        pytest.param(
+            "https://vk.com/search?c%5Bq%5D=dota&c%5Bsection%5D=auto&z=video295508334_171402661",
+            "https://vk.com/video295508334_171402661",
+            does_not_raise,
+            id="search-z-querystring-parameter",
+        ),
+        pytest.param(
+            "https://vk.com/dota2?w=wall-126093223_1057924",
+            "https://vk.com/video-126093223_1057924",
+            does_not_raise,
+            id="from-meta-og-url",
+        ),
 
-    # video ID from URL
-    pytest.param(
-        "https://vk.com/videos-24136539?z=video-24136539_456241181%2Fpl_-24136539_-2",
-        "https://vk.com/video-24136539_456241181",
-        does_not_raise,
-    ),
-    pytest.param(
-        "https://vk.com/videos132886594?z=video132886594_167211693",
-        "https://vk.com/video132886594_167211693",
-        does_not_raise,
-    ),
-    pytest.param(
-        "https://vk.com/search?c%5Bq%5D=dota&c%5Bsection%5D=auto&z=video295508334_171402661",
-        "https://vk.com/video295508334_171402661",
-        does_not_raise,
-    ),
-
-    # video ID from HTTP request
-    pytest.param(
-        "https://vk.com/dota2?w=wall-126093223_1057924",
-        "https://vk.com/video-126093223_1057924",
-        does_not_raise,
-    ),
-
-    # no video ID
-    pytest.param(
-        "https://vk.com/videos-0123456789?z=",
-        "",
-        pytest.raises(NoStreamsError),
-    ),
-    pytest.param(
-        "https://vk.com/video/for_kids?z=video%2Fpl_-22277933_51053000",
-        "",
-        pytest.raises(NoStreamsError),
-    ),
-])
+        # no video ID
+        pytest.param(
+            "https://vk.com/videos-0123456789?z=",
+            "",
+            pytest.raises(NoStreamsError),
+            id="no-streams-1",
+        ),
+        pytest.param(
+            "https://vk.com/video/for_kids?z=video%2Fpl_-22277933_51053000",
+            "",
+            pytest.raises(NoStreamsError),
+            id="no-streams-2",
+        ),
+    ],
+)
 def test_url_redirect(requests_mock: rm.Mocker, session: Streamlink, url: str, newurl: str, raises: nullcontext):
     # noinspection PyTypeChecker
     plugin: VK = VK(session, url)