Skip to content

Commit

Permalink
♻️ refactor opengraph with cache support
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu committed May 23, 2024
1 parent e243fd9 commit 83dc43e
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 61 deletions.
13 changes: 4 additions & 9 deletions src/plugins/about/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@
@Author : yanyongyu
@Date : 2023-03-08 00:11:17
@LastEditors : yanyongyu
@LastEditTime : 2023-12-14 16:57:09
@LastEditTime : 2024-05-23 14:22:49
@Description : About plugin
@GitHub : https://github.com/yanyongyu
"""

__author__ = "yanyongyu"

import secrets

from nonebot import on_command
from nonebot.plugin import PluginMetadata
from nonebot.adapters.onebot.v11 import MessageSegment as QQMS
from nonebot.adapters.qq import MessageSegment as QQOfficialMS

from src.providers.filehost import save_online_image
from src.plugins.github.helpers import NO_GITHUB_EVENT
from src.plugins.github.libs.opengraph import get_opengraph_image
from src.plugins.github.cache.message_tag import RepoTag, create_message_tag
from src.providers.platform import (
TARGET_INFO,
Expand Down Expand Up @@ -49,21 +47,18 @@ async def handle_about(target_info: TARGET_INFO, message_info: MESSAGE_INFO):
)

tag = RepoTag(owner=OWNER, repo=REPO, is_receive=False)
image_url = (
f"https://opengraph.githubassets.com/{secrets.token_urlsafe(16)}/{OWNER}/{REPO}"
)

match target_info.type:
case TargetType.QQ_USER | TargetType.QQ_GROUP:
result = await about.send(QQMS.image(await save_online_image(image_url)))
result = await about.send(QQMS.image(await get_opengraph_image(tag)))
case (
TargetType.QQ_OFFICIAL_USER
| TargetType.QQGUILD_USER
| TargetType.QQ_OFFICIAL_GROUP
| TargetType.QQGUILD_CHANNEL
):
result = await about.send(
QQOfficialMS.image(await save_online_image(image_url))
QQOfficialMS.file_image(await get_opengraph_image(tag))
)

if sent_message_info := extract_sent_message(target_info, result):
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/cache/message_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async def create_message_tag(message: MessageInfo, tag: Tag) -> None:
MESSAGE_TAG_CACHE_KEY.format(
type=message.type.value, message_id=str(message.id)
),
tag.json(),
tag.model_dump_json(),
ex=MESSAGE_TAG_CACHE_EXPIRE,
)

Expand Down
22 changes: 22 additions & 0 deletions src/plugins/github/cache/opengraph.py
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)
)
79 changes: 79 additions & 0 deletions src/plugins/github/libs/opengraph.py
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(

Check failure on line 61 in src/plugins/github/libs/opengraph.py

View workflow job for this annotation

GitHub Actions / Pyright Lint

Cannot access attribute "request" for class "Driver"   Attribute "request" is unknown (reportAttributeAccessIssue)
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
71 changes: 20 additions & 51 deletions src/plugins/github/plugins/github_opengraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@
@Author : yanyongyu
@Date : 2021-04-26 18:19:15
@LastEditors : yanyongyu
@LastEditTime : 2023-12-11 12:04:25
@LastEditTime : 2024-05-23 14:23:14
@Description : None
@GitHub : https://github.com/yanyongyu
"""

__author__ = "yanyongyu"

import secrets

from nonebot import on_regex
from nonebot.typing import T_State
from nonebot.plugin import PluginMetadata
from nonebot.adapters.onebot.v11 import MessageSegment as QQMS
from nonebot.adapters.qq import MessageSegment as QQOfficialMS

