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 List<String> getActors(SeriesInfo self) throws Exception {
129                List<Person> crew = getCrew(self);
130                if (crew != null) {
131                        return crew.stream().filter(Person::isActor).map(Person::getName).collect(toList());
132                }
133                return null;
134        }
135
136        public static List<Person> getCrew(SeriesInfo self) throws Exception {
137                if (isInstance(TheMovieDB_TV, self)) {
138                        return TheMovieDB_TV.getCredits(self.getId(), Locale.US);
139                }
140                if (isInstance(TheTVDB, self)) {
141                        return TheTVDB.getCharacters(self.getId(), Locale.US);
142                }
143                return null;
144        }
145
146        public static Integer getExternalId(Series self, String db) throws Exception {
147                return self.getExternalId(XDB.forName(db));
148        }
149
150        public static Integer getExternalId(SeriesInfo self, String db) throws Exception {
151                if (isInstance(TheMovieDB_TV, self)) {
152                        return XDB.TheMovieDB.getExternalId(self.getId(), XDB.forName(db));
153                }
154                if (isInstance(TheTVDB, self)) {
155                        return XDB.TheTVDB.getExternalId(self.getId(), XDB.forName(db));
156                }
157                if (isInstance(AniDB, self)) {
158                        return XDB.AniDB.getExternalId(self.getId(), XDB.forName(db));
159                }
160                return null;
161        }
162
163        public static Map<String, String> getExternalIds(SeriesInfo self) throws Exception {
164                if (isInstance(TheMovieDB_TV, self)) {
165                        return TheMovieDB_TV.getExternalIds(self.getId());
166                }
167                if (isInstance(TheTVDB, self)) {
168                        return TheTVDB.getExternalIds(self.getId());
169                }
170                return null;
171        }
172
173        public static List<Extra> getExtras(SeriesInfo self) throws Exception {
174                if (isInstance(TheMovieDB_TV, self)) {
175                        return TheMovieDB_TV.getExtras(self.getId());
176                }
177                return null;
178        }
179
180        public static EpisodeInfo getInfo(Episode self) throws Exception {
181                if (isInstance(TheMovieDB_TV, self)) {
182                        // episode lookup requires default SxE numbers
183                        Episode sxe = reorderEpisode(self, SortOrder.Airdate);
184                        int s = isRegularEpisode(self) ? sxe.getSeason() : 0;
185                        int e = isRegularEpisode(self) ? sxe.getEpisode() : sxe.getSpecial();
186                        return TheMovieDB_TV.getEpisodeInfo(self.getSeriesInfo().getId(), s, e, self.getSeriesInfo().getLanguage());
187                }
188                if (isInstance(TheTVDB, self)) {
189                        return TheTVDB.getEpisodeInfo(self.getId(), self.getSeriesInfo().getLanguage());
190                }
191                return null;
192        }
193
194        public static Object getRaw(SeriesInfo self) throws Exception {
195                if (isInstance(TheMovieDB_TV, self)) {
196                        return TheMovieDB_TV.requestSeriesInfo(self.getId(), self.getLanguage(), Cache.ONE_MONTH);
197                }
198                if (isInstance(TheTVDB, self)) {
199                        return TheTVDB.requestSeriesInfo(self.getId(), self.getLanguage());
200                }
201                return null;
202        }
203
204        public static Object getRaw(EpisodeInfo self) throws Exception {
205                if (isInstance(TheMovieDB_TV, self)) {
206                        return TheMovieDB_TV.requestEpisodeInfo(self.getSeriesInfo().getId(), self.getSeason(), self.getEpisode(), self.getSeriesInfo().getLanguage());
207                }
208                if (isInstance(TheTVDB, self)) {
209                        return TheTVDB.requestEpisodeInfo(self.getId(), self.getSeriesInfo().getLanguage());
210                }
211                return null;
212        }
213
214        public static Object getService(Script context, String name) {
215                return WebServices.getService(name);
216        }
217
218}