Repository: streamlink/streamlink

Issue:
dogus.py KRAL TV Plugin error: Unable to open URL:
## Bug Report
- [x] This is a bug report and I have read the contribution guidelines.

### Description dogus.py

<!-- What do you expect to happen, and what is actually happening? -->

Normally the picture and the sound should come, none of it will come

### Reproduction steps / Explicit stream URLs to test

<!-- How can we reproduce this? Please note the exact steps below using the list format supplied. If you need more steps please add them. -->

1. https://www.kralmuzik.com.tr/tv/kral-pop-tv

### Log output
                                                                       
streamlink http://www.kralmuzik.com.tr/tv/kral-pop-tv best
[cli][info] Found matching plugin dogus for URL http://www.kralmuzik.com.tr/tv/kral-pop-tv
error: Unable to open URL: https://dygvideo.dygdigital.com/live/hls/kralpop?token= :  (403 Client Error: Forbidden for url: https://dygvideo.dygdigital.com/live/hls/kralpop?token=%20:%20)                                                                                                                                                          



Discussion:

Resolving patch:
diff --git a/src/streamlink/plugins/dogus.py b/src/streamlink/plugins/dogus.py
--- a/src/streamlink/plugins/dogus.py
+++ b/src/streamlink/plugins/dogus.py
@@ -1,9 +1,14 @@
 import re
+import logging
 
 from streamlink.plugin import Plugin
+from streamlink.plugin.api.utils import itertags
+from streamlink.plugins.youtube import YouTube
 from streamlink.stream import HLSStream
 from streamlink.utils import update_scheme
 
+log = logging.getLogger(__name__)
+
 
 class Dogus(Plugin):
     """
@@ -14,13 +19,13 @@ class Dogus(Plugin):
         (?:
             ntv.com.tr/canli-yayin/ntv|
             ntvspor.net/canli-yayin|
-            kralmuzik.com.tr/tv/kral-tv|
-            kralmuzik.com.tr/tv/kral-pop-tv|
+            kralmuzik.com.tr/tv/|
             eurostartv.com.tr/canli-izle
         )/?""", re.VERBOSE)
     mobile_url_re = re.compile(r"""(?P<q>["'])(?P<url>(https?:)?//[^'"]*?/live/hls/[^'"]*?\?token=)
                                    (?P<token>[^'"]*?)(?P=q)""", re.VERBOSE)
     token_re = re.compile(r"""token=(?P<q>["'])(?P<token>[^'"]*?)(?P=q)""")
+    kral_token_url = "https://service.kralmuzik.com.tr/version/gettoken"
 
     @classmethod
     def can_handle_url(cls, url):
@@ -28,18 +33,31 @@ def can_handle_url(cls, url):
 
     def _get_streams(self):
         res = self.session.http.get(self.url)
-        mobile_url_m = self.mobile_url_re.search(res.text)
-
-        mobile_url = mobile_url_m and update_scheme(self.url, mobile_url_m.group("url"))
 
-        token = mobile_url_m and mobile_url_m.group("token")
-        if not token:
-            # if no token is in the url, try to find it else where in the page
-            token_m = self.token_re.search(res.text)
-            token = token_m and token_m.group("token")
+        # Look for Youtube embedded video first
+        for iframe in itertags(res.text, 'iframe'):
+            if YouTube.can_handle_url(iframe.attributes.get("src")):
+                log.debug("Handing off to YouTube plugin")
+                return self.session.streams(iframe.attributes.get("src"))
 
-        return HLSStream.parse_variant_playlist(self.session, mobile_url + token,
-                                                headers={"Referer": self.url})
+        # Next check for HLS URL with token
+        mobile_url_m = self.mobile_url_re.search(res.text)
+        mobile_url = mobile_url_m and update_scheme(self.url, mobile_url_m.group("url"))
+        if mobile_url:
+            log.debug("Found mobile stream: {0}".format(mobile_url_m.group(0)))
+
+            token = mobile_url_m and mobile_url_m.group("token")
+            if not token and "kralmuzik" in self.url:
+                log.debug("Getting Kral Muzik HLS stream token from API")
+                token = self.session.http.get(self.kral_token_url).text
+            elif not token:
+                # if no token is in the url, try to find it else where in the page
+                log.debug("Searching for HLS stream token in URL")
+                token_m = self.token_re.search(res.text)
+                token = token_m and token_m.group("token")
+
+            return HLSStream.parse_variant_playlist(self.session, mobile_url + token,
+                                                    headers={"Referer": self.url})
 
 
 __plugin__ = Dogus


Test patch: