You've already forked favicon-api-async
25.09.11
This commit is contained in:
@@ -29,6 +29,10 @@ warnings.filterwarnings("ignore", category=XMLParsedAsHTMLWarning)
|
||||
# 获取当前所在目录的绝对路径
|
||||
_current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
_url_count = 0
|
||||
_icon_count = 0
|
||||
_cache_count = 0
|
||||
|
||||
# 预编译正则表达式,提高性能
|
||||
pattern_icon = re.compile(r'(icon|shortcut icon|alternate icon|apple-touch-icon)+', re.I)
|
||||
pattern_link = re.compile(r'(<link[^>]+rel=.(icon|shortcut icon|alternate icon|apple-touch-icon)[^>]+>)', re.I)
|
||||
@@ -235,6 +239,7 @@ async def get_favicon_handler(request: Request,
|
||||
url: Optional[str] = None,
|
||||
refresh: Optional[str] = None) -> dict[str, str] | Response:
|
||||
"""异步处理获取图标的请求"""
|
||||
global _url_count, _icon_count, _cache_count
|
||||
|
||||
# 验证URL参数
|
||||
if not url:
|
||||
@@ -243,7 +248,8 @@ async def get_favicon_handler(request: Request,
|
||||
try:
|
||||
entity = Favicon(url)
|
||||
|
||||
logger.info(f"-> failed url size: {len(favicon.failed_urls)}")
|
||||
# 统计URL数量
|
||||
_url_count += 1
|
||||
|
||||
# 验证域名
|
||||
if not entity.domain:
|
||||
@@ -257,6 +263,11 @@ async def get_favicon_handler(request: Request,
|
||||
else:
|
||||
del favicon.failed_urls[entity.domain]
|
||||
|
||||
logger.info(
|
||||
f"-> count (failed/cached/icon/url): "
|
||||
f"{len(favicon.failed_urls)}/{_cache_count}/{_icon_count}/{_url_count}"
|
||||
)
|
||||
|
||||
# 检查缓存
|
||||
_cached, cached_icon = _get_cache_icon(entity.domain_md5, refresh=refresh in ['true', '1'])
|
||||
|
||||
@@ -276,6 +287,8 @@ async def get_favicon_handler(request: Request,
|
||||
logger.info(f"缓存已过期,加入后台队列刷新(异步): {entity.domain}")
|
||||
bg_tasks.add_task(get_icon_async, entity, _cached)
|
||||
|
||||
# 缓存计数
|
||||
_cache_count += 1
|
||||
return Response(content=icon_content,
|
||||
media_type=content_type if content_type else "image/x-icon",
|
||||
headers=_get_header(content_type, cache_time))
|
||||
@@ -303,6 +316,7 @@ async def get_favicon_handler(request: Request,
|
||||
|
||||
async def get_icon_async(entity: Favicon, _cached: bytes = None) -> Optional[bytes]:
|
||||
"""异步获取图标"""
|
||||
global _icon_count
|
||||
icon_content = None
|
||||
|
||||
try:
|
||||
@@ -355,6 +369,9 @@ async def get_icon_async(entity: Favicon, _cached: bytes = None) -> Optional[byt
|
||||
# 写入缓存文件(注意:文件IO操作仍然是同步的)
|
||||
FileUtil.write_file(cache_path, icon_content, mode='wb')
|
||||
FileUtil.write_file(md5_path, entity.domain, mode='w')
|
||||
|
||||
# 任务计数
|
||||
_icon_count += 1
|
||||
except Exception as e:
|
||||
logger.error(f"异步写入缓存文件失败: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user