Add entities to must-read list
(profile, entity_type, entities)
| 160 | |
| 161 | |
| 162 | def add_must_read(profile, entity_type, entities): |
| 163 | """Add entities to must-read list""" |
| 164 | must_read = profile.get('must_read', {}) |
| 165 | current_list = must_read.get(entity_type, []) |
| 166 | added, already_exists, routed_to_interest = [], [], [] |
| 167 | for entity in entities: |
| 168 | if entity_type == "keywords": |
| 169 | broad_direction = _resolve_broad_must_read_keyword(entity) |
| 170 | if broad_direction: |
| 171 | routed_to_interest.append( |
| 172 | _route_keyword_to_interest_direction(profile, current_list, entity, broad_direction) |
| 173 | ) |
| 174 | continue |
| 175 | if entity in current_list: |
| 176 | already_exists.append(entity) |
| 177 | else: |
| 178 | added.append(entity) |
| 179 | current_list.append(entity) |
| 180 | |
| 181 | must_read[entity_type] = current_list |
| 182 | updated = profile.copy() |
| 183 | updated['must_read'] = must_read |
| 184 | updated['version'] = f"0.{int(profile.get('version', '0.1').split('.')[1]) + 1}" |
| 185 | updated['updated_at'] = datetime.now().isoformat() |
| 186 | |
| 187 | type_labels = {'authors': '作者', 'institutions': '机构', 'keywords': '关键词'} |
| 188 | label = type_labels.get(entity_type, entity_type) |
| 189 | |
| 190 | if added or routed_to_interest: |
| 191 | msg = f"[OK] 已添加 {label}:" + ", ".join(added) |
| 192 | if not added: |
| 193 | msg = "[OK] 已更新兴趣方向" |
| 194 | if routed_to_interest: |
| 195 | msg += "\n[提示] 以下宽泛关键词已转为软兴趣方向,不进入硬必读:" |
| 196 | msg += "\n" + "\n".join(f" - {item}" for item in routed_to_interest) |
| 197 | if already_exists: |
| 198 | msg += f"\n[提示] 已存在:" + ", ".join(already_exists) |
| 199 | msg += f"\n\n当前必读{label}清单:\n" + "\n".join(f" - {e}" for e in current_list) |
| 200 | elif already_exists: |
| 201 | msg = f"[提示] {', '.join(already_exists)} 已在必读{label}清单中" |
| 202 | else: |
| 203 | msg = "[错误] 未添加任何内容" |
| 204 | return (bool(added or routed_to_interest), msg, updated if (added or routed_to_interest) else None) |
| 205 | |
| 206 | |
| 207 | def remove_must_read(profile, entity_type, entities): |
no test coverage detected