61 lines
2.5 KiB
Python
61 lines
2.5 KiB
Python
"""Link previews: a page with OpenGraph tags and a direct picture link."""
|
|
import asyncio, functools, http.server, os, shutil, tempfile, threading
|
|
from playwright.async_api import async_playwright
|
|
from browser import login, BASE, OUT
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
PORT = 8765
|
|
PAGE = """<!doctype html><html><head>
|
|
<title>Fallback title</title>
|
|
<meta property="og:title" content="A very interesting article">
|
|
<meta property="og:description" content="Everything you always wanted to know about link previews.">
|
|
<meta property="og:image" content="/test.png">
|
|
<meta property="og:site_name" content="Example News">
|
|
<link rel="icon" href="/test.png">
|
|
</head><body>Hello</body></html>"""
|
|
|
|
|
|
def serve():
|
|
root = tempfile.mkdtemp()
|
|
with open(os.path.join(root, "article.html"), "w") as f:
|
|
f.write(PAGE)
|
|
shutil.copy(os.path.join(HERE, "test.png"), os.path.join(root, "test.png"))
|
|
handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory=root)
|
|
server = http.server.ThreadingHTTPServer(("127.0.0.1", PORT), handler)
|
|
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
return server
|
|
|
|
|
|
async def main():
|
|
server = serve()
|
|
async with async_playwright() as p:
|
|
browser = await p.chromium.launch()
|
|
_, alice, aerr = await login(browser, "alice@localhost", "alicepass", "previewalice")
|
|
_, bob, berr = await login(browser, "bob@localhost", "bobpass", "previewbob")
|
|
|
|
await alice.goto(BASE + "/chat/bob@localhost")
|
|
await alice.wait_for_selector("#chat_textarea", timeout=15000)
|
|
await bob.goto(BASE + "/chat/alice@localhost")
|
|
await bob.wait_for_selector("#chat_textarea", timeout=15000)
|
|
await bob.wait_for_timeout(1500)
|
|
|
|
for text in (f"Have a look http://127.0.0.1:{PORT}/article.html", f"http://127.0.0.1:{PORT}/test.png"):
|
|
await bob.fill("#chat_textarea", text)
|
|
await bob.press("#chat_textarea", "Enter")
|
|
await bob.wait_for_timeout(1500)
|
|
await alice.wait_for_timeout(4000)
|
|
|
|
for name, page in (("alice", alice), ("bob", bob)):
|
|
await page.screenshot(path=f"{OUT}/preview_{name}.png")
|
|
cards = await page.locator("#chat_widget ul.list.card").all_inner_texts()
|
|
pictures = await page.locator("#chat_widget div.bubble img").count()
|
|
print(name, "cards:", [c.replace("\n", " | ")[:120] for c in cards[-2:]], "pictures:", pictures)
|
|
|
|
for e in aerr + berr:
|
|
print(e)
|
|
await browser.close()
|
|
server.shutdown()
|
|
|
|
|
|
asyncio.run(main())
|