99
1010from config .config_fields import FIELDS
1111from utils import logger
12- from utils .utils import convert_date_jp_to_iso
12+ from utils .utils import convert_date_jp_to_iso , normalize_brand_name
1313
1414
1515class NotionClient :
@@ -23,6 +23,7 @@ def __init__(self, token, game_db_id, brand_db_id, client: httpx.AsyncClient):
2323 "Notion-Version" : "2022-06-28" ,
2424 "Content-Type" : "application/json" ,
2525 }
26+ self ._all_brands_cache = None
2627
2728 async def _request (self , method , url , json_data = None ):
2829 try :
@@ -88,29 +89,39 @@ async def check_page_exists(self, page_id):
8889 except Exception :
8990 return False
9091
91- async def search_brand (self , brand_name ):
92- url = f"https://api.notion.com/v1/databases/{ self .brand_db_id } /query"
93- payload = {"filter" : {"property" : FIELDS ["brand_name" ], "title" : {"equals" : brand_name }}}
94- resp = await self ._request ("POST" , url , payload )
95- return resp .get ("results" , []) if resp else []
92+ async def get_all_brands (self ):
93+ if self ._all_brands_cache is not None :
94+ return self ._all_brands_cache
95+
96+ all_pages = await self .get_all_pages_from_db (self .brand_db_id )
97+ brands = []
98+ for page in all_pages :
99+ props = page .get ("properties" , {})
100+ title_data = props .get (FIELDS ["brand_name" ], {}).get ("title" , [])
101+ title = "" .join ([t .get ("plain_text" , "" ) for t in title_data ]).strip ()
102+ if title :
103+ brands .append ({"title" : title , "id" : page ["id" ]})
104+ self ._all_brands_cache = brands
105+ return brands
96106
97107 async def get_brand_details_by_name (self , name : str ) -> dict | None :
98108 """根据品牌名称查找品牌,并返回其ID和图标状态。"""
99- results = await self .search_brand (name )
100- if not results :
109+ if not name :
101110 return None
102-
103- # 假设第一个结果是正确的
104- page = results [0 ]
105- page_id = page .get ("id" )
106- properties = page .get ("properties" , {})
107- icon_prop = properties .get (FIELDS ["brand_icon" ], {})
108-
109- # 修正:仅当品牌logo文件(files属性)存在时,才认为已有图标。
110- # 移除 or bool(page.get("icon")),避免将页面的Emoji误判为有效图标。
111- has_icon = bool (icon_prop .get ("files" ))
112-
113- return {"page_id" : page_id , "has_icon" : has_icon }
111+
112+ all_brands = await self .get_all_brands ()
113+ normalized_name = normalize_brand_name (name )
114+
115+ for brand in all_brands :
116+ if normalize_brand_name (brand ["title" ]) == normalized_name :
117+ page = await self .get_page (brand ["id" ])
118+ if not page :
119+ continue
120+ properties = page .get ("properties" , {})
121+ icon_prop = properties .get (FIELDS ["brand_icon" ], {})
122+ has_icon = bool (icon_prop .get ("files" ))
123+ return {"page_id" : brand ["id" ], "has_icon" : has_icon }
124+ return None
114125
115126 async def get_all_game_titles (self ):
116127 url = f"https://api.notion.com/v1/databases/{ self .game_db_id } /query"
@@ -399,8 +410,9 @@ async def create_or_update_game(self, properties_schema: dict, page_id=None, **i
399410
400411 async def create_or_update_brand (self , brand_name , page_id = None , ** info ):
401412 if not page_id :
402- existing = await self .search_brand (brand_name )
403- page_id = existing [0 ]["id" ] if existing else None
413+ brand_details = await self .get_brand_details_by_name (brand_name )
414+ if brand_details :
415+ page_id = brand_details .get ("id" )
404416
405417 schema_data = await self .get_database_schema (self .brand_db_id )
406418 if not schema_data :
0 commit comments