Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageSource 添加 get_url 方法 #30

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ _✨ [Nonebot2](https://github.com/nonebot/nonebot2) 用户信息获取插件

可以获取的信息:

| 字段 | 类型 | 说明 | 默认值 | 备注 |
| ---------------- | ----------------------- | -------- | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| user_id | `str` | 用户 id | | |
| user_name | `str` | 用户名 | | |
| user_displayname | `Optional[str]` | 用户昵称 | `None` | |
| user_remark | `Optional[str]` | 用户备注 | `None` | |
| user_avatar | `Optional[ImageSource]` | 用户头像 | `None` | [ImageSource](https://github.com/noneplugin/nonebot-plugin-userinfo/blob/main/nonebot_plugin_userinfo/image_source.py) 可通过 `get_image` 获取 `bytes` 结果 |
| user_gender | `str` | 用户性别 | `"unknown"` | |
| 字段 | 类型 | 说明 | 默认值 | 备注 |
| ---------------- | ----------------------- | -------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| user_id | `str` | 用户 id | | |
| user_name | `str` | 用户名 | | |
| user_displayname | `Optional[str]` | 用户昵称 | `None` | |
| user_remark | `Optional[str]` | 用户备注 | `None` | |
| user_avatar | `Optional[ImageSource]` | 用户头像 | `None` | [ImageSource](https://github.com/noneplugin/nonebot-plugin-userinfo/blob/main/nonebot_plugin_userinfo/image_source.py) 可通过 `get_url` 获取链接,通过 `get_image` 获取 `bytes` 结果 |
| user_gender | `str` | 用户性别 | `"unknown"` | |

### 安装

Expand Down
40 changes: 34 additions & 6 deletions nonebot_plugin_userinfo/image_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@


class ImageSource(BaseModel):
def get_url(self) -> str:
raise NotImplementedError

async def get_image(self) -> bytes:
raise NotImplementedError


class ImageUrl(ImageSource):
url: str

def get_url(self) -> str:
return self.url

async def get_image(self) -> bytes:
return await download_url(self.url)

Expand Down Expand Up @@ -59,19 +65,25 @@ def check_emoji(cls, value: str) -> str:
raise ValueError("Not a emoji")
return value

def get_url(self, style: EmojiStyle = EmojiStyle.Apple) -> str:
return f"https://emojicdn.elk.sh/{self.data}?style={style}"

async def get_image(self, style: EmojiStyle = EmojiStyle.Apple) -> bytes:
url = f"https://emojicdn.elk.sh/{self.data}?style={style}"
url = self.get_url(style)
return await download_url(url)


class QQAvatar(ImageSource):
qq: int

def get_url(self, size: int = 640) -> str:
return f"http://q1.qlogo.cn/g?b=qq&nk={self.qq}&s={size}"

async def get_image(self) -> bytes:
url = f"http://q1.qlogo.cn/g?b=qq&nk={self.qq}&s=640"
url = self.get_url(size=640)
data = await download_url(url)
if hashlib.md5(data).hexdigest() == "acef72340ac0e914090bd35799f5594e":
url = f"http://q1.qlogo.cn/g?b=qq&nk={self.qq}&s=100"
url = self.get_url(size=100)
data = await download_url(url)
return data

Expand All @@ -80,19 +92,27 @@ class QQAvatarOpenId(ImageSource):
appid: str
user_openid: str

def get_url(self) -> str:
return f"https://q.qlogo.cn/qqapp/{self.appid}/{self.user_openid}/100"

async def get_image(self) -> bytes:
url = f"https://q.qlogo.cn/qqapp/{self.appid}/{self.user_openid}/100"
url = self.get_url()
return await download_url(url)


class TelegramFile(ImageSource):
token: str
file_path: str

def get_url(self, api_server: str = "https://api.telegram.org/") -> str:
if Path(self.file_path).exists():
return f"file://{self.file_path}"
return f"{api_server}file/bot{self.token}/{self.file_path}"

async def get_image(self, api_server: str = "https://api.telegram.org/") -> bytes:
if Path(self.file_path).exists():
return await anyio.Path(self.file_path).read_bytes()
url = f"{api_server}file/bot{self.token}/{self.file_path}"
url = self.get_url(api_server)
return await download_url(url)


Expand All @@ -110,11 +130,19 @@ class DiscordUserAvatar(ImageSource):
user_id: int
image_hash: str

def get_url(
self,
base_url: str = "https://cdn.discordapp.com/",
image_format: DiscordImageFormat = DiscordImageFormat.PNG,
image_size: int = 1024,
) -> str:
return f"{base_url}avatars/{self.user_id}/{self.image_hash}.{image_format}?size={image_size}"

async def get_image(
self,
base_url: str = "https://cdn.discordapp.com/",
image_format: DiscordImageFormat = DiscordImageFormat.PNG,
image_size: int = 1024,
) -> bytes:
url = f"{base_url}avatars/{self.user_id}/{self.image_hash}.{image_format}?size={image_size}"
url = self.get_url(base_url, image_format, image_size)
return await download_url(url)
Loading