From f03c05a14140537164558d5b8b56785e2f99e1e9 Mon Sep 17 00:00:00 2001 From: wushuo Date: Fri, 20 Dec 2024 02:51:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96tmdb?= =?UTF-8?q?=E6=A0=87=E9=A2=98=E6=97=B6=E5=B9=B4=E4=BB=BD=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UPDATE.md | 2 +- pom.xml | 2 +- src/main/java/ani/rss/util/TmdbUtil.java | 110 +++++++++++++---------- 3 files changed, 66 insertions(+), 48 deletions(-) diff --git a/UPDATE.md b/UPDATE.md index f44d4b3..65dc380 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 805b925..fa73783 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 80478e9..b217e85 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; + } }