Last active
February 20, 2024 18:11
-
-
Save attakei/d4b9974d06dadf41a04e914ac2c14594 to your computer and use it in GitHub Desktop.
shortcut functions to send post using atproto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Please see `shorcuts.py` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
atproto | |
beautifulsoup4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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