Skip to content

Commit

Permalink
仅在主RSS更新后删除备用RSS
Browse files Browse the repository at this point in the history
  • Loading branch information
wushuo894 committed Oct 27, 2024
1 parent e42e9b8 commit cb4f28c
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 67 deletions.
13 changes: 9 additions & 4 deletions src/main/java/ani/rss/download/Aria2.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,21 @@ public Boolean download(Item item, String savePath, File torrentFile, Boolean ov
}

@Override
public void delete(TorrentsInfo torrentsInfo) {
public Boolean delete(TorrentsInfo torrentsInfo) {
String host = config.getHost();
String password = config.getPassword();
String id = torrentsInfo.getId();
String body = ResourceUtil.readUtf8Str("aria2/removeDownloadResult.json");
body = StrFormatter.format(body, password, id);

HttpReq.post(host + "/jsonrpc", false)
.body(body)
.thenFunction(HttpResponse::isOk);
try {
return HttpReq.post(host + "/jsonrpc", false)
.body(body)
.thenFunction(HttpResponse::isOk);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/ani/rss/download/BaseDownload.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public interface BaseDownload {

Cache<String, String> renameCache = CacheUtil.newFIFOCache(512);

String tag = "ani-rss";

List<String> videoFormat = List.of("mp4", "mkv", "avi", "wmv");

List<String> subtitleFormat = List.of("ass", "ssa", "sub", "srt", "lyc");
Expand Down Expand Up @@ -50,7 +48,7 @@ public interface BaseDownload {
*
* @param torrentsInfo
*/
void delete(TorrentsInfo torrentsInfo);
Boolean delete(TorrentsInfo torrentsInfo);

/**
* 重命名
Expand Down
42 changes: 29 additions & 13 deletions src/main/java/ani/rss/download/Transmission.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ani.rss.entity.Config;
import ani.rss.entity.Item;
import ani.rss.entity.TorrentsInfo;
import ani.rss.enums.TorrentsTags;
import ani.rss.util.ExceptionUtil;
import ani.rss.util.GsonStatic;
import ani.rss.util.HttpReq;
Expand Down Expand Up @@ -95,7 +96,7 @@ public List<TorrentsInfo> getTorrentsInfos() {
JsonObject item = jsonElement.getAsJsonObject();
List<String> tags = item.get("labels").getAsJsonArray()
.asList().stream().map(JsonElement::getAsString).collect(Collectors.toList());
if (!tags.contains(tag)) {
if (!tags.contains(TorrentsTags.ANI_RSS.getValue())) {
continue;
}
List<String> files = item.get("files").getAsJsonArray().asList()
Expand Down Expand Up @@ -135,20 +136,29 @@ public List<TorrentsInfo> getTorrentsInfos() {
@Override
public Boolean download(Item item, String savePath, File torrentFile, Boolean ova) {
String name = item.getReName();
Boolean master = item.getMaster();
String subgroup = item.getSubgroup();
subgroup = StrUtil.blankToDefault(subgroup, "未知字幕组");
String body = ResourceUtil.readUtf8Str("transmission/torrent-add.json");
String extName = FileUtil.extName(torrentFile);
if (StrUtil.isBlank(extName)) {
return false;
}
String torrent = "";

List<String> tags = new ArrayList<>();
tags.add(TorrentsTags.ANI_RSS.getValue());
tags.add(subgroup);
if (!master) {
tags.add(TorrentsTags.BACK_RSS.getValue());
}

String torrent;
if ("txt".equals(extName)) {
torrent = FileUtil.readUtf8String(torrentFile);
body = StrFormatter.format(body, tag, subgroup, savePath, "", torrent);
body = StrFormatter.format(body, GsonStatic.toJson(tags), savePath, "", torrent);
} else {
torrent = Base64.encode(torrentFile);
body = StrFormatter.format(body, tag, subgroup, savePath, torrent, "");
body = StrFormatter.format(body, GsonStatic.toJson(tags), savePath, torrent, "");
}

String id = HttpReq.post(host + "/transmission/rpc", false)
Expand Down Expand Up @@ -191,14 +201,19 @@ public Boolean download(Item item, String savePath, File torrentFile, Boolean ov
}

@Override
public void delete(TorrentsInfo torrentsInfo) {
public Boolean delete(TorrentsInfo torrentsInfo) {
String body = ResourceUtil.readUtf8Str("transmission/torrent-remove.json");
body = StrFormatter.format(body, torrentsInfo.getId());
HttpReq.post(host + "/transmission/rpc", false)
.header(Header.AUTHORIZATION, authorization)
.header("X-Transmission-Session-Id", sessionId)
.body(body)
.then(HttpResponse::isOk);
try {
return HttpReq.post(host + "/transmission/rpc", false)
.header(Header.AUTHORIZATION, authorization)
.header("X-Transmission-Session-Id", sessionId)
.body(body)
.thenFunction(HttpResponse::isOk);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

@Override
Expand Down Expand Up @@ -233,10 +248,11 @@ public void rename(TorrentsInfo torrentsInfo) {
}

@Override
public Boolean addTags(TorrentsInfo torrentsInfo, String tags) {
public Boolean addTags(TorrentsInfo torrentsInfo, String tag) {
String id = torrentsInfo.getId();
List<String> strings = new ArrayList<>(List.of(torrentsInfo.getTags().split(",")));
strings.add(tags);
String tags = torrentsInfo.getTags();
List<String> strings = new ArrayList<>(StrUtil.split(tags, ",", true, true));
strings.add(tag);

String body = ResourceUtil.readUtf8Str("transmission/torrent-set.json");
body = StrFormatter.format(body, GsonStatic.toJson(strings), id);
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/ani/rss/download/qBittorrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ani.rss.entity.Item;
import ani.rss.entity.TorrentsInfo;
import ani.rss.enums.StringEnum;
import ani.rss.enums.TorrentsTags;
import ani.rss.util.ExceptionUtil;
import ani.rss.util.GsonStatic;
import ani.rss.util.HttpReq;
Expand Down Expand Up @@ -84,7 +85,7 @@ public List<TorrentsInfo> getTorrentsInfos() {
continue;
}
// 包含标签
if (StrUtil.split(tags, ",", true, true).contains(tag)) {
if (StrUtil.split(tags, ",", true, true).contains(TorrentsTags.ANI_RSS.getValue())) {
torrentsInfoList.add(torrentsInfo);
}
}
Expand All @@ -95,10 +96,19 @@ public List<TorrentsInfo> getTorrentsInfos() {
@Override
public Boolean download(Item item, String savePath, File torrentFile, Boolean ova) {
String name = item.getReName();
Boolean master = item.getMaster();
String subgroup = item.getSubgroup();
subgroup = StrUtil.blankToDefault(subgroup, "未知字幕组");
String host = config.getHost();
Boolean qbUseDownloadPath = config.getQbUseDownloadPath();

List<String> tags = new ArrayList<>();
tags.add(TorrentsTags.ANI_RSS.getValue());
tags.add(subgroup);
if (!master) {
tags.add(TorrentsTags.BACK_RSS.getValue());
}

HttpRequest httpRequest = HttpReq.post(host + "/api/v2/torrents/add", false)
.form("addToTopOfQueue", false)
.form("autoTMM", false)
Expand All @@ -113,7 +123,7 @@ public Boolean download(Item item, String savePath, File torrentFile, Boolean ov
.form("stopCondition", "None")
.form("upLimit", 102400)
.form("useDownloadPath", qbUseDownloadPath)
.form("tags", "ani-rss," + subgroup);
.form("tags", CollUtil.join(tags, ","));

String extName = FileUtil.extName(torrentFile);
if ("txt".equals(extName)) {
Expand Down Expand Up @@ -150,13 +160,18 @@ public Boolean download(Item item, String savePath, File torrentFile, Boolean ov
}

@Override
public void delete(TorrentsInfo torrentsInfo) {
public Boolean delete(TorrentsInfo torrentsInfo) {
String host = config.getHost();
String hash = torrentsInfo.getHash();
HttpReq.post(host + "/api/v2/torrents/delete", false)
.form("hashes", hash)
.form("deleteFiles", false)
.thenFunction(HttpResponse::isOk);
try {
return HttpReq.post(host + "/api/v2/torrents/delete", false)
.form("hashes", hash)
.form("deleteFiles", false)
.thenFunction(HttpResponse::isOk);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/ani/rss/entity/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public class Config implements Serializable {
*/
private Boolean delete;

/**
* 仅在主RSS更新后删除备用RSS
*/
private Boolean deleteBackRSSOnly;

/**
* 自动推断剧集偏移
*/
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/ani/rss/enums/ServerChanTypeEnum.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ani.rss.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum ServerChanTypeEnum {
SERVER_CHAN("serverChan", "server酱", "https://sctapi.ftqq.com/<sendKey>.send", "SCT"),
SERVER_CHAN_3("serverChan3", "server酱³", "https://<uid>.push.ft07.com/send/<sendKey>.send", "sct");
Expand All @@ -11,12 +13,4 @@ public enum ServerChanTypeEnum {
private final String name;
private final String url;
private final String sendkeyPrefix;

ServerChanTypeEnum(String type, String name, String url, String sendKeyPrefix) {
this.type = type;
this.name = name;
this.url = url;
this.sendkeyPrefix = sendKeyPrefix;
}

}
15 changes: 15 additions & 0 deletions src/main/java/ani/rss/enums/TorrentsTags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ani.rss.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum TorrentsTags {
ANI_RSS("ani-rss"),
RENAME("RENAME"),
BACK_RSS("备用RSS"),
DOWNLOAD_COMPLETE("下载完成");

private final String value;
}
4 changes: 4 additions & 0 deletions src/main/java/ani/rss/task/RenameTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void run() {
super.setName("rename-task-thread");
Config config = ConfigUtil.CONFIG;
double renameSleep = config.getRenameSleep();
Boolean deleteBackRSSOnly = config.getDeleteBackRSSOnly();

log.info("{} 当前设置间隔为 {} 分钟", getName(), renameSleep);
while (loop.get()) {
Expand All @@ -45,6 +46,9 @@ public void run() {
try {
TorrentUtil.rename(torrentsInfo);
TorrentUtil.notification(torrentsInfo);
if (deleteBackRSSOnly) {
continue;
}
TorrentUtil.delete(torrentsInfo);
} catch (Exception e) {
log.error(e.getMessage(), e);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/ani/rss/util/ConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class ConfigUtil {
.setFileExist(false)
.setAwaitStalledUP(true)
.setDelete(false)
.setDeleteBackRSSOnly(false)
.setOffset(false)
.setTitleYear(false)
.setAcronym(false)
Expand Down
Loading

0 comments on commit cb4f28c

Please sign in to comment.