Skip to content

Instantly share code, notes, and snippets.

@attakei
Last active February 20, 2024 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save attakei/d4b9974d06dadf41a04e914ac2c14594 to your computer and use it in GitHub Desktop.
Save attakei/d4b9974d06dadf41a04e914ac2c14594 to your computer and use it in GitHub Desktop.
shortcut functions to send post using atproto
atproto
beautifulsoup4
import httpx
from atproto import models
from bs4 import BeautifulSoup
def build_embed(url):
"""Generate Embed instance from URL finding OGP contents."""
def _find_title(soup: BeautifulSoup, default: str = "") -> str:
og_title = soup.find("meta", attrs={"property": "og:title"})
if og_title:
return og_title["content"]
html_title = soup.find("title")
if html_title:
return html_title.content
return default
def _find_description(soup: BeautifulSoup, default: str = "") -> str:
og_description = soup.find("meta", attrs={"property": "og:description"})
if og_description:
return og_description["content"]
html_description = soup.find("meta", attrs={"name": "description"})
if html_description:
return html_description["content"]
return default
def _find_thumbnail(soup: BeautifulSoup) -> str | None:
og_image = soup.find("meta", attrs={"property": "og:image"})
if og_image:
return og_image["content"]
return None
resp = httpx.get(url)
soup = BeautifulSoup(resp.content, "html.parser")
data = {
"uri": url,
"title": _find_title(soup),
"description": _find_description(soup),
}
return models.AppBskyEmbedExternal.Main(
external=models.AppBskyEmbedExternal.External(**data)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment