001package net.filebot.web; 002 003import static java.util.Arrays.*; 004import static java.util.Collections.*; 005import static net.filebot.Logging.*; 006 007import java.io.Serializable; 008import java.net.URL; 009import java.util.EnumMap; 010import java.util.LinkedHashMap; 011import java.util.List; 012import java.util.Map; 013 014import net.filebot.CachedResource.Transform; 015 016public class MovieInfo implements Crew, Serializable { 017 018 public enum Property { 019 adult, backdrop_path, budget, homepage, id, imdb_id, original_title, original_language, overview, popularity, poster_path, release_date, revenue, runtime, status, tagline, title, video, vote_average, vote_count, certification, collection 020 } 021 022 protected Map<Property, String> fields; 023 024 protected String[] alternativeTitles; 025 protected String[] genres; 026 protected String[] keywords; 027 protected String[] spokenLanguages; 028 protected String[] productionCountries; 029 protected String[] productionCompanies; 030 protected Map<String, String> certifications; 031 protected Map<String, String> translations; 032 033 protected Person[] people; 034 035 public MovieInfo() { 036 // used by serializer 037 } 038 039 public MovieInfo(Map<Property, String> fields, List<String> alternativeTitles, List<String> genres, List<String> keywords, Map<String, String> certifications, Map<String, String> translations, List<String> spokenLanguages, List<String> productionCountries, List<String> productionCompanies, List<Person> people) { 040 this.fields = new EnumMap<Property, String>(fields); 041 this.alternativeTitles = alternativeTitles.toArray(new String[0]); 042 this.genres = genres.toArray(new String[0]); 043 this.keywords = keywords.toArray(new String[0]); 044 this.certifications = new LinkedHashMap<String, String>(certifications); 045 this.translations = new LinkedHashMap<String, String>(translations); 046 this.spokenLanguages = spokenLanguages.toArray(new String[0]); 047 this.productionCountries = productionCountries.toArray(new String[0]); 048 this.productionCompanies = productionCompanies.toArray(new String[0]); 049 this.people = people.toArray(new Person[0]); 050 } 051 052 public String get(Object key) { 053 return fields.get(Property.valueOf(key.toString())); 054 } 055 056 public String get(Property key) { 057 return fields.get(key); 058 } 059 060 private <T> T get(Property key, Transform<String, T> cast) { 061 try { 062 String value = fields.get(key); 063 if (value != null && !value.isEmpty()) { 064 return cast.transform(value); 065 } 066 } catch (Exception e) { 067 debug.warning(message("Invalid value", key, e, fields)); 068 } 069 return null; 070 } 071 072 public String getName() { 073 return get(Property.title); 074 } 075 076 public String getOriginalName() { 077 return get(Property.original_title); 078 } 079 080 public String getOriginalLanguage() { 081 return get(Property.original_language); 082 } 083 084 public String getCollection() { 085 return get(Property.collection); // e.g. Star Wars Collection 086 } 087 088 public String getCertification() { 089 return get(Property.certification); // e.g. PG-13 090 } 091 092 public String getStatus() { 093 return get(Property.status); 094 } 095 096 public boolean isAdult() { 097 return get(Property.adult, Boolean::parseBoolean); 098 } 099 100 public boolean isVideo() { 101 return get(Property.video, Boolean::parseBoolean); 102 } 103 104 public String getTagline() { 105 return get(Property.tagline); 106 } 107 108 public String getOverview() { 109 return get(Property.overview); 110 } 111 112 public Integer getId() { 113 return get(Property.id, Integer::parseInt); 114 } 115 116 public Integer getImdbId() { 117 return get(Property.imdb_id, s -> Integer.parseInt(s.substring(2))); // e.g. tt0379786 118 } 119 120 public Integer getVotes() { 121 return get(Property.vote_count, Integer::parseInt); 122 } 123 124 public Double getRating() { 125 return get(Property.vote_average, Double::parseDouble); 126 } 127 128 public SimpleDate getReleased() { 129 return get(Property.release_date, SimpleDate::parse); // e.g. 2005-09-30 130 } 131 132 public Integer getRuntime() { 133 return get(Property.runtime, Integer::parseInt); 134 } 135 136 public Long getBudget() { 137 return get(Property.budget, Long::parseLong); 138 } 139 140 public Long getRevenue() { 141 return get(Property.revenue, Long::parseLong); 142 } 143 144 public Double getPopularity() { 145 return get(Property.popularity, Double::parseDouble); 146 } 147 148 public URL getHomepage() { 149 return get(Property.homepage, WebRequest::newURL); 150 } 151 152 public URL getPoster() { 153 return get(Property.poster_path, WebRequest::newURL); 154 } 155 156 public URL getBackdrop() { 157 return get(Property.backdrop_path, WebRequest::newURL); 158 } 159 160 public List<String> getGenres() { 161 return unmodifiableList(asList(genres)); 162 } 163 164 public List<String> getKeywords() { 165 return unmodifiableList(asList(keywords)); 166 } 167 168 public List<String> getSpokenLanguages() { 169 return unmodifiableList(asList(spokenLanguages)); 170 } 171 172 public List<Person> getCrew() { 173 return unmodifiableList(asList(people)); 174 } 175 176 public Map<String, String> getCertifications() { 177 return unmodifiableMap(certifications); // e.g. ['US': PG-13] 178 } 179 180 public Map<String, String> getTranslations() { 181 return unmodifiableMap(translations); 182 } 183 184 public List<String> getProductionCountries() { 185 return unmodifiableList(asList(productionCountries)); 186 } 187 188 public List<String> getProductionCompanies() { 189 return unmodifiableList(asList(productionCompanies)); 190 } 191 192 public List<String> getAlternativeTitles() { 193 return unmodifiableList(asList(alternativeTitles)); 194 } 195 196 public Map<Property, String> properties() { 197 return unmodifiableMap(fields); 198 } 199 200 @Override 201 public String toString() { 202 if (fields.containsKey(Property.id)) { 203 return get(Property.id); 204 } 205 206 if (fields.containsKey(Property.imdb_id)) { 207 return get(Property.imdb_id); 208 } 209 210 return fields.toString(); 211 } 212 213}