From b24f2f51cbfe0f011621d10edb8c9d4e402df75c Mon Sep 17 00:00:00 2001 From: bastimeyer Date: Wed, 1 Sep 2021 15:30:28 +0200 Subject: [PATCH] plugins.euronews: rewrite and fix using XPath --- src/streamlink/plugins/euronews.py | 39 ++++++++++++++---------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/streamlink/plugins/euronews.py b/src/streamlink/plugins/euronews.py index f092bed12f7..5a6c645767a 100644 --- a/src/streamlink/plugins/euronews.py +++ b/src/streamlink/plugins/euronews.py @@ -3,9 +3,8 @@ from streamlink.plugin import Plugin, pluginmatcher from streamlink.plugin.api import validate -from streamlink.plugin.api.utils import itertags from streamlink.stream import HLSStream, HTTPStream -from streamlink.utils import parse_json, update_scheme +from streamlink.utils import update_scheme @pluginmatcher(re.compile( @@ -15,41 +14,39 @@ class Euronews(Plugin): API_URL = "https://{subdomain}.euronews.com/api/watchlive.json" def _get_vod_stream(self): - def find_video_url(content): - for elem in itertags(content, "meta"): - if elem.attributes.get("property") == "og:video": - return elem.attributes.get("content") - - video_url = self.session.http.get(self.url, schema=validate.Schema( - validate.transform(find_video_url), - validate.any(None, validate.url()) + root = self.session.http.get(self.url, schema=validate.Schema( + validate.parse_html() )) - if video_url is not None: + video_url = root.xpath("string(.//meta[@property='og:video'][1]/@content)") + if video_url: return dict(vod=HTTPStream(self.session, video_url)) - def _get_live_streams(self): - def find_video_id(content): - for elem in itertags(content, "div"): - if elem.attributes.get("id") == "pfpPlayer" and elem.attributes.get("data-google-src") is not None: - return elem.attributes.get("data-video-id") + video_id = root.xpath("string(.//div[@data-google-src]/@data-video-id)") + if video_id: + return self.session.streams(f"https://www.youtube.com/watch?v={video_id}") + video_url = root.xpath("string(.//iframe[@id='pfpPlayer'][starts-with(@src,'https://www.youtube.com/')][1]/@src)") + if video_url: + return self.session.streams(video_url) + + def _get_live_streams(self): video_id = self.session.http.get(self.url, schema=validate.Schema( - validate.transform(find_video_id), - validate.any(None, str) + validate.parse_html(), + validate.xml_xpath_string(".//div[@data-google-src]/@data-video-id") )) - if video_id is not None: + if video_id: return self.session.streams(f"https://www.youtube.com/watch?v={video_id}") info_url = self.session.http.get(self.API_URL.format(subdomain=self.match.group("subdomain")), schema=validate.Schema( - validate.transform(parse_json), + validate.parse_json(), {"url": validate.url()}, validate.get("url"), validate.transform(lambda url: update_scheme("https://", url)) )) hls_url = self.session.http.get(info_url, schema=validate.Schema( - validate.transform(parse_json), + validate.parse_json(), { "status": "ok", "protocol": "hls",