from __future__ import print_function
import re

from streamlink.plugin import Plugin
from streamlink.plugin.api import useragents
from streamlink.plugin.api import validate
from streamlink.stream import HLSStream


class RaiPlay(Plugin):
    url_re = re.compile(r"https?://(?:www\.)?raiplay\.it/dirette/(\w+)/?")
    stream_re = re.compile(r"data-video-url.*?=.*?\"([^\"]+)\"")
    stream_schema = validate.Schema(
        validate.all(
            validate.transform(stream_re.search),
            validate.any(
                None,
                validate.all(validate.get(1), validate.url())
            )
        )
    )

    @classmethod
    def can_handle_url(cls, url):
        return cls.url_re.match(url) is not None

    def _get_streams(self):
        channel = self.url_re.match(self.url).group(1)
        self.logger.debug("Found channel: {0}", channel)
        stream_url = self.session.http.get(self.url, schema=self.stream_schema)
        if stream_url:
            return HLSStream.parse_variant_playlist(self.session, stream_url, headers={"User-Agent": useragents.CHROME})


__plugin__ = RaiPlay

@@ -1,41 +1,18 @@
-from __future__ import print_function
+import logging
 %0Aimp
@@ -39,22 +39,22 @@
 ink.
-plugin
+compat
  import 
 Plug
@@ -49,22 +49,36 @@
  import 
-Plugin
+urlparse, urlunparse
 %0Afrom st
@@ -92,28 +92,24 @@
 k.plugin
-.api
  import 
 useragen
@@ -100,26 +100,22 @@
  import 
-useragents
+Plugin
 %0Afrom st
@@ -190,16 +190,92 @@
 SStream%0A
+from streamlink.utils import parse_json%0A%0A%0Alog = logging.getLogger(__name__)%0A
 %0A%0Aclass 
@@ -295,22 +295,23 @@
 n):%0A    
-url
 _re
+_url
  = re.co
@@ -371,21 +371,21 @@
 ?%22)%0A
+%0A
     
-stream_re
+_re_data
  = r
@@ -411,18 +411,19 @@
 deo-
-url.*?=.*?
+json%5Cs*=%5Cs*
 %5C%22(%5B
@@ -437,29 +437,28 @@
 %22%22)%0A    
-stream
 _schema
+_data
  = valid
@@ -473,34 +473,8 @@
 ma(%0A
-        validate.all(%0A    
     
@@ -500,17 +500,16 @@
 orm(
-stream_re
+_re_data
 .sea
@@ -514,36 +514,32 @@
 earch),%0A        
-  
-  
 validate.any(%0A  
@@ -539,115 +539,202 @@
 any(
-%0A                None,%0A     
+None, validate.get(1))%0A
     
+)%0A
     
-   validate.all(validate.get(1), validate.url())%0A            )%0A        
+_schema_json = validate.Schema(%0A        validate.transform(parse_json),%0A        validate.get(%22video%22),%0A        validate.get(%22content_url%22),%0A        validate.url(
 )%0A  
@@ -808,22 +808,23 @@
 urn cls.
-url
 _re
+_url
 .match(u
@@ -876,22 +876,23 @@
         
-channe
+json_ur
 l = self
@@ -896,96 +896,238 @@
 elf.
-url_re.match(self.url).group(1)%0A        self.logger.debug(%22Found channel: %7B0%7D%22, channel)
+session.http.get(self.url, schema=self._schema_data)%0A        if not json_url:%0A            return%0A%0A        json_url = urlunparse(urlparse(self.url)._replace(path=json_url))%0A        log.debug(%22Found JSON URL: %7B0%7D%22.format(json_url))%0A
 %0A   
@@ -1158,37 +1158,37 @@
 ession.http.get(
-self.
+json_
 url, schema=self
@@ -1192,63 +1192,408 @@
 elf.
-stream_schema)%0A        if stream_url:%0A            retur
+_schema_json)%0A        log.debug(%22Found stream URL: %7B0%7D%22.format(stream_url))%0A%0A        res = self.session.http.request(%22HEAD%22, stream_url)%0A        # status code will be 200 even if geo-blocked, so check the returned content-type%0A        if not res or not res.headers or res.headers%5B%22Content-Type%22%5D == %22video/mp4%22:%0A            log.error(%22Geo-restricted content%22)%0A            return%0A%0A        for stream i
 n HL
@@ -1651,52 +1651,43 @@
 _url
-, headers=%7B%22User-Agent%22: useragents.CHROME%7D)
+).items():%0A            yield stream
 %0A%0A%0A_

rewrite and fix plugin (#3147)