You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.2 KiB
45 lines
1.2 KiB
"""图片消息解析"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
from pathlib import Path
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# 微信图片缓存默认路径
|
|
_WECHAT_IMAGE_DIRS = [
|
|
Path(os.environ.get("APPDATA", "")) / "Tencent" / "WeChat" / "Image",
|
|
Path(os.environ.get("USERPROFILE", "")) / "Documents" / "WeChat Files",
|
|
]
|
|
|
|
|
|
class ImageParser:
|
|
"""解析图片消息,尝试定位本地缓存文件"""
|
|
|
|
@staticmethod
|
|
def is_image_message(content: str) -> bool:
|
|
tag = content.strip("[]")
|
|
return "图片" in tag or "image" in tag.lower()
|
|
|
|
@staticmethod
|
|
def find_cached_image(filename: str) -> Path | None:
|
|
"""在常见微信缓存目录中查找图片文件"""
|
|
for base_dir in _WECHAT_IMAGE_DIRS:
|
|
if not base_dir.exists():
|
|
continue
|
|
for p in base_dir.rglob(filename):
|
|
if p.is_file():
|
|
return p
|
|
return None
|
|
|
|
@staticmethod
|
|
def get_image_info(path: Path) -> dict:
|
|
"""获取图片基本信息"""
|
|
stat = path.stat()
|
|
return {
|
|
"path": str(path),
|
|
"size": stat.st_size,
|
|
"suffix": path.suffix,
|
|
}
|
|
|