Test cat images
This commit is contained in:
commit
4fd3d49af1
3 changed files with 131 additions and 0 deletions
39
urlpreview.py
Normal file
39
urlpreview.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
import aiohttp
|
||||
import html.parser
|
||||
import asyncio
|
||||
|
||||
class TitleExtractor(html.parser.HTMLParser):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.current_data = None
|
||||
self.latest_title = None
|
||||
|
||||
def handle_data(self, data):
|
||||
self.current_data = data
|
||||
|
||||
def handle_endtag(self, tag):
|
||||
if tag == "title":
|
||||
self.latest_title = self.current_data
|
||||
|
||||
async def get_preview(url):
|
||||
session = aiohttp.ClientSession()
|
||||
session.headers["user-agent"] = "Mozilla/5.0 XmppPreviewer/1.0"
|
||||
resp = await session.get(url)
|
||||
title = None
|
||||
print(url, resp.status)
|
||||
if resp.headers["content-type"].startswith("text/html"):
|
||||
parser = TitleExtractor()
|
||||
while chunk := await resp.content.read(4096):
|
||||
parser.feed(chunk.decode("utf-8")) # assume utf-8
|
||||
if parser.latest_title:
|
||||
title = parser.latest_title
|
||||
break
|
||||
resp.close()
|
||||
await session.close()
|
||||
return title
|
||||
|
||||
if __name__ == "__main__":
|
||||
async def main():
|
||||
print(await get_preview("https://youtu.be/YUMaoIt1rDU"))
|
||||
|
||||
asyncio.run(main())
|
Loading…
Add table
Add a link
Reference in a new issue