Refactor
This commit is contained in:
parent
bbed902c63
commit
a0ebae1db2
|
@ -6,6 +6,7 @@ import static net.filebot.Logging.*;
|
|||
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.cedarsoftware.util.io.JsonObject;
|
||||
import com.cedarsoftware.util.io.JsonReader;
|
||||
|
@ -26,15 +27,15 @@ public class JsonUtilities {
|
|||
}
|
||||
|
||||
public static Object[] asArray(Object node) {
|
||||
if (node instanceof Object[]) {
|
||||
return (Object[]) node;
|
||||
}
|
||||
if (node instanceof JsonObject) {
|
||||
JsonObject<?, ?> jsonObject = (JsonObject<?, ?>) node;
|
||||
if (jsonObject.isArray()) {
|
||||
return jsonObject.getArray();
|
||||
}
|
||||
}
|
||||
if (node instanceof Object[]) {
|
||||
return (Object[]) node;
|
||||
}
|
||||
return EMPTY_ARRAY;
|
||||
}
|
||||
|
||||
|
@ -42,6 +43,10 @@ public class JsonUtilities {
|
|||
return stream(asArray(node)).map(JsonUtilities::asMap).filter(m -> m.size() > 0).toArray(Map[]::new);
|
||||
}
|
||||
|
||||
public static Stream<Map<?, ?>> streamJsonObjects(Object node) {
|
||||
return stream(asMapArray(node));
|
||||
}
|
||||
|
||||
public static Object[] getArray(Object node, String key) {
|
||||
return asArray(asMap(node).get(key));
|
||||
}
|
||||
|
@ -54,6 +59,10 @@ public class JsonUtilities {
|
|||
return asMapArray(asMap(node).get(key));
|
||||
}
|
||||
|
||||
public static Stream<Map<?, ?>> streamJsonObjects(Object node, String key) {
|
||||
return stream(getMapArray(node, key));
|
||||
}
|
||||
|
||||
public static Map<?, ?> getFirstMap(Object node, String key) {
|
||||
Object[] values = getArray(node, key);
|
||||
if (values.length > 0) {
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package net.filebot.web;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.*;
|
||||
import static java.util.Arrays.*;
|
||||
import static net.filebot.util.JsonUtilities.*;
|
||||
import static net.filebot.web.WebRequest.*;
|
||||
|
||||
|
@ -116,7 +115,7 @@ public class AcoustIDClient implements MusicIdentificationService {
|
|||
|
||||
for (Object result : getArray(data, "results")) {
|
||||
// pick most likely matching recording
|
||||
return stream(getMapArray(result, "recordings")).sorted((Map<?, ?> r1, Map<?, ?> r2) -> {
|
||||
return streamJsonObjects(result, "recordings").sorted((r1, r2) -> {
|
||||
Integer i1 = getInteger(r1, "duration");
|
||||
Integer i2 = getInteger(r2, "duration");
|
||||
return Double.compare(i1 == null ? Double.NaN : Math.abs(i1 - targetDuration), i2 == null ? Double.NaN : Math.abs(i2 - targetDuration));
|
||||
|
@ -140,7 +139,7 @@ public class AcoustIDClient implements MusicIdentificationService {
|
|||
return audioTrack; // default to simple music info if album data is undesirable
|
||||
}
|
||||
|
||||
for (Map<?, ?> release : asMapArray(releases)) {
|
||||
return streamJsonObjects(releases).map(release -> {
|
||||
AudioTrack thisRelease = audioTrack.clone();
|
||||
|
||||
try {
|
||||
|
@ -151,7 +150,7 @@ public class AcoustIDClient implements MusicIdentificationService {
|
|||
}
|
||||
|
||||
if (thisRelease.albumReleaseDate == null || thisRelease.albumReleaseDate.getTimeStamp() >= (audioTrack.albumReleaseDate == null ? Long.MAX_VALUE : audioTrack.albumReleaseDate.getTimeStamp())) {
|
||||
continue;
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<?, ?> medium = getFirstMap(release, "mediums");
|
||||
|
@ -178,10 +177,8 @@ public class AcoustIDClient implements MusicIdentificationService {
|
|||
// full info audio track
|
||||
return thisRelease;
|
||||
}
|
||||
}
|
||||
|
||||
// default to simple music info if extended info is not available
|
||||
return audioTrack;
|
||||
return null;
|
||||
}).filter(Objects::nonNull).findFirst().orElse(audioTrack); // default to simple music info if extended info is not available
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger(AcoustIDClient.class.getName()).log(Level.WARNING, e.getMessage(), e);
|
||||
return null;
|
||||
|
|
|
@ -30,7 +30,7 @@ import java.util.zip.GZIPInputStream;
|
|||
|
||||
import javax.swing.Icon;
|
||||
|
||||
import net.filebot.CacheManager;
|
||||
import net.filebot.Cache;
|
||||
import net.filebot.CacheType;
|
||||
import net.filebot.ResourceManager;
|
||||
|
||||
|
@ -79,7 +79,7 @@ public class AnidbClient extends AbstractEpisodeListProvider {
|
|||
|
||||
@Override
|
||||
public ResultCache getCache() {
|
||||
return new ResultCache(getName(), CacheManager.getInstance().getCache(getName(), CacheType.Weekly));
|
||||
return new ResultCache(getName(), Cache.getCache(getName(), CacheType.Weekly));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package net.filebot.web;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.*;
|
||||
import static java.util.Arrays.*;
|
||||
import static java.util.Collections.*;
|
||||
import static net.filebot.util.JsonUtilities.*;
|
||||
|
||||
|
@ -45,7 +44,7 @@ public class FanartTVClient {
|
|||
Object json = readJson(UTF_8.decode(data));
|
||||
|
||||
return asMap(json).entrySet().stream().flatMap(it -> {
|
||||
return stream(asMapArray(it.getValue())).map(item -> {
|
||||
return streamJsonObjects(it.getValue()).map(item -> {
|
||||
Map<FanartProperty, String> fields = new EnumMap<FanartProperty, String>(FanartProperty.class);
|
||||
fields.put(FanartProperty.type, it.getKey().toString());
|
||||
|
||||
|
|
|
@ -7,10 +7,8 @@ import static net.filebot.web.WebRequest.*;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import javax.swing.Icon;
|
||||
|
@ -62,7 +60,7 @@ public class TVMazeClient extends AbstractEpisodeListProvider {
|
|||
Object response = request("search/shows?q=" + encode(query, true));
|
||||
|
||||
// TODO: FUTURE WORK: consider adding TVmaze aka titles for each result, e.g. http://api.tvmaze.com/shows/1/akas
|
||||
return stream(asMapArray(response)).map(it -> {
|
||||
return streamJsonObjects(response).map(it -> {
|
||||
Object show = it.get("show");
|
||||
Integer id = getInteger(show, "id");
|
||||
String name = getString(show, "name");
|
||||
|
@ -95,24 +93,19 @@ public class TVMazeClient extends AbstractEpisodeListProvider {
|
|||
|
||||
@Override
|
||||
protected SeriesData fetchSeriesData(SearchResult searchResult, SortOrder sortOrder, Locale locale) throws Exception {
|
||||
TVMazeSearchResult show = (TVMazeSearchResult) searchResult;
|
||||
|
||||
SeriesInfo seriesInfo = fetchSeriesInfo(show, sortOrder, locale);
|
||||
List<Episode> episodes = new ArrayList<Episode>(25);
|
||||
SeriesInfo seriesInfo = fetchSeriesInfo((TVMazeSearchResult) searchResult, sortOrder, locale);
|
||||
|
||||
// e.g. http://api.tvmaze.com/shows/1/episodes
|
||||
Object response = request("shows/" + show.getId() + "/episodes");
|
||||
Object response = request("shows/" + seriesInfo.getId() + "/episodes");
|
||||
|
||||
for (Map<?, ?> episode : asMapArray(response)) {
|
||||
List<Episode> episodes = streamJsonObjects(response).map(episode -> {
|
||||
String episodeTitle = getString(episode, "name");
|
||||
Integer seasonNumber = getInteger(episode, "season");
|
||||
Integer episodeNumber = getInteger(episode, "number");
|
||||
SimpleDate airdate = getStringValue(episode, "airdate", SimpleDate::parse);
|
||||
|
||||
if (episodeNumber != null && episodeTitle != null) {
|
||||
episodes.add(new Episode(seriesInfo.getName(), seasonNumber, episodeNumber, episodeTitle, null, null, airdate, new SeriesInfo(seriesInfo)));
|
||||
}
|
||||
}
|
||||
return new Episode(seriesInfo.getName(), seasonNumber, episodeNumber, episodeTitle, null, null, airdate, seriesInfo);
|
||||
}).collect(toList());
|
||||
|
||||
return new SeriesData(seriesInfo, episodes);
|
||||
}
|
||||
|
|
|
@ -5,9 +5,6 @@ import static org.junit.Assert.*;
|
|||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import net.sf.ehcache.CacheManager;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -132,10 +129,4 @@ public class AnidbClientTest {
|
|||
assertEquals("http://anidb.net/a1539", anidb.getEpisodeListLink(monsterSearchResult).toURL().toString());
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
@AfterClass
|
||||
public static void clearCache() {
|
||||
CacheManager.getInstance().clearAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,14 +1,11 @@
|
|||
package net.filebot.web;
|
||||
|
||||
import static net.filebot.web.EpisodeUtilities.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import net.sf.ehcache.CacheManager;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TVMazeClientTest {
|
||||
|
@ -32,7 +29,7 @@ public class TVMazeClientTest {
|
|||
|
||||
@Test
|
||||
public void getEpisodeList() throws Exception {
|
||||
List<Episode> list = EpisodeUtilities.filterBySeason(client.getEpisodeList(buffySearchResult, SortOrder.Airdate, Locale.ENGLISH), 7);
|
||||
List<Episode> list = filterBySeason(client.getEpisodeList(buffySearchResult, SortOrder.Airdate, Locale.ENGLISH), 7);
|
||||
|
||||
assertEquals(22, list.size());
|
||||
|
||||
|
@ -68,10 +65,4 @@ public class TVMazeClientTest {
|
|||
assertEquals("http://www.tvmaze.com/shows/427", client.getEpisodeListLink(buffySearchResult).toString());
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
@AfterClass
|
||||
public static void clearCache() {
|
||||
CacheManager.getInstance().clearAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,10 +10,7 @@ import java.util.Map;
|
|||
|
||||
import net.filebot.web.TheTVDBClient.BannerDescriptor;
|
||||
import net.filebot.web.TheTVDBClient.MirrorType;
|
||||
import net.sf.ehcache.CacheManager;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TheTVDBClientTest {
|
||||
|
@ -180,10 +177,4 @@ public class TheTVDBClientTest {
|
|||
assertEquals(486993, WebRequest.fetch(banners.get(0).getUrl()).remaining(), 0);
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
@AfterClass
|
||||
public static void clearCache() {
|
||||
CacheManager.getInstance().clearAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue