Skip to content

Commit

Permalink
fix: sonarr v3
Browse files Browse the repository at this point in the history
  • Loading branch information
nekolr committed Jan 1, 2024
1 parent cff7181 commit 1a0beae
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 122 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,31 @@


import com.github.nekolr.peashooter.api.sonarr.req.AddRssIndexer;
import com.github.nekolr.peashooter.api.sonarr.rsp.Queue;
import com.github.nekolr.peashooter.api.sonarr.rsp.Series;
import com.github.nekolr.peashooter.api.sonarr.rsp.Status;

import java.util.List;

public interface SonarrV3Api {

String ADD_INDEXER_URI = "/api/v3/indexer";

String GET_STATUS_URI = "/api/v3/system/status";

String GET_SERIES_LIST_URI = "/api/v3/series";

String GET_SERIES_URI = "/api/v3/series/{0}";

String GET_QUEUE_LIST_URI = "/api/queue";

List<Queue> getQueueList();

Boolean addRssIndexer(AddRssIndexer indexer);

Status getStatus();

List<Series> getSeriesList();

Series getSeries(String id);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package com.github.nekolr.peashooter.api.sonarr.client;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.github.nekolr.peashooter.api.sonarr.SonarrV3Api;
import com.github.nekolr.peashooter.api.sonarr.req.AddRssIndexer;
import com.github.nekolr.peashooter.api.sonarr.rsp.Queue;
import com.github.nekolr.peashooter.api.sonarr.rsp.Series;
import com.github.nekolr.peashooter.api.sonarr.rsp.Status;
import com.github.nekolr.peashooter.config.SettingsManager;
import jodd.http.HttpException;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.List;

import static com.github.nekolr.peashooter.constant.Peashooter.API_KEY;

@Slf4j
Expand All @@ -19,6 +29,18 @@ public class SonarrV3Client implements SonarrV3Api {

private final SettingsManager settingsManager;

@Override
public List<Queue> getQueueList() {
String apiKey = settingsManager.get().getSonarr().getApiKey();
HttpRequest request = HttpRequest.get(this.getUrl(GET_QUEUE_LIST_URI));
request.query(API_KEY, apiKey);
HttpResponse response = request.send();
if (response.statusCode() != 200)
return null;
JSONObject respObj = JSON.parseObject(response.bodyText());
return JSON.parseArray(respObj.getString("records"), Queue.class);
}

@Override
public Boolean addRssIndexer(AddRssIndexer indexer) {
String apiKey = settingsManager.get().getSonarr().getApiKey();
Expand All @@ -34,6 +56,46 @@ public Boolean addRssIndexer(AddRssIndexer indexer) {
}
}

@Override
public Status getStatus() {
String apiKey = settingsManager.get().getSonarr().getApiKey();
HttpRequest request = HttpRequest.get(this.getUrl(GET_STATUS_URI));
request.query(API_KEY, apiKey);
try {
HttpResponse response = request.send();
if (response.statusCode() != 200)
return null;
return JSON.parseObject(response.bodyText(), Status.class);
} catch (Exception e) {
log.error("get status error: {}", e.getMessage());
return null;
}
}

@Override
public List<Series> getSeriesList() {
String apiKey = settingsManager.get().getSonarr().getApiKey();
HttpRequest request = HttpRequest.get(this.getUrl(GET_SERIES_LIST_URI));
request.query(API_KEY, apiKey);
HttpResponse response = request.send();
if (response.statusCode() != 200)
return null;
return JSON.parseArray(response.bodyText(), Series.class);
}

@Override
@Retryable(retryFor = HttpException.class, backoff = @Backoff(multiplier = 1.5))
public Series getSeries(String id) {
String apiKey = settingsManager.get().getSonarr().getApiKey();
String uri = MessageFormat.format(GET_SERIES_URI, id);
HttpRequest request = HttpRequest.get(this.getUrl(uri));
request.query(API_KEY, apiKey);
HttpResponse response = request.send();
if (response.statusCode() != 200)
return null;
return JSON.parseObject(response.bodyText(), Series.class);
}

private String getUrl(String uri) {
return settingsManager.get().getSonarr().getUrl() + uri;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.nekolr.peashooter.api.sonarr.rsp;

public record Series(String id, String title, Integer seasonCount, Long tvdbId,
public record Series(String id, String title, Long tvdbId,
String imdbId, String languageProfileId, String qualityProfileId) {

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.nekolr.peashooter.job.rename;

import com.github.nekolr.peashooter.api.qb.QBittorrentApi;
import com.github.nekolr.peashooter.api.sonarr.SonarrApi;
import com.github.nekolr.peashooter.api.sonarr.SonarrV3Api;
import com.github.nekolr.peashooter.api.sonarr.rsp.Queue;
import com.github.nekolr.peashooter.entity.domain.DownloadInfo;
import com.github.nekolr.peashooter.service.IDownloadInfoService;
Expand All @@ -20,14 +20,14 @@
@RequiredArgsConstructor
public class RenameTorrentJob extends QuartzJobBean {

private final SonarrApi sonarrApi;
private final SonarrV3Api sonarrV3Api;
private final QBittorrentApi qBittorrentApi;
private final IDownloadInfoService downloadInfoService;

@Override
protected void executeInternal(JobExecutionContext context) {
List<DownloadInfo> downloadInfoList = downloadInfoService.findAll();
List<Queue> queueList = sonarrApi.getQueueList();
List<Queue> queueList = sonarrV3Api.getQueueList();
for (DownloadInfo info : downloadInfoList) {
if (!CollectionUtils.isEmpty(queueList)) {
for (Queue queue : queueList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.nekolr.peashooter.rss.convert;

import com.github.nekolr.peashooter.api.sonarr.SonarrApi;
import com.github.nekolr.peashooter.api.sonarr.SonarrV3Api;
import com.github.nekolr.peashooter.api.sonarr.rsp.Series;
import com.github.nekolr.peashooter.config.SettingsManager;
import com.github.nekolr.peashooter.constant.Peashooter;
Expand Down Expand Up @@ -38,7 +38,8 @@ public class RssConvertorImpl implements RssConvertor {
private static final String TORRENT_URI_PARAM_TITLE = "title";
private static final String TORRENT_URI_PARAM_EPISODE = "episode";
private static final String TORRENT_URI_PARAM_SEASON = "season";
private final SonarrApi sonarrApi;

private final SonarrV3Api sonarrV3Api;
private final SettingsManager settingsManager;
private Map<String, PubDateResolver> resolverMap;

Expand Down Expand Up @@ -78,7 +79,7 @@ public Item convert(SyndEntry entry, ConvertContext ctx) {
Item item = null;
String epTitle = FeedUtils.getTitle(entry);
List<Matcher> matchers = ctx.matchers();
Series series = sonarrApi.getSeries(ctx.referenceId());
Series series = sonarrV3Api.getSeries(ctx.referenceId());
if (Objects.isNull(series)) {
log.warn("转换分组 {} 失败,Entry Title:{}", ctx.groupId(), epTitle);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.nekolr.peashooter.api.qb.QBittorrentApi;
import com.github.nekolr.peashooter.api.qb.rsp.AppVersion;
import com.github.nekolr.peashooter.api.sonarr.SonarrApi;
import com.github.nekolr.peashooter.api.sonarr.SonarrV3Api;
import com.github.nekolr.peashooter.api.sonarr.rsp.Status;
import com.github.nekolr.peashooter.config.Settings;
import com.github.nekolr.peashooter.config.Settings.*;
Expand All @@ -19,13 +19,13 @@
@RequiredArgsConstructor
public class SettingsServiceImpl implements ISettingsService {

private final SonarrApi sonarrApi;
private final SonarrV3Api sonarrV3Api;
private final QBittorrentApi qBittorrentApi;
private final SettingsManager settingsManager;

@Override
public boolean testSonarr() {
Status status = sonarrApi.getStatus();
Status status = sonarrV3Api.getStatus();
return Objects.nonNull(status);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.nekolr.peashooter.service.impl;

import com.github.nekolr.peashooter.api.sonarr.SonarrApi;
import com.github.nekolr.peashooter.api.sonarr.SonarrV3Api;
import com.github.nekolr.peashooter.api.sonarr.req.AddRssIndexer;
import com.github.nekolr.peashooter.api.sonarr.rsp.Series;
Expand Down Expand Up @@ -31,15 +30,14 @@ public class SonarrServiceImpl implements ISonarrService {

private Map<String, SeriesNameDto> sonarrSeries = new ConcurrentHashMap();

private final SonarrApi sonarrApi;
private final SonarrV3Api sonarrV3Api;
private final TheMovieDbApi theMovieDbApi;
private final SettingsManager settingsManager;
private final ISeriesNameService seriesNameService;

@Override
public List<SeriesNameDto> getSeriesNameList() {
List<Series> seriesList = sonarrApi.getSeriesList();
List<Series> seriesList = sonarrV3Api.getSeriesList();
if (CollectionUtils.isEmpty(seriesList)) {
return Collections.emptyList();
} else {
Expand All @@ -54,7 +52,7 @@ public List<SeriesNameDto> getSeriesNameList() {
@Override
public List<SeriesNameDto> refreshSeriesName() {
log.info("开始刷新 sonarr 的剧集中文信息");
List<Series> seriesList = sonarrApi.getSeriesList();
List<Series> seriesList = sonarrV3Api.getSeriesList();
if (CollectionUtils.isEmpty(seriesList)) {
log.info("没有获取到 sonarr 的剧集信息");
return Collections.emptyList();
Expand Down Expand Up @@ -98,7 +96,7 @@ public List<SeriesNameDto> refreshSeriesName() {
public void syncSeriesLatest() {
final long REFRESH_COUNT = 100;
log.info("重新同步 sonarr 最近 {} 部剧集的中文信息", REFRESH_COUNT);
List<Series> seriesList = sonarrApi.getSeriesList();
List<Series> seriesList = sonarrV3Api.getSeriesList();
if (CollectionUtils.isEmpty(seriesList)) {
log.info("没有获取到 sonarr 的剧集信息");
} else {
Expand Down

0 comments on commit 1a0beae

Please sign in to comment.