From aa1639859023042ad339eb3bbf04236c58ade39f Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 28 Aug 2014 11:42:41 +0000 Subject: [PATCH] * make sure unexpected JSON response data does not make us throw up NPEs --- .../net/filebot/format/MediaBindingBean.java | 4 +- source/net/filebot/web/TMDbClient.java | 140 +++++++++++------- 2 files changed, 86 insertions(+), 58 deletions(-) diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index 50924771..9dfaba4e 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -253,7 +253,7 @@ public class MediaBindingBean { @Define("imdbid") public String getImdbId() throws Exception { - int imdbid = getMovie().getImdbId(); + Integer imdbid = getMovie().getImdbId(); if (imdbid <= 0) { if (getMovie().getTmdbId() <= 0) { @@ -264,7 +264,7 @@ public class MediaBindingBean { imdbid = WebServices.TheMovieDB.getMovieInfo(getMovie(), Locale.ENGLISH, false).getImdbId(); } - return String.format("tt%07d", imdbid); + return imdbid != null ? String.format("tt%07d", imdbid) : null; } @Define("vc") diff --git a/source/net/filebot/web/TMDbClient.java b/source/net/filebot/web/TMDbClient.java index f1c68246..128a0b56 100644 --- a/source/net/filebot/web/TMDbClient.java +++ b/source/net/filebot/web/TMDbClient.java @@ -208,69 +208,97 @@ public class TMDbClient implements MovieIdentificationService { JSONObject collection = (JSONObject) response.get("belongs_to_collection"); fields.put(MovieProperty.collection, (String) collection.get("name")); } catch (Exception e) { - // ignore + // movie doesn't belong to any collection } List genres = new ArrayList(); - for (JSONObject it : jsonList(response.get("genres"))) { - String name = (String) it.get("name"); - if (name != null && name.length() > 0) { - genres.add(name); - } - } - - List spokenLanguages = new ArrayList(); - for (JSONObject it : jsonList(response.get("spoken_languages"))) { - spokenLanguages.add((String) it.get("iso_639_1")); - } - - List alternativeTitles = new ArrayList(); - JSONObject titles = (JSONObject) response.get("alternative_titles"); - for (JSONObject it : jsonList(titles.get("titles"))) { - alternativeTitles.add((String) it.get("title")); - } - - String countryCode = locale.getCountry().isEmpty() ? "US" : locale.getCountry(); - JSONObject releases = (JSONObject) response.get("releases"); - for (JSONObject it : jsonList(releases.get("countries"))) { - if (countryCode.equals(it.get("iso_3166_1"))) { - fields.put(MovieProperty.certification, (String) it.get("certification")); - } - } - - List cast = new ArrayList(); - JSONObject castResponse = (JSONObject) response.get("casts"); - for (String section : new String[] { "cast", "crew" }) { - for (JSONObject it : jsonList(castResponse.get(section))) { - Map person = new EnumMap(PersonProperty.class); - for (PersonProperty key : PersonProperty.values()) { - Object value = it.get(key.name()); - if (value != null) { - person.put(key, value.toString()); - } - } - cast.add(new Person(person)); - } - } - - List trailers = new ArrayList(); - JSONObject trailerResponse = (JSONObject) response.get("trailers"); try { - for (String section : new String[] { "quicktime", "youtube" }) { - for (JSONObject it : jsonList(trailerResponse.get(section))) { - Map sources = new LinkedHashMap(); - if (it.containsKey("sources")) { - for (JSONObject s : jsonList(it.get("sources"))) { - sources.put(s.get("size").toString(), s.get("source").toString()); - } - } else { - sources.put(it.get("size").toString(), it.get("source").toString()); - } - trailers.add(new Trailer(section, it.get("name").toString(), sources)); + for (JSONObject it : jsonList(response.get("genres"))) { + String name = (String) it.get("name"); + if (name != null && name.length() > 0) { + genres.add(name); } } } catch (Exception e) { - Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal trailer data: " + trailerResponse); + Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal genres data: " + response); + } + + List spokenLanguages = new ArrayList(); + try { + for (JSONObject it : jsonList(response.get("spoken_languages"))) { + spokenLanguages.add((String) it.get("iso_639_1")); + } + } catch (Exception e) { + Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal spoken_languages data: " + response); + } + + List alternativeTitles = new ArrayList(); + try { + JSONObject titles = (JSONObject) response.get("alternative_titles"); + if (titles != null) { + for (JSONObject it : jsonList(titles.get("titles"))) { + alternativeTitles.add((String) it.get("title")); + } + } + } catch (Exception e) { + Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal alternative_titles data: " + response); + } + + try { + String countryCode = locale.getCountry().isEmpty() ? "US" : locale.getCountry(); + JSONObject releases = (JSONObject) response.get("releases"); + if (releases != null) { + for (JSONObject it : jsonList(releases.get("countries"))) { + if (countryCode.equals(it.get("iso_3166_1"))) { + fields.put(MovieProperty.certification, (String) it.get("certification")); + } + } + } + } catch (Exception e) { + Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal releases data: " + response); + } + + List cast = new ArrayList(); + try { + JSONObject castResponse = (JSONObject) response.get("casts"); + if (castResponse != null) { + for (String section : new String[] { "cast", "crew" }) { + for (JSONObject it : jsonList(castResponse.get(section))) { + Map person = new EnumMap(PersonProperty.class); + for (PersonProperty key : PersonProperty.values()) { + Object value = it.get(key.name()); + if (value != null) { + person.put(key, value.toString()); + } + } + cast.add(new Person(person)); + } + } + } + } catch (Exception e) { + Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal casts data: " + response); + } + + List trailers = new ArrayList(); + try { + JSONObject trailerResponse = (JSONObject) response.get("trailers"); + if (trailerResponse != null) { + for (String section : new String[] { "quicktime", "youtube" }) { + for (JSONObject it : jsonList(trailerResponse.get(section))) { + Map sources = new LinkedHashMap(); + if (it.containsKey("sources")) { + for (JSONObject s : jsonList(it.get("sources"))) { + sources.put(s.get("size").toString(), s.get("source").toString()); + } + } else { + sources.put(it.get("size").toString(), it.get("source").toString()); + } + trailers.add(new Trailer(section, it.get("name").toString(), sources)); + } + } + } + } catch (Exception e) { + Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal trailers data: " + response); } return new MovieInfo(fields, alternativeTitles, genres, spokenLanguages, cast, trailers);