-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor opengraph with cache support
- Loading branch information
Showing
5 changed files
with
126 additions
and
61 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from datetime import timedelta | ||
|
||
from src.providers.redis import redis_client | ||
|
||
OPENGRAPH_CACHE_KEY = "cache:github:opengraph:{type}:{identifier}" | ||
OPENGRAPH_CACHE_EXPIRE = timedelta(days=1) | ||
|
||
|
||
async def save_opengraph(type: str, identifier: str, img: bytes) -> None: | ||
"""Save opengraph image to Redis.""" | ||
await redis_client.set( | ||
OPENGRAPH_CACHE_KEY.format(type=type, identifier=identifier), | ||
img, | ||
ex=OPENGRAPH_CACHE_EXPIRE, | ||
) | ||
|
||
|
||
async def get_opengraph(type: str, identifier: str) -> bytes | None: | ||
"""Get opengraph image from Redis.""" | ||
return await redis_client.get( | ||
OPENGRAPH_CACHE_KEY.format(type=type, identifier=identifier) | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import secrets | ||
|
||
import nonebot | ||
from nonebot.drivers import Request, HTTPClientMixin | ||
|
||
from src.plugins.github.cache.opengraph import get_opengraph, save_opengraph | ||
from src.plugins.github.cache.message_tag import ( | ||
Tag, | ||
RepoTag, | ||
IssueTag, | ||
CommitTag, | ||
ReleaseTag, | ||
PullRequestTag, | ||
) | ||
|
||
driver = nonebot.get_driver() | ||
assert isinstance(driver, HTTPClientMixin) | ||
|
||
|
||
async def get_opengraph_image(tag: Tag) -> bytes: | ||
match tag: | ||
case IssueTag(): | ||
cache_type = "issue" | ||
cache_identifier = f"{tag.owner}/{tag.repo}/{tag.number}" | ||
link = ( | ||
f"https://opengraph.githubassets.com/{secrets.token_urlsafe(16)}/" | ||
f"{tag.owner}/{tag.repo}/issues/{tag.number}" | ||
) | ||
case PullRequestTag(): | ||
cache_type = "pr" | ||
cache_identifier = f"{tag.owner}/{tag.repo}/{tag.number}" | ||
link = ( | ||
f"https://opengraph.githubassets.com/{secrets.token_urlsafe(16)}/" | ||
f"{tag.owner}/{tag.repo}/pull/{tag.number}" | ||
) | ||
case CommitTag(): | ||
cache_type = "commit" | ||
cache_identifier = f"{tag.owner}/{tag.repo}/{tag.commit}" | ||
link = ( | ||
f"https://opengraph.githubassets.com/{secrets.token_urlsafe(16)}/" | ||
f"{tag.owner}/{tag.repo}/commit/{tag.commit}" | ||
) | ||
case ReleaseTag(): | ||
cache_type = "release" | ||
cache_identifier = f"{tag.owner}/{tag.repo}/{tag.tag}" | ||
link = ( | ||
f"https://opengraph.githubassets.com/{secrets.token_urlsafe(16)}/" | ||
f"{tag.owner}/{tag.repo}/releases/tag/{tag.tag}" | ||
) | ||
case RepoTag(): | ||
cache_type = "repo" | ||
cache_identifier = f"{tag.owner}/{tag.repo}" | ||
link = ( | ||
f"https://opengraph.githubassets.com/{secrets.token_urlsafe(16)}/" | ||
f"{tag.owner}/{tag.repo}" | ||
) | ||
|
||
if image := await get_opengraph(cache_type, cache_identifier): | ||
return image | ||
|
||
resp = await driver.request( | ||
Request( | ||
"GET", | ||
link, | ||
headers={ | ||
"User-Agent": ( | ||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like" | ||
" Gecko) Chrome/120.0.0.0 Safari/537.36" | ||
) | ||
}, | ||
) | ||
) | ||
if (400 <= resp.status_code < 600) or not (content := resp.content): | ||
raise RuntimeError(f"Failed to download opengraph for {tag!r}: {resp}") | ||
if isinstance(content, str): | ||
content = content.encode("utf-8") | ||
|
||
await save_opengraph(cache_type, cache_identifier, content) | ||
return content |
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