Repository: streamlink/streamlink

Issue:
metube.id plugin request
## Plugin Request
- [x] This is a plugin request and I have read the contribution guidelines and plugin request requirements.

### Description
www.metube.id
- serveral Indonesia TV channels / legal streaming portal, see TERMS & CONDITIONS
link to live channel selection:
- https://www.metube.id/live

### Example stream URLs
1.  https://www.metube.id/live/METROTV
2.  https://www.metube.id/live/GTV
3.  https://www.metube.id/live/RCTI
4.  https://www.metube.id/live/TRANS7
5. ...

### Additional comments, screenshots, etc.
sample embeded stream: m3u8 / accessing: HTTP 502 bad Gateway

1. https://cdn-livetv1.metube.id/hls/metrotv.m3u8  / 
    https://cdn-livetv1.metube.id/hls/metrotv_480/index.m3u8
2. https://cdn-livetv2.metube.id/hls/globaltv.m3u8 / 
    https://cdn-livetv2.metube.id/hls/globaltv_480/index.m3u8
3. https://cdn-livetv2.metube.id/hls/rcti.m3u8
    https://cdn-livetv2.metube.id/hls/rcti_480/index.m3u8
4. https://cdn-livetv1.metube.id/hls/trans7.m3u8
    https://cdn-livetv1.metube.id/hls/trans7_480/index.m3u8
5. .....

######




Discussion:
I think there is some indonesian want plugin for this site, Hope someone could help us to make plugin for us. High demand please consider it. Thank You So Much.
@akiwatti 

> HTTP 502 bad Gateway

if you send a **User-Agent** and **Origin** http header, it should work

```
streamlink https://cdn-livetv1.metube.id/hls/metrotv.m3u8 best --http-header "Origin=https://www.metube.id" --http-header "User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"
```

Resolving patch:
diff --git a/src/streamlink/plugins/metube.py b/src/streamlink/plugins/metube.py
new file mode 100644
--- /dev/null
+++ b/src/streamlink/plugins/metube.py
@@ -0,0 +1,58 @@
+import re
+
+from streamlink.plugin import Plugin
+from streamlink.plugin.api import useragents
+from streamlink.stream import HLSStream
+
+
+class MeTube(Plugin):
+
+    _url_re = re.compile(r"""https?://(?:www\.)?metube\.id/
+                         (?P<type>live|videos)/\w+(?:/.*)?""", re.VERBOSE)
+
+    # extracted from webpage source
+    _VOD_STREAM_NAMES = {
+        "3000k": "1080p",
+        "1800k": "720p",
+        "800k": "480p",
+        "300k": "240p"
+    }
+
+    @classmethod
+    def can_handle_url(cls, url):
+        return cls._url_re.match(url) is not None
+
+    def _get_streams(self):
+        stream_type = self._url_re.match(self.url).group("type")
+        hls_re = re.compile(r"""["'](?P<url>[^"']+\.m3u8[^"']*?)["']""")
+
+        headers = {
+            "Origin": "https://www.metube.id",
+            "User-Agent": useragents.FIREFOX
+        }
+
+        res = self.session.http.get(self.url)
+        match = hls_re.search(res.text)
+
+        if not match:
+            return
+
+        stream_url = match.group("url")
+
+        if stream_type == "live":
+            return HLSStream.parse_variant_playlist(self.session, stream_url,
+                                                    headers=headers)
+        else:
+            streams = {}
+
+            for quality, stream in HLSStream.parse_variant_playlist(
+                                   self.session,
+                                   stream_url,
+                                   headers=headers).items():
+                    name = self._VOD_STREAM_NAMES.get(quality, quality)
+                    streams[name] = stream
+
+            return streams
+
+
+__plugin__ = MeTube


Test patch:
diff --git a/tests/plugins/test_metube.py b/tests/plugins/test_metube.py
new file mode 100644
--- /dev/null
+++ b/tests/plugins/test_metube.py
@@ -0,0 +1,24 @@
+import unittest
+
+from streamlink.plugins.metube import MeTube
+
+
+class TestPluginMeTube(unittest.TestCase):
+    def test_can_handle_url(self):
+        should_match = [
+            'https://www.metube.id/live/METROTV',
+            'https://www.metube.id/live/GTV',
+            'https://www.metube.id/videos/16881738/yudi_28_bogor_-amazingakak',
+            'https://www.metube.id/videos/16873428/liverpool-vs-psg-3-2-goals-and-highlights-2018',
+        ]
+        for url in should_match:
+            self.assertTrue(MeTube.can_handle_url(url))
+
+    def test_can_handle_url_negative(self):
+        should_not_match = [
+            'https://www.metube.id/me/IMAA2018',
+            'https://www.metube.id/auditions',
+            'https://www.twitch.tv/twitch'
+        ]
+        for url in should_not_match:
+            self.assertFalse(MeTube.can_handle_url(url))