From 2fb9c4cfd9480a89d2da389efbb2658f3a5fe78f Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 8 Jun 2019 02:02:58 +0700 Subject: [PATCH] Add anime-lists parser * https://github.com/ScudLee/anime-lists --- source/net/filebot/web/AnimeLists.java | 67 +++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/source/net/filebot/web/AnimeLists.java b/source/net/filebot/web/AnimeLists.java index 413444ca..8b0bcf24 100644 --- a/source/net/filebot/web/AnimeLists.java +++ b/source/net/filebot/web/AnimeLists.java @@ -1,10 +1,15 @@ package net.filebot.web; +import static java.util.Arrays.*; +import static java.util.stream.Collectors.*; import static net.filebot.CachedResource.*; import static net.filebot.util.StringUtilities.*; import java.io.ByteArrayInputStream; import java.net.URL; +import java.util.List; +import java.util.Locale; +import java.util.Optional; import javax.xml.bind.JAXBContext; import javax.xml.bind.annotation.XmlAttribute; @@ -18,17 +23,55 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import net.filebot.Cache; import net.filebot.CacheType; import net.filebot.Resource; +import net.filebot.WebServices; public enum AnimeLists { AniDB, TheTVDB; - public static final Resource MODEL = Resource.lazy(AnimeLists::fetchModel); + public Optional map(Episode episode, AnimeLists destination) throws Exception { + return find(episode.getSeriesInfo().getId()).map(a -> { + Integer s = destination.getSeason(a); + Integer e = destination.getEpisodeNumber(a, episode.getEpisode()); + + return episode.derive(s, e); + }); + } + + protected Integer getSeason(Entry a) { + return this == AniDB ? null : a.defaulttvdbseason; + } + + protected Integer getEpisodeNumber(Entry a, Integer e) { + if (a.episodeoffset != null) { + return this == AniDB ? e - a.episodeoffset : e + a.episodeoffset; + } + + return e; + } + + public Optional map(int id, AnimeLists destination) throws Exception { + return find(id).map(destination::getId); + } + + public Optional find(int id) throws Exception { + return stream(MODEL.get().anime).filter(this::isValid).filter(a -> id == getId(a)).findFirst(); + } + + protected int getId(Entry a) { + return this == AniDB ? a.anidbid : a.tvdbid; + } + + protected boolean isValid(Entry a) { + return a.anidbid != null && a.tvdbid != null; + } protected static Cache getCache() { return Cache.getCache("animelists", CacheType.Monthly); } + protected static final Resource MODEL = Resource.lazy(AnimeLists::fetchModel); + protected static Model fetchModel() throws Exception { Cache cache = getCache(); @@ -102,8 +145,28 @@ public enum AnimeLists { } } + public static List names() { + return stream(values()).map(Enum::name).collect(toList()); + } + + public static AnimeLists forName(String name) { + for (AnimeLists db : values()) { + if (db.name().equalsIgnoreCase(name)) { + return db; + } + } + + throw new IllegalArgumentException(String.format("%s not in %s", name, asList(values()))); + } + public static void main(String[] args) throws Exception { - System.out.println(AnimeLists.MODEL.get()); + System.out.println(AnimeLists.AniDB.map(14444, AnimeLists.TheTVDB)); + + List episodes = WebServices.AniDB.getEpisodeList(14444, SortOrder.Absolute, Locale.ENGLISH); + for (Episode episode : episodes) { + System.out.println(AnimeLists.AniDB.map(episode, AnimeLists.TheTVDB)); + } + System.exit(0); }