Reformatted code base using black.
This commit is contained in:
parent
56319cbad3
commit
53fdaa9d09
1 changed files with 12604 additions and 8966 deletions
|
|
@ -109,8 +109,7 @@ class Preview:
|
|||
|
||||
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 @@ class Preview:
|
|||
|
||||
# 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 @@ class Preview:
|
|||
log.warning(f"Can't clean html data: {e}\n{preview_data}")
|
||||
del preview_data["html"]
|
||||
|
||||
|
||||
return preview_data
|
||||
|
||||
@classmethod
|
||||
|
|
@ -305,15 +305,15 @@ class Preview:
|
|||
"""
|
||||
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 @@ class Preview:
|
|||
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 @@ class Preview:
|
|||
"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 @@ class Preview:
|
|||
|
||||
# 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 @@ class Preview:
|
|||
@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 @@ class Preview:
|
|||
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 = {
|
||||
|
|
|
|||
Loading…
Reference in a new issue