Repository: streamlink/streamlink

Issue:
plugins.huya: 403 Client Error: Forbidden for url
### 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)

### Streamlink version

streamlink 6.5.1

### Description

please forgive me, my english so bad and  it's hard to understand all these rules, im newbie inpython, I don’t know, this has already been asked and I read the answer that this is not a streamlink problem, but maybe you can help me



### Debug log

```text
C:\Users\matebeats>streamlink.exe https://www.huya.com/sbwfz best
[cli][info] Found matching plugin huya for URL https://www.huya.com/sbwfz
[cli][info] Available streams: source_hw (worst, best)
[cli][info] Opening stream: source_hw (http)
[cli][info] Starting player: D:\matebeats\soft\media\VLC\vlc.exe
[cli][error] Try 1/1: Could not open stream <HTTPStream ['http', 'https://hw.flv.huya.com/src/70165683-70165683-301359313786503168-140454822-10057-A-0-1.flv?wsSecret=79ab95ca0c616135da585dba0e2373ef&wsTime=65a9468d&fm=RFdxOEJjSjNoNkRKdDZUWV8kMF8kMV8kMl8kMw%3D%3D&ctype=huya_live&fs=bgct']> (Could not open stream: Unable to open URL: https://hw.flv.huya.com/src/70165683-70165683-301359313786503168-140454822-10057-A-0-1.flv?wsSecret=79ab95ca0c616135da585dba0e2373ef&wsTime=65a9468d&fm=RFdxOEJjSjNoNkRKdDZUWV8kMF8kMV8kMl8kMw%3D%3D&ctype=huya_live&fs=bgct (403 Client Error: Forbidden for url: https://7b232e9a7ece13f936b4e19cb1594132.livehwc3.cn/hw.flv.huya.com/src/70165683-70165683-301359313786503168-140454822-10057-A-0-1.flv?cdn_redirect_domain=14769ccad573a2388a716f2ade587c96.livehwc3.cn&cdn_redirect=true&wsTime=65a9468d&ctype=huya_live&fs=bgct&policy=6&fm=RFdxOEJjSjNoNkRKdDZUWV8kMF8kMV8kMl8kMw%3D%3D&wsSecret=79ab95ca0c616135da585dba0e2373ef&cdn_redirect_tag=1705592487))
error: Could not open stream <HTTPStream ['http', 'https://hw.flv.huya.com/src/70165683-70165683-301359313786503168-140454822-10057-A-0-1.flv?wsSecret=79ab95ca0c616135da585dba0e2373ef&wsTime=65a9468d&fm=RFdxOEJjSjNoNkRKdDZUWV8kMF8kMV8kMl8kMw%3D%3D&ctype=huya_live&fs=bgct']>, tried 1 times, exiting
```



Discussion:
what should I do and where? im replace huya.py but its not work, then I manually deleted where the - was and inserted it where + the stream started for a second, test_huya.py I didn't find the streamlink in the directory, sorry again for my knowledge

> I manually deleted where the - was and inserted it where +

You were looking at the code **diff** format, which is not intended for you to copy and modify by hand.

Just download the full plugin file from here (the "raw" file link) and sideload it:
https://raw.githubusercontent.com/bastimeyer/streamlink/plugins/huya/5784/src/streamlink/plugins/huya.py

More help here:

- https://github.com/streamlink/streamlink/blob/master/CONTRIBUTING.md#pull-request-feedback
- https://streamlink.github.io/latest/cli/plugin-sideloading.html

If you can, please provide feedback with a full **debug log** (`--loglevel=debug`), so we can see that you are correctly sideloading the new plugin. Please check if any streams on this site are not working, because as I said in the pull request, I am not sure if my changes are working on all live streams. Thank you.

Resolving patch:
diff --git a/src/streamlink/plugins/huya.py b/src/streamlink/plugins/huya.py
--- a/src/streamlink/plugins/huya.py
+++ b/src/streamlink/plugins/huya.py
@@ -12,22 +12,28 @@
 import re
 from html import unescape as html_unescape
 from typing import Dict
+from urllib.parse import parse_qsl
 
 from streamlink.plugin import Plugin, pluginmatcher
 from streamlink.plugin.api import validate
 from streamlink.stream.http import HTTPStream
-from streamlink.utils.url import update_scheme
+from streamlink.utils.url import update_qsd, update_scheme
 
 
 log = logging.getLogger(__name__)
 
 
 @pluginmatcher(re.compile(
-    r"https?://(?:www\.)?huya\.com/(?P<channel>[^/]+)",
+    r"https?://(?:www\.)?huya\.com/(?P<channel>[^/?]+)",
 ))
 class Huya(Plugin):
     QUALITY_WEIGHTS: Dict[str, int] = {}
 
+    _QUALITY_WEIGHTS_OVERRIDE = {
+        "source_hy": -1000,  # SSLCertVerificationError
+    }
+    _STREAM_URL_QUERYSTRING_PARAMS = "wsSecret", "wsTime"
+
     @classmethod
     def stream_weight(cls, key):
         weight = cls.QUALITY_WEIGHTS.get(key)
@@ -97,13 +103,18 @@ def _get_streams(self):
 
         self.id, self.author, self.title, streamdata = data
 
+        self.session.http.headers.update({
+            "Origin": "https://www.huya.com",
+            "Referer": "https://www.huya.com/",
+        })
+
         for cdntype, priority, streamname, flvurl, suffix, anticode in streamdata:
-            url = update_scheme("https://", f"{flvurl}/{streamname}.{suffix}?{anticode}")
-            if self.session.http.head(url, raise_for_status=False).status_code >= 400:
-                continue
+            qs = {k: v for k, v in dict(parse_qsl(anticode)).items() if k in self._STREAM_URL_QUERYSTRING_PARAMS}
+            url = update_scheme("https://", f"{flvurl}/{streamname}.{suffix}")
+            url = update_qsd(url, qs)
 
             name = f"source_{cdntype.lower()}"
-            self.QUALITY_WEIGHTS[name] = priority
+            self.QUALITY_WEIGHTS[name] = self._QUALITY_WEIGHTS_OVERRIDE.get(name, priority)
             yield name, HTTPStream(self.session, url)
 
         log.debug(f"QUALITY_WEIGHTS: {self.QUALITY_WEIGHTS!r}")


Test patch:
diff --git a/tests/plugins/test_huya.py b/tests/plugins/test_huya.py
--- a/tests/plugins/test_huya.py
+++ b/tests/plugins/test_huya.py
@@ -5,14 +5,10 @@
 class TestPluginCanHandleUrlHuya(PluginCanHandleUrl):
     __plugin__ = Huya
 
-    should_match = [
-        "http://www.huya.com/123123123",
-        "http://www.huya.com/name",
-        "https://www.huya.com/123123123",
-        "https://www.huya.com/name",
+    should_match_groups = [
+        ("https://www.huya.com/CHANNEL", {"channel": "CHANNEL"}),
     ]
 
     should_not_match = [
-        "http://www.huya.com",
         "https://www.huya.com",
     ]