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