Mercurial > libervia-backend
diff libervia/backend/plugins/plugin_misc_url_preview.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | eaa0daa7f834 |
children |
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_misc_url_preview.py Tue Jun 18 12:06:45 2024 +0200 +++ b/libervia/backend/plugins/plugin_misc_url_preview.py Wed Jun 19 18:44:57 2024 +0200 @@ -109,8 +109,7 @@ self.register("invidious", self.fetch_invidious_data, priority=-90) self.register_domain_protocol( - ["yewtu.be", "www.yewtu.be", "invidious.fdn.fr"], - "invidious" + ["yewtu.be", "www.yewtu.be", "invidious.fdn.fr"], "invidious" ) # bridge methods @@ -126,7 +125,9 @@ # API - def _url_preview_get(self, url: str, options: str, profile_key: str) -> defer.Deferred: + def _url_preview_get( + self, url: str, options: str, profile_key: str + ) -> defer.Deferred: client = self.host.get_client(profile_key) d = defer.ensureDeferred( self.get_preview_data(client, url, data_format.deserialise(options)) @@ -190,7 +191,6 @@ log.warning(f"Can't clean html data: {e}\n{preview_data}") del preview_data["html"] - return preview_data @classmethod @@ -305,15 +305,15 @@ """ oembed_url = f"https://www.youtube.com/oembed?url={parse.quote(url)}&format=json" data = await self._fetch_oembed_data(oembed_url) - if data is not None and 'html' in data: - html = data['html'] + if data is not None and "html" in data: + html = data["html"] root = etree.HTML(html) - iframe_elt = root.xpath('//iframe') + iframe_elt = root.xpath("//iframe") if iframe_elt: - iframe_elt[0].attrib['style'] = ( - 'position: absolute; top: 0; left: 0; width: 100%; height: 100%;' - ) - data['html'] = etree.tostring(root, method='html', encoding='unicode') + iframe_elt[0].attrib[ + "style" + ] = "position: absolute; top: 0; left: 0; width: 100%; height: 100%;" + data["html"] = etree.tostring(root, method="html", encoding="unicode") else: log.warning("No <iframe> found in the YouTube oEmbed response") @@ -347,7 +347,6 @@ f"Failed to fetch preview for {url}, status code: {resp.code}" ) - async def fetch_generic_data( self, client: SatXMPPEntity, url: str, options: dict ) -> Optional[dict]: @@ -430,9 +429,11 @@ "title": page.get("title"), "description": page.get("extract"), "url": url, - "image": page.get("thumbnail", {}).get("source") - if "thumbnail" in page - else None, + "image": ( + page.get("thumbnail", {}).get("source") + if "thumbnail" in page + else None + ), } else: raise PreviewFetchError( @@ -441,7 +442,9 @@ # Invidious - async def fetch_invidious_data(self, client: SatXMPPEntity, url: str, options: dict) -> Optional[dict]: + async def fetch_invidious_data( + self, client: SatXMPPEntity, url: str, options: dict + ) -> Optional[dict]: """ Fetch Invidious data from a url and generate HTML iframe. @@ -450,10 +453,10 @@ @return: A dictionary containing the Invidious data or None if no data could be fetched. """ parsed_url = parse.urlparse(url) - if 'watch' in parsed_url.path: - video_id = parse.parse_qs(parsed_url.query).get('v', [None])[0] + if "watch" in parsed_url.path: + video_id = parse.parse_qs(parsed_url.query).get("v", [None])[0] else: - video_id = parsed_url.path.strip('/') + video_id = parsed_url.path.strip("/") if not video_id: log.warning(f"Can't extract video ID from {url}") return None @@ -465,25 +468,25 @@ video_data = await resp.json() # construct the iframe html code html = ( - f'<iframe' + f"<iframe" f' width="100%"' f' height="auto"' f' src="https://{parsed_url.netloc}/embed/{video_id}"' f' frameborder="0" ' f' allow="' - f' accelerometer;' - f' autoplay;' - f' clipboard-write;' - f' encrypted-media;' - f' gyroscope;' + f" accelerometer;" + f" autoplay;" + f" clipboard-write;" + f" encrypted-media;" + f" gyroscope;" f' picture-in-picture"' f' style="' - f' position: absolute;' - f' top: 0;' - f' left: 0;' - f' width: 100%;' + f" position: absolute;" + f" top: 0;" + f" left: 0;" + f" width: 100%;" f' height: 100%;"' - f' allowfullscreen></iframe>' + f" allowfullscreen></iframe>" ) # structure the data to be returned data = {