Skip to content

Commit

Permalink
修复获取tmdb标题时年份异常的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wushuo894 committed Dec 19, 2024
1 parent 74c550f commit f03c05a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 48 deletions.
2 changes: 1 addition & 1 deletion UPDATE.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
预览时可以显示缺少的集数
修复获取tmdb标题时年份异常的问题
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ani.rss</groupId>
<artifactId>ani-rss</artifactId>
<version>1.1.212</version>
<version>1.1.213</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
110 changes: 64 additions & 46 deletions src/main/java/ani/rss/util/TmdbUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,70 +34,86 @@ public synchronized static String getName(String name, String type) {
if (StrUtil.isBlank(name)) {
return "";
}
AtomicReference<String> 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();
}
if (StrUtil.isBlank(name)) {
return "";
}
Config config = ConfigUtil.CONFIG;
String tmdbLanguage = config.getTmdbLanguage();
AtomicReference<String> 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<JsonObject> 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<JsonObject> 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;
}
}

0 comments on commit f03c05a

Please sign in to comment.