Repository: streamlink/streamlink

Issue:
vlive: plugin is broken
<!--
Thanks for reporting a plugin issue!
USE THE TEMPLATE. Otherwise your plugin issue may be rejected.

First, see the contribution guidelines:
https://github.com/streamlink/streamlink/blob/master/CONTRIBUTING.md#contributing-to-streamlink

Also check the list of open and closed plugin issues:
https://github.com/streamlink/streamlink/issues?q=is%3Aissue+label%3A%22plugin+issue%22

Please see the text preview to avoid unnecessary formatting errors.
-->


## Plugin Issue

<!-- Replace [ ] with [x] in order to check the box -->
- [x] This is a plugin issue and I have read the contribution guidelines.


### Description

<!-- Explain the plugin issue as thoroughly as you can. -->
VLive.tv plugin is broken due to revising the site. The way to get parameters required for getting stream urls is changed now.

### 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. -->

Example: streamlink https://www.vlive.tv/video/221427 best


### Log output

<!--
TEXT LOG OUTPUT IS REQUIRED for a plugin issue!
Use the `--loglevel debug` parameter and avoid using parameters which suppress log output.
https://streamlink.github.io/cli.html#cmdoption-l

Make sure to **remove usernames and passwords**
You can copy the output to https://gist.github.com/ or paste it below.

Don't post screenshots of the log output and instead copy the text from your terminal application.
-->

```
streamlink -l debug https://www.vlive.tv/video/221427
[cli][debug] OS:         Windows 10
[cli][debug] Python:     3.6.6
[cli][debug] Streamlink: 1.7.0
[cli][debug] Requests(2.24.0), Socks(1.7.1), Websocket(0.57.0)
[cli][info] Found matching plugin vlive for URL https://www.vlive.tv/video/221427
error: Could not find video status information!
```


### Additional comments, etc.



[Love Streamlink? Please consider supporting our collective. Thanks!](https://opencollective.com/streamlink/donate)



Discussion:

Resolving patch:
diff --git a/src/streamlink/plugins/vlive.py b/src/streamlink/plugins/vlive.py
--- a/src/streamlink/plugins/vlive.py
+++ b/src/streamlink/plugins/vlive.py
@@ -1,58 +1,106 @@
-import json
 import logging
 import re
 
-from streamlink.plugin import Plugin, PluginError
+from streamlink.plugin import Plugin
+from streamlink.plugin.api import validate
 from streamlink.stream import HLSStream
+from streamlink.utils import parse_json
 
 log = logging.getLogger(__name__)
 
 
 class Vlive(Plugin):
-    _url_re = re.compile(r"https?://(?:www.)vlive\.tv/video/(\d+)")
-    _video_status = re.compile(r'oVideoStatus = (.+)<', re.DOTALL)
-    _video_init_url = "https://www.vlive.tv/video/init/view"
+    _url_re = re.compile(r"https://www\.vlive\.tv/(?P<format>video|post)/(?P<id>[0-9\-]+)")
+    _page_info = re.compile(r'window\.__PRELOADED_STATE__\s*=\s*({.*})\s*<', re.DOTALL)
+    _playinfo_url = "https://www.vlive.tv/globalv-web/vam-web/old/v3/live/{0}/playInfo"
+
+    _schema_video = validate.Schema(
+        validate.transform(_page_info.search),
+        validate.any(None, validate.all(
+            validate.get(1),
+            validate.transform(parse_json),
+            validate.any(validate.all(
+                {"postDetail": {"post": {"officialVideo": {
+                    "type": str,
+                    "videoSeq": int,
+                    validate.optional("status"): str,
+                }}}},
+                validate.get("postDetail"),
+                validate.get("post"),
+                validate.get("officialVideo")),
+                validate.all(
+                    {"postDetail": {"error": {
+                        "errorCode": str,
+                    }}},
+                    validate.get("postDetail"),
+                    validate.get("error")))
+        ))
+    )
+
+    _schema_stream = validate.Schema(
+        validate.transform(parse_json),
+        validate.all(
+            {"result": {"streamList": [{
+                "streamName": str,
+                "serviceUrl": str,
+            }]}},
+            validate.get("result"),
+            validate.get("streamList")
+        )
+    )
 
     @classmethod
     def can_handle_url(cls, url):
         return cls._url_re.match(url) is not None
 
-    @property
-    def video_id(self):
-        return self._url_re.match(self.url).group(1)
-
     def _get_streams(self):
-        vinit_req = self.session.http.get(self._video_init_url,
-                                          params=dict(videoSeq=self.video_id),
-                                          headers=dict(referer=self.url))
-        if vinit_req.status_code != 200:
-            raise PluginError('Could not get video init page (HTTP Status {})'
-                              .format(vinit_req.status_code))
-
-        video_status_js = self._video_status.search(vinit_req.text)
-        if not video_status_js:
-            raise PluginError('Could not find video status information!')
-
-        video_status = json.loads(video_status_js.group(1))
-
-        if video_status['viewType'] == 'vod':
-            raise PluginError('VODs are not supported')
+        video_json = self.session.http.get(self.url, headers={"Referer": self.url},
+                                           schema=self._schema_video)
+        if video_json is None:
+            log.error('Could not parse video page')
+            return
+
+        err = video_json.get('errorCode')
+        if err == 'common_700':
+            log.error('Available only to members of the channel')
+            return
+        elif err == 'common_702':
+            log.error('Vlive+ VODs are not supported')
+            return
+        elif err == 'common_404':
+            log.error('Could not find video page')
+            return
+        elif err is not None:
+            log.error('Unknown error code: {0}'.format(err))
+            return
+
+        if video_json['type'] == 'VOD':
+            log.error('VODs are not supported')
+            return
+
+        url_format, video_id = self._url_re.match(self.url).groups()
+        if url_format == 'post':
+            video_id = str(video_json['videoSeq'])
+
+        video_status = video_json.get('status')
+        if video_status == 'ENDED':
+            log.error('Stream has ended')
+            return
+        elif video_status != 'ON_AIR':
+            log.error('Unknown video status: {0}'.format(video_status))
+            return
+
+        stream_info = self.session.http.get(self._playinfo_url.format(video_id),
+                                            headers={"Referer": self.url},
+                                            schema=self._schema_stream)
 
-        if 'liveStreamInfo' not in video_status:
-            raise PluginError('Stream is offline')
-
-        stream_info = json.loads(video_status['liveStreamInfo'])
-
-        streams = dict()
         # All "resolutions" have a variant playlist with only one entry, so just combine them
-        for i in stream_info['resolutions']:
-            res_streams = HLSStream.parse_variant_playlist(self.session, i['cdnUrl'])
+        for i in stream_info:
+            res_streams = HLSStream.parse_variant_playlist(self.session, i['serviceUrl'])
             if len(res_streams.values()) > 1:
                 log.warning('More than one stream in variant playlist, using first entry!')
 
-            streams[i['name']] = res_streams.popitem()[1]
-
-        return streams
+            yield i['streamName'], res_streams.popitem()[1]
 
 
 __plugin__ = Vlive


Test patch:
diff --git a/tests/plugins/test_vlive.py b/tests/plugins/test_vlive.py
--- a/tests/plugins/test_vlive.py
+++ b/tests/plugins/test_vlive.py
@@ -8,6 +8,7 @@
 class TestPluginVlive:
     valid_urls = [
         ("https://www.vlive.tv/video/156824",),
+        ("https://www.vlive.tv/post/0-19740901",)
     ]
     invalid_urls = [
         ("https://www.vlive.tv/events/2019vheartbeat?lang=en",),