Skip to content

plugins.telemadrid: new plugin #5212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 2, 2023

Conversation

pablohoffman
Copy link
Contributor

Add plugin for telemadrid.es. Includes test.

Copy link
Member

@bastimeyer bastimeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

Adding a plugin for this site should be fine according to the plugin guidelines. It's always better to open an issue first though, to not risk your time being wasted on writing a new plugin that would not get accepted in the end.

In regards to the plugin code, the brightcove player is already implemented by the brightcove plugin, which provides the BrightcovePlayer class that can be used by other plugins, like bfmtv for example.

To make the code review a bit easier, here's the diff with a proper plugin implementation:

diff --git a/src/streamlink/plugins/telemadrid.py b/src/streamlink/plugins/telemadrid.py
index 9bd4028e..67b85ef4 100644
--- a/src/streamlink/plugins/telemadrid.py
+++ b/src/streamlink/plugins/telemadrid.py
@@ -1,60 +1,35 @@
 """
-$description Spanish live TV channel for Telemadrid
+$description Spanish live TV channel for Telemadrid, a public regional television station.
 $url telemadrid.es
 $type live, vod
 $region Spain
 """
 
-import logging
 import re
 
 from streamlink.plugin import Plugin, PluginError, pluginmatcher
 from streamlink.plugin.api import validate
-from streamlink.stream.hls import HLSStream
-
-
-log = logging.getLogger(__name__)
+from streamlink.plugins.brightcove import BrightcovePlayer
 
 
 @pluginmatcher(re.compile(
     r"https?://(?:www\.)?telemadrid\.es/",
 ))
 class Telemadrid(Plugin):
-    _API_URL = "https://edge.api.brightcove.com/playback/v1/accounts/{data_account}/videos/{data_video_id}"
-    _PLAYER_URL = "https://players.brightcove.net/{data_account}/{data_player}_default/index.min.js"
-
     def _get_streams(self):
         try:
             data = self.session.http.get(self.url, schema=validate.Schema(
                 validate.parse_html(),
-                validate.xml_find(".//video[@class='video-js']"),
+                validate.xml_find(".//video[@class='video-js'][@data-video-id][@data-account][@data-player][1]"),
                 validate.union_get("data-video-id", "data-account", "data-player"),
             ))
         except PluginError:
             return
-        data_video_id, data_account, data_player = data
-
-        url = self._PLAYER_URL.format(data_account=data_account, data_player=data_player)
-        policy_key = self.session.http.get(url, schema=validate.Schema(
-            re.compile(r"""options:\s*{.+policyKey:\s*"([^"]+)""", re.DOTALL),
-            validate.any(None, validate.get(1)),
-        ))
-        if not policy_key:
-            return
 
-        url = self._API_URL.format(data_account=data_account, data_video_id=data_video_id)
-
-        streams = self.session.http.get(
-            url,
-            headers={"Accept": f"application/json;pk={policy_key}"},
-            schema=validate.Schema(
-                validate.parse_json(),
-                validate.get("sources"),
-            ),
-        )
+        data_video_id, data_account, data_player = data
+        player = BrightcovePlayer(self.session, data_account, f"{data_player}_default")
 
-        for stream in streams:
-            return HLSStream.parse_variant_playlist(self.session, stream["src"])
+        return player.get_streams(data_video_id)
 
 
 __plugin__ = Telemadrid
diff --git a/tests/plugins/test_telemadrid.py b/tests/plugins/test_telemadrid.py
index 0aace017..894e3643 100644
--- a/tests/plugins/test_telemadrid.py
+++ b/tests/plugins/test_telemadrid.py
@@ -6,5 +6,6 @@ class TestPluginCanHandleUrlTelemadrid(PluginCanHandleUrl):
     __plugin__ = Telemadrid
 
     should_match = [
+        "https://www.telemadrid.es/",
         "https://www.telemadrid.es/emision-en-directo/",
     ]

Whether you want to catch the PluginError when no player data could be found is up to you, as it suppresses the detailed validation error message in that case. Suppressing the error should be fine though, as it's a very simple validation schema.

$ streamlink -l debug 'https://www.telemadrid.es/' best
[cli][debug] OS:         Linux-6.1.12-1-git-x86_64-with-glibc2.37
[cli][debug] Python:     3.11.1
[cli][debug] Streamlink: 5.3.1+8.ge204b717.dirty
[cli][debug] Dependencies:
[cli][debug]  certifi: 2022.12.7
[cli][debug]  isodate: 0.6.1
[cli][debug]  lxml: 4.9.2
[cli][debug]  pycountry: 22.3.5
[cli][debug]  pycryptodome: 3.17
[cli][debug]  PySocks: 1.7.1
[cli][debug]  requests: 2.28.2
[cli][debug]  urllib3: 1.26.14
[cli][debug]  websocket-client: 1.5.1
[cli][debug] Arguments:
[cli][debug]  url=https://www.telemadrid.es/
[cli][debug]  stream=['best']
[cli][debug]  --loglevel=debug
[cli][debug]  --player=mpv
[cli][info] Found matching plugin telemadrid for URL https://www.telemadrid.es/
[plugins.brightcove][debug] Creating player for account 104403117001 (player_id=Hknu0DaYb_default)
[plugins.brightcove][debug] Finding streams for video: 5829222728001
[plugins.brightcove][debug] Found policy key: BCpkADawqM2CeLSeLZFDp2Cn2VfHcuVQ3OWM5GcFOh5LTRwcu-IGhwwImdMlRU_6LwP0eDn-oHHYmBPh5HEh_TPoT8wNxhAt8elMkeWrjQ0KkOBdgtwNovkBnQ4
[utils.l10n][debug] Language code: en_US
[cli][info] Available streams: 180p (worst), 360p, 720p (best)
[cli][info] Opening stream: 720p (hls)
[cli][info] Starting player: mpv
...

@bastimeyer bastimeyer changed the title Add Telemadrid plugin plugins.telemadrid: new plugin Mar 2, 2023
@pablohoffman
Copy link
Contributor Author

Thanks for the quick review @bastimeyer, I've incorporated the changes (new commit on this branch).

@pablohoffman
Copy link
Contributor Author

Done @bastimeyer, I also decided to stop catching PluginError.

…ict() call, stop catching PluginError (for more detailed schema validation errors)
@bastimeyer bastimeyer merged commit 1b9ce00 into streamlink:master Mar 2, 2023
@bastimeyer
Copy link
Member

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants