diff --git a/UPDATE.md b/UPDATE.md
index f44d4b35..65dc3802 100644
--- a/UPDATE.md
+++ b/UPDATE.md
@@ -1 +1 @@
-预览时可以显示缺少的集数
\ No newline at end of file
+修复获取tmdb标题时年份异常的问题
diff --git a/pom.xml b/pom.xml
index 805b9258..fa737837 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
ani.rss
ani-rss
- 1.1.212
+ 1.1.213
11
diff --git a/src/main/java/ani/rss/util/TmdbUtil.java b/src/main/java/ani/rss/util/TmdbUtil.java
index 80478e9c..b217e85f 100644
--- a/src/main/java/ani/rss/util/TmdbUtil.java
+++ b/src/main/java/ani/rss/util/TmdbUtil.java
@@ -10,11 +10,13 @@
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.google.gson.JsonObject;
+import lombok.Data;
+import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
+import java.util.Date;
import java.util.List;
import java.util.Optional;
-import java.util.concurrent.atomic.AtomicReference;
@Slf4j
public class TmdbUtil {
@@ -32,9 +34,10 @@ public synchronized static String getName(String name, String type) {
if (StrUtil.isBlank(name)) {
return "";
}
- AtomicReference year = new AtomicReference<>("");
- if (ReUtil.contains(StringEnum.YEAR_REG, name)) {
- year.set(ReUtil.get(StringEnum.YEAR_REG, name, 1));
+
+ boolean year = ReUtil.contains(StringEnum.YEAR_REG, name);
+
+ if (year) {
name = name.replaceAll(StringEnum.YEAR_REG, "")
.trim();
}
@@ -42,60 +45,75 @@ public synchronized static String getName(String name, String type) {
return "";
}
Config config = ConfigUtil.CONFIG;
- String tmdbLanguage = config.getTmdbLanguage();
- AtomicReference tmdbId = new AtomicReference<>("");
- String themoviedbName;
+ Tmdb tmdb;
try {
- String finalName = name;
- themoviedbName = HttpReq.get("https://api.themoviedb.org/3/search/" + type, true)
- .form("query", URLUtil.encodeBlank(name))
- .form("api_key", TMDB_API)
- .form("language", tmdbLanguage)
- .thenFunction(res -> {
- Assert.isTrue(res.isOk(), "status: {}", res.getStatus());
- List results = GsonStatic.fromJsonList(GsonStatic.fromJson(res.body(), JsonObject.class)
- .getAsJsonArray("results"), JsonObject.class);
- if (results.isEmpty()) {
- if (!finalName.contains(" ")) {
- return "";
- }
- ThreadUtil.sleep(500);
- return getName(finalName.split(" ")[0], type);
- }
- JsonObject jsonObject = results.get(0);
- String id = jsonObject.get("id").getAsString();
- String title = Optional.of(jsonObject)
- .map(o -> o.get("name"))
- .orElse(jsonObject.get("title")).getAsString();
-
- String date = Optional.of(jsonObject)
- .map(o -> o.get("first_air_date"))
- .orElse(jsonObject.get("release_date")).getAsString();
-
- title = RenameUtil.getName(title);
- tmdbId.set(id);
-
- if (StrUtil.isNotBlank(year.get())) {
- year.set(String.valueOf(DateUtil.year(DateUtil.parse(date))));
- }
-
- return StrUtil.blankToDefault(title, "");
- });
+ tmdb = getTmdb(name, type);
} catch (Exception e) {
String message = ExceptionUtil.getMessage(e);
log.error(message, e);
return "";
}
+
+ String themoviedbName = tmdb.getName();
if (StrUtil.isBlank(themoviedbName)) {
return "";
}
- if (StrUtil.isNotBlank(year.get())) {
- themoviedbName = StrFormatter.format("{} ({})", themoviedbName, year);
+
+ if (year) {
+ themoviedbName = StrFormatter.format("{} ({})", themoviedbName, DateUtil.year(tmdb.getDate()));
}
- if (config.getTmdbId() && StrUtil.isNotBlank(tmdbId.get())) {
- themoviedbName = StrFormatter.format("{} [tmdbid={}]", themoviedbName, tmdbId.get());
+
+ if (config.getTmdbId()) {
+ themoviedbName = StrFormatter.format("{} [tmdbid={}]", themoviedbName, tmdb.getId());
}
return themoviedbName;
}
+
+ public static Tmdb getTmdb(String titleName, String type) {
+ Config config = ConfigUtil.CONFIG;
+ String tmdbLanguage = config.getTmdbLanguage();
+
+ return HttpReq.get("https://api.themoviedb.org/3/search/" + type, true)
+ .form("query", URLUtil.encodeBlank(titleName))
+ .form("api_key", TMDB_API)
+ .form("language", tmdbLanguage)
+ .thenFunction(res -> {
+ Assert.isTrue(res.isOk(), "status: {}", res.getStatus());
+ List results = GsonStatic.fromJsonList(GsonStatic.fromJson(res.body(), JsonObject.class)
+ .getAsJsonArray("results"), JsonObject.class);
+ if (results.isEmpty()) {
+ if (!titleName.contains(" ")) {
+ return null;
+ }
+ ThreadUtil.sleep(500);
+ return getTmdb(titleName.split(" ")[0], type);
+ }
+
+ JsonObject jsonObject = results.get(0);
+ String id = jsonObject.get("id").getAsString();
+ String title = Optional.of(jsonObject)
+ .map(o -> o.get("name"))
+ .orElse(jsonObject.get("title")).getAsString();
+
+ String date = Optional.of(jsonObject)
+ .map(o -> o.get("first_air_date"))
+ .orElse(jsonObject.get("release_date")).getAsString();
+
+ title = RenameUtil.getName(title);
+
+ return new Tmdb()
+ .setId(id)
+ .setName(title)
+ .setDate(DateUtil.parse(date));
+ });
+ }
+
+ @Data
+ @Accessors(chain = true)
+ public static class Tmdb {
+ private String id;
+ private String name;
+ private Date date;
+ }
}