Skip to content

Commit

Permalink
perf: 修改匿名头像为本地文件
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed May 18, 2024
1 parent 7b8af60 commit d0415f3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Binary file added assets/bag-on-head.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 26 additions & 2 deletions campux/social/render/apirender.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import datetime
import base64

import aiohttp

Expand Down Expand Up @@ -94,6 +95,23 @@
</script>"""


async def get_image_base64(
url: str=None,
local_path: str=None
) -> str:

if url:
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
img = await resp.read()
return base64.b64encode(img).decode()

if local_path:
with open(local_path, "rb") as f:
img = f.read()
return base64.b64encode(img).decode()


class IdoknowAPIRender:

ap: app.Application
Expand All @@ -108,7 +126,6 @@ async def render(self, post: entity.Post) -> bytes:
jinja_data = {
"username": str(post.uin),
"content": post.text,
"user_avatar": f"https://q1.qlogo.cn/g?b=qq&amp;nk={post.uin}&amp;s=640",
"foot_left_hint": f"{post.uin} 发表于 {time_str}",
"foot_right_hint": "开发 @RockChinQ | @Soulter",
"bg_fixed_br": f"https://q1.qlogo.cn/g?b=qq&amp;nk={self.ap.config.campux_qq_bot_uin}&amp;s=640",
Expand All @@ -117,9 +134,16 @@ async def render(self, post: entity.Post) -> bytes:

if post.anon:
jinja_data["username"] = "匿名"
jinja_data["user_avatar"] = "https://avatars.githubusercontent.com/u/10137?v=4"
jinja_data["foot_left_hint"] = "匿名用户 发表于 " + time_str

jinja_data["user_avatar"] = "data:image/png;base64," + await get_image_base64(
local_path="assets/bag-on-head.png"
)
else:
jinja_data["user_avatar"] = "data:image/png;base64," + await get_image_base64(
url=f"https://q1.qlogo.cn/g?b=qq&nk={post.uin}&s=640"
)

async with aiohttp.ClientSession() as session:
async with session.post(
self.ap.config.campux_text_to_image_api+"/generate",
Expand Down

0 comments on commit d0415f3

Please sign in to comment.