from src.plugins.github import config
from src.providers.filehost import save_online_image
from src.plugins.github.helpers import NO_GITHUB_EVENT
from src.plugins.github.libs.opengraph import get_opengraph_image
from src.plugins.github.dependencies import (
COMMIT,
RELEASE,
Expand Down Expand Up @@ -72,28 +70,21 @@
async def handle(
target_info: TARGET_INFO, message_info: MESSAGE_INFO, repo: REPOSITORY
):
await create_message_tag(
message_info,
RepoTag(owner=repo.owner.login, repo=repo.name, is_receive=True),
)

image_url = (
f"https://opengraph.githubassets.com/{secrets.token_urlsafe(16)}/"
f"{repo.owner.login}/{repo.name}"
)
tag = RepoTag(owner=repo.owner.login, repo=repo.name, is_receive=True)

await create_message_tag(message_info, tag)

match target_info.type:
case TargetType.QQ_USER | TargetType.QQ_GROUP:
result = await repo_graph.send(
QQMS.image(await save_online_image(image_url))
)
result = await repo_graph.send(QQMS.image(await get_opengraph_image(tag)))
case (
TargetType.QQ_OFFICIAL_USER
| TargetType.QQGUILD_USER
| TargetType.QQ_OFFICIAL_GROUP
| TargetType.QQGUILD_CHANNEL
):
result = await repo_graph.send(
QQOfficialMS.image(await save_online_image(image_url))
QQOfficialMS.file_image(await get_opengraph_image(tag))
)

tag = RepoTag(owner=repo.owner.login, repo=repo.name, is_receive=False)
Expand Down Expand Up @@ -124,33 +115,21 @@ async def handle_commit(
owner = state["owner"]
repo = state["repo"]

await create_message_tag(
message_info,
CommitTag(
owner=owner,
repo=repo,
commit=commit.sha,
is_receive=True,
),
)

image_url = (
f"https://opengraph.githubassets.com/{secrets.token_urlsafe(16)}/"
f"{owner}/{repo}/commit/{commit.sha}"
)
tag = CommitTag(owner=owner, repo=repo, commit=commit.sha, is_receive=True)

await create_message_tag(message_info, tag)

match target_info.type:
case TargetType.QQ_USER | TargetType.QQ_GROUP:
result = await commit_graph.send(
QQMS.image(await save_online_image(image_url))
)
result = await commit_graph.send(QQMS.image(await get_opengraph_image(tag)))
case (
TargetType.QQ_OFFICIAL_USER
| TargetType.QQGUILD_USER
| TargetType.QQ_OFFICIAL_GROUP
| TargetType.QQGUILD_CHANNEL
):
result = await commit_graph.send(
QQOfficialMS.image(await save_online_image(image_url))
QQOfficialMS.file_image(await get_opengraph_image(tag))
)

tag = CommitTag(owner=owner, repo=repo, commit=commit.sha, is_receive=False)
Expand All @@ -175,24 +154,14 @@ async def handle_release(
owner = state["owner"]
repo = state["repo"]

await create_message_tag(
message_info,
ReleaseTag(
owner=owner,
repo=repo,
tag=release.tag_name,
is_receive=True,
),
)

image_url = (
f"https://opengraph.githubassets.com/{secrets.token_urlsafe(16)}/"
f"{owner}/{repo}/releases/tag/{release.tag_name}"
)
tag = ReleaseTag(owner=owner, repo=repo, tag=release.tag_name, is_receive=True)

await create_message_tag(message_info, tag)

match target_info.type:
case TargetType.QQ_USER | TargetType.QQ_GROUP:
result = await release_graph.send(
QQMS.image(await save_online_image(image_url))
QQMS.image(await get_opengraph_image(tag))
)
case (
TargetType.QQ_OFFICIAL_USER
Expand All @@ -201,7 +170,7 @@ async def handle_release(
| TargetType.QQGUILD_CHANNEL
):
result = await release_graph.send(
QQOfficialMS.image(await save_online_image(image_url))
QQOfficialMS.file_image(await get_opengraph_image(tag))
)

tag = ReleaseTag(owner=owner, repo=repo, tag=release.tag_name, is_receive=False)
Expand Down

0 comments on commit 83dc43e

Please sign in to comment.