Skip to content

Commit 29b7e4e

Browse files
committed
feat(BangumiService): add getSubjectInfo method and refactor related functionality
1 parent 402caa5 commit 29b7e4e

9 files changed

Lines changed: 324 additions & 75 deletions

File tree

common/src/main/java/com/ligg/common/thirdparty/bangumi/response/SubjectDetailDto.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ public static class SubjectTag {
8888
@JsonIgnoreProperties(ignoreUnknown = true)
8989
public static class SubjectInterest {
9090
private Long id;
91-
private Integer rate;
91+
private Integer rate = 0;
9292
private Integer type;
93-
private String comment;
94-
private List<String> tags;
95-
private Integer epStatus;
96-
private Integer volStatus;
93+
private String comment = "";
94+
private List<String> tags = List.of();
95+
private Integer epStatus = 0;
96+
private Integer volStatus = 0;
9797
@JsonProperty("private")
98-
private Boolean privately;
98+
private Boolean privately = false;
9999
private Long updatedAt;
100100
}
101101
}

common/src/main/java/com/ligg/common/utils/InfoboxParser.java

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.util.StringUtils;
44

5+
import java.util.ArrayList;
56
import java.util.Collections;
67
import java.util.LinkedHashMap;
78
import java.util.List;
@@ -12,20 +13,6 @@
1213

1314
/**
1415
* Bangumi wiki 原始字符串(infobox)解析工具。
15-
*
16-
* <pre>
17-
* 输入示例:
18-
* {{Infobox animanga/TVAnime
19-
* |中文名= 呪術廻戦
20-
* |话数= 24
21-
* |放送开始= 2020年10月2日
22-
* |导演= 朴性厚
23-
* |原作= 芥見下々
24-
* |人物设定= 平松禎史
25-
* }}
26-
*
27-
* 输出 info 示例: "24话 / 2020年10月2日 / 朴性厚 / 芥見下々 / 平松禎史"
28-
* </pre>
2916
*/
3017
public final class InfoboxParser {
3118

@@ -88,4 +75,62 @@ public static String toInfo(String infobox) {
8875
.filter(StringUtils::hasText)
8976
.collect(Collectors.joining(" / "));
9077
}
78+
79+
/**
80+
* 将 infobox wiki 字符串解析为 key → 多值列表,同时处理单行和多行({…})值。
81+
* <pre>
82+
* 单行:|话数= 25 → "话数" → ["25"]
83+
* 多行:|别名={ → "别名" → ["叛逆的鲁路修R2", "Code Geass: ..."]
84+
* [叛逆的鲁路修R2]
85+
* [Code Geass: ...]
86+
* }
87+
* </pre>
88+
*/
89+
public static Map<String, List<String>> toEntries(String infobox) {
90+
if (!StringUtils.hasText(infobox)) {
91+
return Collections.emptyMap();
92+
}
93+
Map<String, List<String>> entries = new LinkedHashMap<>();
94+
String[] lines = infobox.split("\\R");
95+
String currentKey = null;
96+
boolean inBlock = false;
97+
98+
for (String line : lines) {
99+
String trimmed = line.trim();
100+
if (trimmed.equals("}}")) {
101+
break;
102+
}
103+
if (trimmed.startsWith("{{")) {
104+
continue;
105+
}
106+
if (inBlock) {
107+
if (trimmed.equals("}")) {
108+
inBlock = false;
109+
currentKey = null;
110+
continue;
111+
}
112+
// 多行值:去掉首尾的 [ ]
113+
if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
114+
String v = trimmed.substring(1, trimmed.length() - 1).trim();
115+
if (!v.isEmpty()) {
116+
entries.get(currentKey).add(v);
117+
}
118+
}
119+
continue;
120+
}
121+
Matcher m = INFOBOX_LINE.matcher(trimmed);
122+
if (m.matches()) {
123+
currentKey = m.group(1).trim();
124+
String value = m.group(2).trim();
125+
if (value.equals("{")) {
126+
inBlock = true;
127+
entries.put(currentKey, new ArrayList<>());
128+
} else {
129+
entries.put(currentKey, new ArrayList<>(List.of(value)));
130+
currentKey = null;
131+
}
132+
}
133+
}
134+
return Collections.unmodifiableMap(entries);
135+
}
91136
}

flow-bootstrap/src/main/resources/application.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ anime-flow:
5454

5555
sync:
5656
archive:
57-
enabled: true
57+
enabled: false
5858
run-on-startup-if-missing: true
5959
latest-url: https://raw.githubusercontent.com/openAnimeFlow/animeFlow-assets/main/archive/latest.json
6060
cron: "0 0 3 * * *"

