Repository: streamlink/streamlink
Pull request #2204 (2018-12-19 12:00:26)

plugins.afreeca: use Referer for every connection
closes https://github.com/streamlink/streamlink/issues/2203

codecov[bot]: # [Codecov](https://codecov.io/gh/streamlink/streamlink/pull/2204?src=pr&el=h1) Report
> Merging [#2204](https://codecov.io/gh/streamlink/streamlink/pull/2204?src=pr&el=desc) into [master](https://codecov.io/gh/streamlink/streamlink/commit/3946184ab4a9bf9dbc9f9810e27beed44f99791f?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `0%`.


```diff
@@           Coverage Diff           @@
##           master    #2204   +/-   ##
=======================================
  Coverage   52.36%   52.36%           
=======================================
  Files         237      237           
  Lines       14712    14712           
=======================================
  Hits         7704     7704           
  Misses       7008     7008
```

gravyboat: Looks good, thanks @back-to.

Patch:
diff --git a/docs/plugin_matrix.rst b/docs/plugin_matrix.rst
index d4f25cb9..6bd754a1 100644
--- a/docs/plugin_matrix.rst
+++ b/docs/plugin_matrix.rst
@@ -15,8 +15,7 @@ Name                    URL(s)               Live  VOD   Notes
 abematv                 abema.tv             Yes   Yes   Streams are geo-restricted to Japan.
 abweb                   abweb.com            Yes   No    Requires a login and a subscription.
 adultswim               adultswim.com        Yes   Yes   Streams may be geo-restricted, some VOD streams are protected by DRM.
-afreeca                 - afreecatv.com      Yes   No
-                        - afreeca.tv
+afreeca                 play.afreecatv.com   Yes   No
 aljazeeraen             aljazeera.com        Yes   Yes   English version of the site.
 animelab                animelab.com         --    Yes   Requires a login. Streams may be geo-restricted to Australia and New Zealand.
 app17                   17app.co             Yes   --
diff --git a/src/streamlink/plugins/afreeca.py b/src/streamlink/plugins/afreeca.py
index a9fec3f6..319bc4f4 100644
--- a/src/streamlink/plugins/afreeca.py
+++ b/src/streamlink/plugins/afreeca.py
@@ -19,7 +19,7 @@ QUALITY_WEIGHTS = {
     "sd": 480
 }
 
-_url_re = re.compile(r"http(s)?://(?P<cdn>\w+\.)?afreeca(tv)?\.com/(?P<username>\w+)(/\d+)?")
+_url_re = re.compile(r"https?://play\.afreecatv\.com/(?P<username>\w+)(?:/\d+)?")
 
 _channel_schema = validate.Schema(
     {
@@ -86,10 +86,6 @@ class AfreecaTV(Plugin):
         return self.session.http.json(res, schema=_channel_schema)
 
     def _get_hls_key(self, broadcast, username, quality):
-        headers = {
-            "Referer": self.url
-        }
-
         data = {
             "bid": username,
             "bno": broadcast,
@@ -97,7 +93,7 @@ class AfreecaTV(Plugin):
             "quality": quality,
             "type": "pwd"
         }
-        res = self.session.http.post(CHANNEL_API_URL, data=data, headers=headers)
+        res = self.session.http.post(CHANNEL_API_URL, data=data)
         return self.session.http.json(res, schema=_channel_schema)
 
     def _get_stream_info(self, broadcast, quality, cdn, rmd):
@@ -139,6 +135,7 @@ class AfreecaTV(Plugin):
             return False
 
     def _get_streams(self):
+        self.session.http.headers.update({"Referer": self.url})
         if not self.session.get_option("hls-segment-ignore-names"):
             ignore_segment = ["preloading"]
             self.session.set_option("hls-segment-ignore-names", ignore_segment)


Test patch:
diff --git a/tests/plugins/test_afreeca.py b/tests/plugins/test_afreeca.py
index fffc7cab..ab7b98d6 100644
--- a/tests/plugins/test_afreeca.py
+++ b/tests/plugins/test_afreeca.py
@@ -6,15 +6,18 @@ from streamlink.plugins.afreeca import AfreecaTV
 class TestPluginAfreecaTV(unittest.TestCase):
     def test_can_handle_url(self):
         should_match = [
-            "http://afreecatv.com/exampleuser",
-            "http://afreecatv.com/exampleuser/123123123",
             "http://play.afreecatv.com/exampleuser",
             "http://play.afreecatv.com/exampleuser/123123123",
+            "https://play.afreecatv.com/exampleuser",
         ]
         for url in should_match:
             self.assertTrue(AfreecaTV.can_handle_url(url))
 
         should_not_match = [
+            "http://afreeca.com/exampleuser",
+            "http://afreeca.com/exampleuser/123123123",
+            "http://afreecatv.com/exampleuser",
+            "http://afreecatv.com/exampleuser/123123123",
             "http://www.afreecatv.com.tw",
             "http://www.afreecatv.jp",
         ]