diff --git a/urlpreview.py b/urlpreview.py index 50cb0bb..e242630 100644 --- a/urlpreview.py +++ b/urlpreview.py @@ -1,6 +1,7 @@ import aiohttp import html.parser import asyncio +import urllib.parse class TitleExtractor(html.parser.HTMLParser): def __init__(self): @@ -16,11 +17,14 @@ class TitleExtractor(html.parser.HTMLParser): self.latest_title = self.current_data async def get_preview(url): + url = url.replace("youtube.com/shorts/", "youtube.com/watch?v=") + url_parsed = urllib.parse.urlparse(url) + url_no_fragment = url_parsed._replace(fragment="").geturl() session = aiohttp.ClientSession() session.headers["user-agent"] = "Mozilla/5.0 XmppPreviewer/1.0" - resp = await session.get(url) + resp = await session.get(url_no_fragment) title = None - print(url, resp.status) + print(url_no_fragment, resp.status) if resp.headers["content-type"].startswith("text/html"): parser = TitleExtractor() while chunk := await resp.content.read(4096): @@ -34,6 +38,6 @@ async def get_preview(url): if __name__ == "__main__": async def main(): - print(await get_preview("https://youtu.be/YUMaoIt1rDU")) + print(await get_preview("https://youtu.be/WNFahAioGP8")) asyncio.run(main())