flow-client/src/main/java/com/ligg/flowclient/controller/bangumi/SubjectsController.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -322,32 +322,15 @@ public Result<SubjectDetailVo> subjectDetail(
322322
@NotNull @PathVariable int subjectId,
323323
@RequestAttribute(name = AuthorizationInterceptor.ACCESS_TOKEN_REQUEST_ATTRIBUTE, required = false)
324324
String flowAccessToken) {
325+
long userId = 0;
325326
if (StringUtils.hasText(flowAccessToken)) {
326327
try {
327-
Long userId = jwtTokenService.validateAccessToken(flowAccessToken);
328-
UserOauthEntity oauth = bangumiOAuthTokenService.findBangumiOauth(userId);
329-
if (oauth != null) {
330-
SubjectDetailDto dto = bangumiOAuthExecutor.execute(oauth,
331-
bangumiToken -> bangumiClient.getSubject(subjectId, bangumiToken));
332-
return Result.success(ResponseCode.SUCCESS, toSubjectDetailVo(dto));
333-
}
328+
userId = jwtTokenService.validateAccessToken(flowAccessToken);
334329
} catch (LoginExpiredException ignored) {
335-
// token 无效或未绑定 Bangumi,回退公开缓存逻辑
330+
// 未登录,回退公开访问
336331
}
337332
}
338-
339-
String cacheKey = BangumiConstants.BANGUMI_SUBJECT_DETAIL_CACHE_KEY_PREFIX + ':' + subjectId;
340-
SubjectDetailVo vo = bangumiCacheService.getOrLoad(
341-
cacheKey,
342-
BangumiCacheService.lockKey(cacheKey),
343-
SubjectDetailVo.class,
344-
BangumiConstants.BANGUMI_SUBJECT_DETAIL_CACHE_TTL_SECONDS,
345-
"获取条目详情超时,请稍后重试",
346-
"获取条目详情被中断",
347-
() -> toSubjectDetailVo(bangumiClient.getSubject(subjectId, null)),
348-
SubjectsController::shouldCacheSubjectDetail,
349-
() -> log.info("条目详情(命中缓存), subjectId={}", subjectId));
350-
return Result.success(ResponseCode.SUCCESS, vo);
333+
return Result.success(ResponseCode.SUCCESS, bangumiService.getSubjectInfo(subjectId, userId));
351334
}
352335

353336
/**

flow-client/src/main/java/com/ligg/flowclient/mapper/UserBgmCollectionMapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
44
import com.ligg.common.entity.UserBgmCollectionEntity;
55
import com.ligg.flowclient.module.dto.UserBgmCollectionRow;
6+
import com.ligg.flowclient.module.dto.UserSubjectInterestRow;
67
import org.apache.ibatis.annotations.Mapper;
78
import org.apache.ibatis.annotations.Param;
89

@@ -20,4 +21,11 @@ List<UserBgmCollectionRow> selectPageByUserFilter(@Param("userId") Long userId,
2021
@Param("subjectType") int subjectType,
2122
@Param("limit") int limit,
2223
@Param("offset") int offset);
24+
25+
/**
26+
* 获取用户条目评价
27+
*/
28+
UserSubjectInterestRow selectUserSubjectInterest(@Param("userId") Long userId,
29+
@Param("subjectId") Integer subjectId);
30+
2331
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @author Ligg
3+
* @date 2026/7/2 02:48
4+
*/
5+
package com.ligg.flowclient.module.dto;
6+
7+
import lombok.AllArgsConstructor;
8+
import lombok.Data;
9+
import lombok.NoArgsConstructor;
10+
11+
import java.util.List;
12+
13+
@Data
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
public class UserSubjectInterestRow {
17+
private Long id;
18+
private Integer rate;
19+
private Integer type;
20+
private String comment;
21+
private List<String> tags;
22+
private Integer epStatus;
23+
private Integer volStatus;
24+
private Boolean privately;
25+
private Long bgmUpdatedAt;
26+
}

flow-client/src/main/java/com/ligg/flowclient/service/BangumiService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
import com.ligg.common.thirdparty.bangumi.response.SubjectEpisodesDto;
55
import com.ligg.common.thirdparty.bangumi.response.SubjectsDto;
66
import com.ligg.common.vo.bangumi.SearchSuggestionsVo;
7+
import com.ligg.common.vo.bangumi.SubjectDetailVo;
78
import com.ligg.common.vo.bangumi.SubjectRelationsVo;
89

910
public interface BangumiService {
1011

12+
/**
13+
* 条目详情
14+
*/
15+
SubjectDetailVo getSubjectInfo(Integer subjectId, long userId);
16+
1117
/**
1218
* 获取番剧剧集列表
1319
*/
@@ -28,5 +34,5 @@ public interface BangumiService {
2834
/**
2935
* 获取关联条目
3036
*/
31-
SubjectRelationsVo getRelatedSubjects(Integer subjectId, int limit, int offset,int type, String bangumiAccessToken);
37+
SubjectRelationsVo getRelatedSubjects(Integer subjectId, int limit, int offset, int type, String bangumiAccessToken);
3238
}

0 commit comments

Comments
 (0)