mkbloke · GitHub

diff --git a/src/streamlink/plugins/htv.py b/src/streamlink/plugins/htv.py
index ece1d3f0..a23205c3 100644
--- a/src/streamlink/plugins/htv.py
+++ b/src/streamlink/plugins/htv.py
@@ -17,7 +17,7 @@ log = logging.getLogger(__name__)
 @pluginmatcher(re.compile(
-    r"https?://(?:www\.)?htv\.com\.vn/truc-tuyen(?:\?channel=(\d+))?$"
+    r"https?://(?:www\.)?htv\.com\.vn/truc-tuyen(?:\?channel=(?P<channel>\w+)&?|$)"
 ))
 class HTV(Plugin):
     hls_url_re = re.compile(r'var\s+iosUrl\s*=\s*"([^"]+)"')
@@ -25,15 +25,13 @@ class HTV(Plugin):
     def get_channels(self):
         data = self.session.http.get(self.url, schema=validate.Schema(
             validate.parse_html(),
-            validate.xml_findall(".//a[@data-code]"), [
+            validate.xml_xpath(".//*[contains(@class,'channel-list')]//a[@data-id][@data-code]"),
+            [
                 validate.union_get("data-id", "data-code"),
             ],
         ))
-        if not data:
-            return
-
-        return {i[0]: i[1] for i in data}
+        return {k: v for k, v in data}
     def _get_streams(self):
         channels = self.get_channels()
@@ -44,17 +42,16 @@ class HTV(Plugin):
         log.debug(f"channels={channels}")
-        channel_id = list(channels.keys())[0]
-        channel_code = list(channels.values())[0]
-
-        id = self.match.group(1)
-        if id and id not in channels:
-            log.error(f"Unknown channel ID: {id}")
+        channel_id = self.match.group("channel")
+        if channel_id is None:
+            channel_id, channel_code = next(iter(channels.items()))
+        elif channel_id in channels:
+            channel_code = channels[channel_id]
+        else:
+            log.error(f"Unknown channel ID: {channel_id}")
             return
-        if id:
-            channel_id = id
-            channel_code = channels[id]
+        log.info(f"Channel: {channel_code}")
         json = self.session.http.post(
             "https://www.htv.com.vn/HTVModule/Services/htvService.aspx",
@@ -66,7 +63,8 @@ class HTV(Plugin):
                 "date": date.today().strftime("%d-%m-%Y"),
             },
             schema=validate.Schema(
-                validate.parse_json(), {
+                validate.parse_json(),
+                {
                     "success": bool,
                     "chanelUrl": validate.url(),
                 },
diff --git a/tests/plugins/test_htv.py b/tests/plugins/test_htv.py
index 18c18149..ac1306eb 100644
--- a/tests/plugins/test_htv.py
+++ b/tests/plugins/test_htv.py
@@ -5,28 +5,20 @@ from tests.plugins import PluginCanHandleUrl
 class TestPluginCanHandleUrlHTV(PluginCanHandleUrl):
     __plugin__ = HTV
-    should_match = [
-        "http://htv.com.vn/truc-tuyen",
-        "http://www.htv.com.vn/truc-tuyen",
-        "http://www.htv.com.vn/truc-tuyen?channel=1",
-        "http://www.htv.com.vn/truc-tuyen?channel=456",
-        "https://htv.com.vn/truc-tuyen",
-        "https://www.htv.com.vn/truc-tuyen",
-        "https://www.htv.com.vn/truc-tuyen?channel=1",
-        "https://www.htv.com.vn/truc-tuyen?channel=456",
+    should_match_groups = [
+        ("https://htv.com.vn/truc-tuyen", {}),
+        ("https://htv.com.vn/truc-tuyen?channel=123", {"channel": "123"}),
+        ("https://htv.com.vn/truc-tuyen?channel=123&foo", {"channel": "123"}),
+        ("https://www.htv.com.vn/truc-tuyen", {}),
+        ("https://www.htv.com.vn/truc-tuyen?channel=123", {"channel": "123"}),
+        ("https://www.htv.com.vn/truc-tuyen?channel=123&foo", {"channel": "123"}),
     ]
     should_not_match = [
-        "http://htv.com.vn/",
-        "http://htv.com.vn/any/path",
-        "http://www.htv.com.vn/",
-        "http://www.htv.com.vn/any/path",
-        "http://www.htv.com.vn/truc-tuyen?channel=x",
-        "http://www.htv.com.vn/truc-tuyen?other=1",
         "https://htv.com.vn/",
         "https://htv.com.vn/any/path",
+        "https://htv.com.vn/truc-tuyen?foo",
         "https://www.htv.com.vn/",
         "https://www.htv.com.vn/any/path",
-        "https://www.htv.com.vn/truc-tuyen?channel=x",
-        "https://www.htv.com.vn/truc-tuyen?other=1",
+        "https://www.htv.com.vn/truc-tuyen?foo",
     ]

Read the original on github.com ↗