From 1ebe2a350c5288eff988663c5291084a908461c3 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 19 May 2019 13:16:14 +0700 Subject: [PATCH] Refactor Manami --- source/net/filebot/web/Manami.java | 91 ++++++++++++------------------ 1 file changed, 36 insertions(+), 55 deletions(-) diff --git a/source/net/filebot/web/Manami.java b/source/net/filebot/web/Manami.java index 20e8edb1..7b0a7291 100644 --- a/source/net/filebot/web/Manami.java +++ b/source/net/filebot/web/Manami.java @@ -5,89 +5,70 @@ import static java.util.stream.Collectors.*; import static net.filebot.CachedResource.*; import static net.filebot.util.JsonUtilities.*; -import java.net.URI; import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Stream; -import javax.swing.Icon; - import net.filebot.Cache; import net.filebot.CacheType; import net.filebot.Resource; -public class Manami implements Datasource { +public enum Manami implements ArtworkProvider { - public static final Manami INSTANCE = new Manami(); + AniDB; + + public String getURI(int id) { + return "https://anidb.net/a" + id; + } - @Override public String getIdentifier() { - return "Minami"; + return name().toLowerCase(); } @Override - public Icon getIcon() { - return null; + public List getArtwork(int id, String category, Locale locale) throws Exception { + List artwork = new ArrayList(1); + + Optional picture = getRecord(id).map(r -> getString(r, "picture")).filter(r -> r.endsWith(".jpg")); + if (picture.isPresent()) { + artwork.add(new Artwork(Stream.of("picture"), new URL(picture.get()), null, null)); + } + + return artwork; } - protected Cache getCache() { - return Cache.getCache(getIdentifier(), CacheType.Persistent); - } + public Optional> getRecord(int id) throws Exception { + String uri = getURI(id); - protected Object request(String file) throws Exception { - return getCache().json(file, this::getResource).fetch(fetchIfNoneMatch(URL::getPath, getCache())).expire(Cache.ONE_MONTH).get(); - } - - protected URL getResource(String file) throws Exception { - return new URL("https://raw.githubusercontent.com/manami-project/anime-offline-database/master/" + file); - } - - protected Object getDatabase() throws Exception { - return request("anime-offline-database.json"); - } - - protected Object getDeadEntries() throws Exception { - return request("dead-entries.json"); - } - - protected final Resource database = Resource.lazy(this::getDatabase); - protected final Resource deadEntries = Resource.lazy(this::getDeadEntries); - - public Stream> getRecords() throws Exception { - return streamJsonObjects(database.get(), "data"); - } - - public Optional> getRecord(String uri) throws Exception { return getRecords().filter(r -> { return stream(getArray(r, "sources")).anyMatch(uri::equals); }).findFirst(); } - public Optional getPicture(String uri) throws Exception { - return getRecord(uri).map(r -> getStringValue(r, "picture", URI::create)).filter(r -> r.getPath().endsWith(".jpg")); + public Stream> getRecords() throws Exception { + return streamJsonObjects(database.get(), "data"); } - public Set getDeadEntries(Source source) throws Exception { - return stream(getArray(deadEntries.get(), source.getIdentifier())).map(Object::toString).map(Integer::parseInt).collect(toSet()); + public Set getDeadEntries() throws Exception { + return stream(getArray(deadEntries.get(), getIdentifier())).map(Object::toString).map(Integer::parseInt).collect(toSet()); } - public enum Source { - - AniDB; - - public String getURI(int id) { - switch (this) { - case AniDB: - return "https://anidb.net/a" + id; - } - return null; - } - - public String getIdentifier() { - return name().toLowerCase(); - } + protected static Cache getCache() { + return Cache.getCache("manami", CacheType.Persistent); } + protected static Object request(String file) throws Exception { + return getCache().json(file, f -> { + return new URL("https://raw.githubusercontent.com/manami-project/anime-offline-database/master/" + f); + }).fetch(fetchIfNoneMatch(URL::getPath, getCache())).expire(Cache.ONE_MONTH).get(); + } + + protected static final Resource database = Resource.lazy(() -> request("anime-offline-database.json")); + protected static final Resource deadEntries = Resource.lazy(() -> request("dead-entries.json")); + }