"""
$description Live TV channels from RAI, an Italian public, state-owned broadcaster.
$url raiplay.it
$type live
$region Italy
"""

import logging
import re
from urllib.parse import urlparse, urlunparse

from streamlink.plugin import Plugin, pluginmatcher
from streamlink.plugin.api import validate
from streamlink.stream.hls import HLSStream

log = logging.getLogger(__name__)


@pluginmatcher(re.compile(
    r"https?://(?:www\.)?raiplay\.it/dirette/(\w+)/?"
))
class RaiPlay(Plugin):
    def _get_streams(self):
        json_url = self.session.http.get(self.url, schema=validate.Schema(
            validate.parse_html(),
            validate.xml_xpath_string(".//*[@data-video-json][1]/@data-video-json"),
        ))
        if not json_url:
            return

        json_url = urlunparse(urlparse(self.url)._replace(path=json_url))
        log.debug(f"Found JSON URL: {json_url}")

        stream_url = self.session.http.get(json_url, schema=validate.Schema(
            validate.parse_json(),
            {"video": {"content_url": validate.url()}},
            validate.get(("video", "content_url")),
        ))
        log.debug(f"Found stream URL: {stream_url}")

        res = self.session.http.request("HEAD", stream_url)
        # status code will be 200 even if geo-blocked, so check the returned content-type
        if not res or not res.headers or res.headers["Content-Type"] == "video/mp4":
            log.error("Geo-restricted content")
            return

        yield from HLSStream.parse_variant_playlist(self.session, stream_url).items()


__plugin__ = RaiPlay

@@ -176,16 +176,27 @@
 e import
+ parse_qsl,
  urlpars
@@ -348,16 +348,60 @@
 LSStream
+%0Afrom streamlink.utils.url import update_qsd
 %0A%0Alog = 
@@ -537,16 +537,55 @@
 lugin):%0A
+    _DEFAULT_MEDIAPOLIS_OUTPUT = %2264%22%0A%0A
     def 
@@ -987,22 +987,23 @@
         
-stream
+content
 _url = s
@@ -1223,51 +1223,469 @@
     
-log.debug(f%22Found stream URL: %7Bstream_url%7D%22
+if not content_url:%0A            log.error(%22Missing content URL%22)%0A            return%0A%0A        content_url = content_url.replace(%22/relinkerServlet.mp4%22, %22/relinkerServlet.htm%22)%0A        parsed = urlparse(content_url)%0A        params = dict(parse_qsl(parsed.query))%0A        if not parsed.path.endswith(%22.xml%22) and not params.get(%22output%22):%0A            params%5B%22output%22%5D = self._DEFAULT_MEDIAPOLIS_OUTPUT%0A            content_url = update_qsd(urlunparse(parsed), params
 )%0A%0A 
@@ -1719,30 +1719,20 @@
 ttp.
-request(%22HEAD%22, stream
+head(content
 _url
@@ -1968,32 +1968,598 @@
         return%0A%0A
+        stream_url = self.session.http.get(%0A            content_url,%0A            schema=validate.Schema(%0A                validate.parse_xml(),%0A                validate.xml_element(tag=%22Mediapolis%22),%0A                validate.xml_xpath_string(%22./url%5B@type='content'%5D/text()%22),%0A                validate.none_or_all(%0A                    validate.transform(str.strip),%0A                    validate.url(path=validate.endswith(%22.m3u8%22)),%0A                ),%0A            ),%0A        )%0A        if not stream_url:%0A            log.error(%22Missing stream URL%22)%0A            return%0A%0A
         yield fr

update and fix plugin