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.EpisodeInfo;
019import net.filebot.web.EpisodeListProvider;
020import net.filebot.web.Extra;
021import net.filebot.web.Movie;
022import net.filebot.web.MovieCollection;
023import net.filebot.web.MovieInfo;
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 MovieInfo 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<Extra> getExtras(Movie self) throws Exception {
076                if (self.getTmdbId() > 0) {
077                        return TheMovieDB.getExtras(self.getTmdbId());
078                }
079                return null;
080        }
081
082        public static List<Episode> getEpisodes(SeriesInfo self) throws Exception {
083                for (EpisodeListProvider db : getEpisodeListProviders()) {
084                        if (isInstance(db, self)) {
085                                return db.getEpisodeList(self.getId(), SortOrder.forName(self.getOrder()), self.getLanguage());
086                        }
087                }
088                return null;
089        }
090
091        public static List<Artwork> getArtwork(SeriesInfo self) throws Exception {
092                for (EpisodeListProvider db : getEpisodeListProviders()) {
093                        if (isInstance(db, self) && db instanceof ArtworkProvider) {
094                                return ((ArtworkProvider) db).getArtwork(self.getId(), self.getLanguage());
095                        }
096                }
097                return null;
098        }
099
100        public static List<Artwork> getArtwork(SeriesInfo self, String category, Locale locale) throws Exception {
101                for (EpisodeListProvider db : getEpisodeListProviders()) {
102                        if (isInstance(db, self) && db instanceof ArtworkProvider) {
103                                return ((ArtworkProvider) db).getArtwork(self.getId(), category, locale);
104                        }
105                }
106                return null;
107        }
108
109        public static SeriesInfo getSeries(Episode self) throws Exception {
110                SeriesInfo series = self.getSeriesInfo();
111                for (EpisodeListProvider db : getEpisodeListProviders()) {
112                        if (isInstance(db, series)) {
113                                return db.getSeriesInfo(series.getId(), series.getLanguage());
114                        }
115                }
116                return null;
117        }
118
119        public static SeriesDetails getDetails(SeriesInfo self) throws Exception {
120                for (EpisodeListProvider db : getEpisodeListProviders()) {
121                        if (isInstance(db, self)) {
122                                return (SeriesDetails) db.getSeriesInfo(self.getId(), self.getLanguage());
123                        }
124                }
125                return null;
126        }
127
128        public static EpisodeInfo getInfo(Episode self) throws Exception {
129                for (EpisodeListProvider db : getEpisodeListProviders()) {
130                        if (isInstance(db, self)) {
131                                return db.getEpisodeInfo(self, self.getSeriesInfo().getLanguage());
132                        }
133                }
134                return null;
135        }
136
137        public static List<String> getActors(SeriesInfo self) throws Exception {
138                List<Person> crew = getCrew(self);
139                if (crew != null) {
140                        return crew.stream().filter(Person::isActor).map(Person::getName).collect(toList());
141                }
142                return null;
143        }
144
145        public static List<Person> getCrew(SeriesInfo self) throws Exception {
146                if (isInstance(TheMovieDB_TV, self)) {
147                        return TheMovieDB_TV.getCredits(self.getId(), Locale.US);
148                }
149                if (isInstance(TheTVDB, self)) {
150                        return TheTVDB.getCharacters(self.getId(), Locale.US);
151                }
152                return null;
153        }
154
155        public static Integer getExternalId(Series self, String db) throws Exception {
156                return self.getExternalId(XDB.forName(db));
157        }
158
159        public static Integer getExternalId(SeriesInfo self, String db) throws Exception {
160                if (isInstance(TheMovieDB_TV, self)) {
161                        return XDB.TheMovieDB.getExternalId(self.getId(), XDB.forName(db));
162                }
163                if (isInstance(TheTVDB, self)) {
164                        return XDB.TheTVDB.getExternalId(self.getId(), XDB.forName(db));
165                }
166                if (isInstance(AniDB, self)) {
167                        return XDB.AniDB.getExternalId(self.getId(), XDB.forName(db));
168                }
169                return null;
170        }
171
172        public static Map<String, String> getExternalIds(SeriesInfo self) throws Exception {
173                if (isInstance(TheMovieDB_TV, self)) {
174                        return TheMovieDB_TV.getExternalIds(self.getId());
175                }
176                if (isInstance(TheTVDB, self)) {
177                        return TheTVDB.getExternalIds(self.getId());
178                }
179                return null;
180        }
181
182        public static List<Extra> getExtras(SeriesInfo self) throws Exception {
183                if (isInstance(TheMovieDB_TV, self)) {
184                        return TheMovieDB_TV.getExtras(self.getId());
185                }
186                return null;
187        }
188
189        public static Object getRaw(SeriesInfo self) throws Exception {
190                if (isInstance(TheMovieDB_TV, self)) {
191                        return TheMovieDB_TV.requestSeriesInfo(self.getId(), self.getLanguage(), Cache.ONE_MONTH);
192                }
193                if (isInstance(TheTVDB, self)) {
194                        return TheTVDB.requestSeriesInfo(self.getId(), self.getLanguage());
195                }
196                return null;
197        }
198
199        public static Object getRaw(EpisodeInfo self) throws Exception {
200                if (isInstance(TheMovieDB_TV, self)) {
201                        return TheMovieDB_TV.requestEpisodeInfo(self.getSeriesInfo().getId(), self.getSeason(), self.getEpisode(), self.getSeriesInfo().getLanguage());
202                }
203                if (isInstance(TheTVDB, self)) {
204                        return TheTVDB.requestEpisodeInfo(self.getId(), self.getSeriesInfo().getLanguage());
205                }
206                return null;
207        }
208
209        public static Object getService(Script context, String name) {
210                return WebServices.getService(name);
211        }
212
213}