001package net.filebot.format;
002
003import static java.util.stream.Collectors.*;
004import static net.filebot.WebServices.*;
005import static net.filebot.web.EpisodeUtilities.*;
006
007import java.util.List;
008import java.util.Locale;
009import java.util.Map;
010
011import groovy.lang.Script;
012
013import net.filebot.Cache;
014import net.filebot.WebServices;
015import net.filebot.web.Artwork;
016import net.filebot.web.ArtworkProvider;
017import net.filebot.web.Episode;
018import net.filebot.web.EpisodeDetails;
019import net.filebot.web.EpisodeListProvider;
020import net.filebot.web.Extra;
021import net.filebot.web.Movie;
022import net.filebot.web.MovieCollection;
023import net.filebot.web.MovieDetails;
024import net.filebot.web.Person;
025import net.filebot.web.Series;
026import net.filebot.web.SeriesDetails;
027import net.filebot.web.SeriesInfo;
028import net.filebot.web.SortOrder;
029import net.filebot.web.XDB;
030
031/**
032 * Groovy Extension Methods for Extended Metadata
033 */
034public class ExtendedMetadataMethods {
035
036        public static MovieDetails getInfo(Movie self) throws Exception {
037                return TheMovieDB.getMovieInfo(self, self.getLanguage(), true);
038        }
039
040        public static Map<String, String> getTranslations(Movie self) throws Exception {
041                if (self.getTmdbId() > 0) {
042                        return TheMovieDB.getTranslations(self.getTmdbId());
043                }
044                return null;
045        }
046
047        public static Map<String, List<String>> getAlternativeTitles(Movie self) throws Exception {
048                if (self.getTmdbId() > 0) {
049                        return TheMovieDB.getAlternativeTitles(self.getTmdbId());
050                }
051                return null;
052        }
053
054        public static MovieCollection getCollection(Movie self) throws Exception {
055                if (self.getTmdbId() > 0) {
056                        return TheMovieDB.getCollection(self.getTmdbId(), self.getLanguage());
057                }
058                return null;
059        }
060
061        public static List<Artwork> getArtwork(Movie self) throws Exception {
062                if (self.getTmdbId() > 0) {
063                        return TheMovieDB.getArtwork(self.getTmdbId(), self.getLanguage());
064                }
065                return null;
066        }
067
068        public static List<Artwork> getArtwork(Movie self, String category, Locale locale) throws Exception {
069                if (self.getTmdbId() > 0) {
070                        return TheMovieDB.getArtwork(self.getTmdbId(), category, locale);
071                }
072                return null;
073        }
074
075        public static List<Artwork> getFanart(Movie self) throws Exception {
076                if (self.getTmdbId() > 0) {
077                        return FanartTV.TheMovieDB.getArtwork(self.getTmdbId(), self.getLanguage());
078                }
079                return null;
080        }
081
082        public static List<Extra> getExtras(Movie self) throws Exception {
083                if (self.getTmdbId() > 0) {
084                        return TheMovieDB.getExtras(self.getTmdbId());
085                }
086                return null;
087        }
088
089        public static List<Episode> getEpisodes(SeriesInfo self) throws Exception {
090                for (EpisodeListProvider db : getEpisodeListProviders()) {
091                        if (isInstance(db, self)) {
092                                return db.getEpisodeList(self.getId(), SortOrder.forName(self.getOrder()), self.getLanguage());
093                        }
094                }
095                return null;
096        }
097
098        public static List<Artwork> getArtwork(SeriesInfo self) throws Exception {
099                for (EpisodeListProvider db : getEpisodeListProviders()) {
100                        if (isInstance(db, self) && db instanceof ArtworkProvider) {
101                                return ((ArtworkProvider) db).getArtwork(self.getId(), self.getLanguage());
102                        }
103                }
104                return null;
105        }
106
107        public static List<Artwork> getArtwork(SeriesInfo self, String category, Locale locale) throws Exception {
108                for (EpisodeListProvider db : getEpisodeListProviders()) {
109                        if (isInstance(db, self) && db instanceof ArtworkProvider) {
110                                return ((ArtworkProvider) db).getArtwork(self.getId(), category, locale);
111                        }
112                }
113                return null;
114        }
115
116        public static List<Artwork> getFanart(SeriesInfo self) throws Exception {
117                Integer sid = getExternalId(self, "TheTVDB");
118                if (sid != null) {
119                        return FanartTV.TheTVDB.getArtwork(sid, self.getLanguage()); // webservice.fanart.tv requires TheTVDB ID for lookup
120                }
121                return null;
122        }
123
124        public static SeriesInfo getSeries(Episode self) throws Exception {
125                SeriesInfo series = self.getSeriesInfo();
126                for (EpisodeListProvider db : getEpisodeListProviders()) {
127                        if (isInstance(db, series)) {
128                                return db.getSeriesInfo(series.getId(), series.getLanguage());
129                        }
130                }
131                return null;
132        }
133
134        public static SeriesDetails getDetails(SeriesInfo self) throws Exception {
135                for (EpisodeListProvider db : getEpisodeListProviders()) {
136                        if (isInstance(db, self)) {
137                                return (SeriesDetails) db.getSeriesInfo(self.getId(), self.getLanguage());
138                        }
139                }
140                return null;
141        }
142
143        public static EpisodeDetails getInfo(Episode self) throws Exception {
144                for (EpisodeListProvider db : getEpisodeListProviders()) {
145                        if (isInstance(db, self)) {
146                                return db.getEpisodeInfo(self, self.getSeriesInfo().getLanguage());
147                        }
148                }
149                return null;
150        }
151
152        public static List<String> getActors(SeriesInfo self) throws Exception {
153                List<Person> crew = getCrew(self);
154                if (crew != null) {
155                        return crew.stream().filter(Person::isActor).map(Person::getName).collect(toList());
156                }
157                return null;
158        }
159
160        public static List<Person> getCrew(SeriesInfo self) throws Exception {
161                if (isInstance(TheMovieDB_TV, self)) {
162                        return TheMovieDB_TV.getCredits(self.getId(), Locale.US);
163                }
164                if (isInstance(TheTVDB, self)) {
165                        return TheTVDB.getCharacters(self.getId(), Locale.US);
166                }
167                return null;
168        }
169
170        public static Integer getExternalId(Series self, String db) throws Exception {
171                return self.getExternalId(XDB.forName(db));
172        }
173
174        public static Integer getExternalId(SeriesInfo self, String db) throws Exception {
175                if (isInstance(TheMovieDB_TV, self)) {
176                        return XDB.TheMovieDB.getExternalId(self.getId(), XDB.forName(db));
177                }
178                if (isInstance(TheTVDB, self)) {
179                        return XDB.TheTVDB.getExternalId(self.getId(), XDB.forName(db));
180                }
181                if (isInstance(AniDB, self)) {
182                        return XDB.AniDB.getExternalId(self.getId(), XDB.forName(db));
183                }
184                return null;
185        }
186
187        public static SeriesInfo getExternalSeries(SeriesInfo self, String db) throws Exception {
188                switch (XDB.forName(db)) {
189                        case TheMovieDB:
190                                return TheMovieDB_TV.getSeriesInfo(getExternalId(self, db), self.getLanguage());
191                        case TheTVDB:
192                                return TheTVDB.getSeriesInfo(getExternalId(self, db), self.getLanguage());
193                        case AniDB:
194                                return AniDB.getSeriesInfo(getExternalId(self, db), self.getLanguage());
195                        case TVmaze:
196                                return TVmaze.getSeriesInfo(getExternalId(self, db), self.getLanguage());
197                        default:
198                                return null;
199                }
200        }
201
202        public static Map<String, String> getExternalIds(SeriesInfo self) throws Exception {
203                if (isInstance(TheMovieDB_TV, self)) {
204                        return TheMovieDB_TV.getExternalIds(self.getId());
205                }
206                if (isInstance(TheTVDB, self)) {
207                        return TheTVDB.getExternalIds(self.getId());
208                }
209                return null;
210        }
211
212        public static List<Extra> getExtras(SeriesInfo self) throws Exception {
213                if (isInstance(TheMovieDB_TV, self)) {
214                        return TheMovieDB_TV.getExtras(self.getId());
215                }
216                return null;
217        }
218
219        public static Object getRaw(SeriesInfo self) throws Exception {
220                if (isInstance(TheMovieDB_TV, self)) {
221                        return TheMovieDB_TV.requestSeriesInfo(self.getId(), self.getLanguage(), Cache.ONE_MONTH);
222                }
223                if (isInstance(TheTVDB, self)) {
224                        return TheTVDB.requestSeriesInfo(self.getId(), self.getLanguage());
225                }
226                return null;
227        }
228
229        public static Object getRaw(EpisodeDetails self) throws Exception {
230                if (isInstance(TheMovieDB_TV, self)) {
231                        return TheMovieDB_TV.requestEpisodeInfo(self.getSeriesInfo().getId(), self.getSeason(), self.getEpisode(), self.getSeriesInfo().getLanguage());
232                }
233                if (isInstance(TheTVDB, self)) {
234                        return TheTVDB.requestEpisodeInfo(self.getId(), self.getSeriesInfo().getLanguage());
235                }
236                return null;
237        }
238
239        public static Object getService(Script context, String name) {
240                return WebServices.getService(name);
241        }
242
243}