Java tutorial
/* * Copyright (c) 2004-2013 YAMJ Members * http://code.google.com/p/moviejukebox/people/list * * This file is part of the Yet Another Movie Jukebox (YAMJ). * * The YAMJ is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * YAMJ is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with the YAMJ. If not, see <http://www.gnu.org/licenses/>. * * Web: http://code.google.com/p/moviejukebox/ * */ package com.tda.sitefilm.allocine; import java.io.InputStream; import java.util.List; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.tda.sitefilm.allocine.jaxb.ActivityType; import com.tda.sitefilm.allocine.jaxb.CastMember; import com.tda.sitefilm.allocine.jaxb.Casting; import com.tda.sitefilm.allocine.jaxb.Distributor; import com.tda.sitefilm.allocine.jaxb.Episode; import com.tda.sitefilm.allocine.jaxb.EpisodeList; import com.tda.sitefilm.allocine.jaxb.GenreList; import com.tda.sitefilm.allocine.jaxb.HtmlSynopsisType; import com.tda.sitefilm.allocine.jaxb.Media; import com.tda.sitefilm.allocine.jaxb.MediaList; import com.tda.sitefilm.allocine.jaxb.Movie; import com.tda.sitefilm.allocine.jaxb.MovieCertificate; import com.tda.sitefilm.allocine.jaxb.NationalityList; import com.tda.sitefilm.allocine.jaxb.Person; import com.tda.sitefilm.allocine.jaxb.PosterType; import com.tda.sitefilm.allocine.jaxb.RatingStatsType; import com.tda.sitefilm.allocine.jaxb.RatingType; import com.tda.sitefilm.allocine.jaxb.Release; import com.tda.sitefilm.allocine.jaxb.Season; import com.tda.sitefilm.allocine.jaxb.SeasonList; import com.tda.sitefilm.allocine.jaxb.Statistics; import com.tda.sitefilm.allocine.jaxb.ThumbnailType; import com.tda.sitefilm.allocine.jaxb.Tvseries; import com.tda.sitefilm.allocine.jaxb.TypeType; /** * Implementation for JSON format * * @author modmax */ public final class JSONAllocineAPIHelper extends AbstractAllocineAPI { /** * Constructor. * * @param apiKey The API key for allocine */ public JSONAllocineAPIHelper(String apiKey) { super(apiKey, "json"); } /** * bla * @param query nom du film recherch * @return la rehcerche * @exception Exception exception */ //@Override public Search searchMovieInfos(String query) throws Exception { JsonNode rootNode = null; InputStream inputStream = connectSearchMovieInfos(query).getInputStream(); try { ObjectMapper mapper = new ObjectMapper(); rootNode = mapper.readTree(inputStream); } finally { closeInputStream(inputStream); } Search search = new Search(); if (rootNode == null) { return search; } JsonNode feedNode = rootNode.get("feed"); if (feedNode != null) { JsonNode moviesNode = feedNode.get("movie"); if (moviesNode != null && moviesNode.size() > 0) { for (int i = 0; i < moviesNode.size(); i++) { JsonNode movieNode = moviesNode.get(i); Movie movie = new Movie(); movie.setCode(getValueAsInt(movieNode.get("code"))); movie.setTitle(getValueAsString(movieNode.get("title"))); movie.setOriginalTitle(getValueAsString(movieNode.get("originalTitle"))); movie.setProductionYear(getValueAsString(movieNode.get("productionYear"))); // parse poster movie.setPoster(parsePosterType(movieNode)); // enough values for search result search.getMovie().add(movie); } // NOTE: pagination not supported; so totalsResults may be higher than count search.setTotalResults(getValueAsInt(feedNode.get("totalResults"))); } } return search; } //@Override public Search searchTvseriesInfos(String query) throws Exception { JsonNode rootNode = null; InputStream inputStream = connectSearchTvseriesInfos(query).getInputStream(); try { ObjectMapper mapper = new ObjectMapper(); rootNode = mapper.readTree(inputStream); } finally { closeInputStream(inputStream); } Search search = new Search(); if (rootNode == null) { return search; } JsonNode feedNode = rootNode.get("feed"); if (feedNode != null) { JsonNode tvseriesNode = feedNode.get("tvseries"); if (tvseriesNode != null && tvseriesNode.size() > 0) { for (int i = 0; i < tvseriesNode.size(); i++) { JsonNode tvserieNode = tvseriesNode.get(i); Tvseries tvseries = new Tvseries(); tvseries.setCode(getValueAsInt(tvserieNode.get("code"))); tvseries.setTitle(getValueAsString(tvserieNode.get("title"))); tvseries.setOriginalTitle(getValueAsString(tvserieNode.get("originalTitle"))); tvseries.setYearStart(getValueAsString(tvserieNode.get("yearStart"))); tvseries.setYearEnd(getValueAsString(tvserieNode.get("yearEnd"))); // enough values for search result search.getTvseries().add(tvseries); } // NOTE: pagination not supported; so totalsResults may be higher than count search.setTotalResults(getValueAsInt(feedNode.get("totalResults"))); } } return search; } //@Override public MovieInfos getMovieInfos(String allocineId) throws Exception { JsonNode rootNode = null; InputStream inputStream = connectGetMovieInfos(allocineId).getInputStream(); try { ObjectMapper mapper = new ObjectMapper(); rootNode = mapper.readTree(inputStream); } finally { closeInputStream(inputStream); } MovieInfos infos = new MovieInfos(); if (rootNode == null) { return infos; } JsonNode movieNode = rootNode.get("movie"); if (movieNode != null) { infos.setCode(getValueAsInt(movieNode.get("code"))); infos.setTitle(getValueAsString(movieNode.get("title"))); infos.setOriginalTitle(getValueAsString(movieNode.get("originalTitle"))); infos.setProductionYear(getValueAsString(movieNode.get("productionYear"))); infos.setRuntime(getValueAsInt(movieNode.get("runtime"))); // parse synopsis infos.setHtmlSynopsis(parseHtmlSynopsis(movieNode)); // parse release infos.setRelease(parseRelease(movieNode)); // parse poster infos.setPoster(parsePosterType(movieNode)); // parse genres if (infos.getGenreList() == null) infos.setGenreList(new GenreList()); parseListValues(infos.getGenreList().getGenre(), movieNode.get("genre")); // parse nationality if (infos.getNationalityList() == null) infos.setNationalityList(new NationalityList()); parseListValues(infos.getNationalityList().getNationality(), movieNode.get("nationality")); // parse certificates /*if(infos.getMovieCertificate()==null) infos.setMovieCertificate(new MovieCertificate()); parseListValues(infos.getMovieCertificate().getCertificate(), movieNode.get("movieCertificate"));*/ // parse media if (infos.getMediaList() == null) infos.setMediaList(new MediaList()); parseMedia(infos.getMediaList().getMedia(), movieNode); // parse casting if (infos.getCasting() == null) infos.setCasting(new Casting()); parseCasting(infos.getCasting().getCastMember(), movieNode); // parse statistics infos.setStatistics(parseStatistics(movieNode)); } return infos; } //@Override public TvSeriesInfos getTvSeriesInfos(String allocineId) throws Exception { JsonNode rootNode = null; InputStream inputStream = connectGetTvSeriesInfos(allocineId).getInputStream(); try { ObjectMapper mapper = new ObjectMapper(); rootNode = mapper.readTree(inputStream); } finally { closeInputStream(inputStream); } TvSeriesInfos infos = new TvSeriesInfos(); if (rootNode == null) { return infos; } JsonNode tvseriesNode = rootNode.get("tvseries"); if (tvseriesNode != null) { infos.setCode(getValueAsInt(tvseriesNode.get("code"))); infos.setTitle(getValueAsString(tvseriesNode.get("title"))); infos.setOriginalTitle(getValueAsString(tvseriesNode.get("originalTitle"))); infos.setYearStart(getValueAsString(tvseriesNode.get("yearStart"))); infos.setYearEnd(getValueAsString(tvseriesNode.get("yearEnd"))); infos.setSeasonCount(getValueAsInt(tvseriesNode.get("seasonCount"))); // parse original channel infos.setOriginalChannel(parseOriginalChannel(tvseriesNode)); // parse synopsis infos.setHtmlSynopsis(parseHtmlSynopsis(tvseriesNode)); // parse release infos.setRelease(parseRelease(tvseriesNode)); // parse genres if (infos.getGenreList() == null) infos.setGenreList(new GenreList()); parseListValues(infos.getGenreList().getGenre(), tvseriesNode.get("genre")); // parse nationality if (infos.getNationalityList() == null) infos.setNationalityList(new NationalityList()); parseListValues(infos.getNationalityList().getNationality(), tvseriesNode.get("nationality")); // parse media if (infos.getMediaList() == null) infos.setMediaList(new MediaList()); parseMedia(infos.getMediaList().getMedia(), tvseriesNode); // parse casting if (infos.getCasting() == null) infos.setCasting(new Casting()); parseCasting(infos.getCasting().getCastMember(), tvseriesNode); // parse statistics infos.setStatistics(parseStatistics(tvseriesNode)); // parse seasons if (infos.getSeasonList() == null) infos.setSeasonList(new SeasonList()); parseSeasonList(infos.getSeasonList().getSeason(), tvseriesNode); } return infos; } //@Override public TvSeasonInfos getTvSeasonInfos(Integer seasonCode) throws Exception { JsonNode rootNode = null; InputStream inputStream = connectGetTvSeasonInfos(seasonCode).getInputStream(); try { ObjectMapper mapper = new ObjectMapper(); rootNode = mapper.readTree(inputStream); } finally { closeInputStream(inputStream); } TvSeasonInfos infos = new TvSeasonInfos(); if (rootNode == null) { return infos; } JsonNode seasonNode = rootNode.get("season"); if (seasonNode != null) { infos.setCode(getValueAsInt(seasonNode.get("code"))); infos.setSeasonNumber(getValueAsInt(seasonNode.get("seasonNumber"))); infos.setYearStart(getValueAsString(seasonNode.get("yearStart"))); infos.setYearEnd(getValueAsString(seasonNode.get("yearEnd"))); // parse episodes if (infos.getEpisodeList() == null) infos.setEpisodeList(new EpisodeList()); parseEpisodeList(infos.getEpisodeList().getEpisode(), seasonNode); } return infos; } private static int getValueAsInt(JsonNode node) { if (node == null) { return -1; } else if (node.isNull()) { return -1; } try { return Integer.parseInt(node.asText()); } catch (Exception ignore) { return -1; } } private static float getValueAsFloat(JsonNode node) { if (node == null) { return 0f; } else if (node.isNull()) { return 0f; } try { return Float.parseFloat(node.asText()); } catch (Exception ignore) { return 0f; } } private static String getValueAsString(JsonNode node) { if (node == null) { return null; } else if (node.isNull()) { return null; } return String.valueOf(node.asText()); } private static void parseListValues(List<String> values, JsonNode valuesNode) { if (valuesNode != null && (valuesNode.size() > 0)) { for (int i = 0; i < valuesNode.size(); i++) { String value = getValueAsString(valuesNode.get(i).get("$")); if (value != null) { values.add(value); } } } } private static HtmlSynopsisType parseHtmlSynopsis(JsonNode rootNode) { JsonNode node = rootNode.get("synopsis"); if (node != null) { HtmlSynopsisType synopsis = new HtmlSynopsisType(); synopsis.getContent().add(getValueAsString(node)); return synopsis; } return null; } private static Release parseRelease(JsonNode rootNode) { // parse release JsonNode node = rootNode.get("release"); if (node != null && (node.size() > 0)) { Release release = new Release(); release.setReleaseDate(getValueAsString(node.get("releaseDate"))); node = node.get("distributor"); if (node != null && (node.size() > 0)) { Distributor distributor = new Distributor(); distributor.setName(getValueAsString(node.get("name"))); release.setDistributor(distributor); } return release; } return null; } private static PosterType parsePosterType(JsonNode rootNode) { JsonNode node = rootNode.get("poster"); if (node != null) { String href = getValueAsString(node.get("href")); if (href != null) { PosterType posterType = new PosterType(); posterType.setHref(href); return posterType; } } return null; } private static Statistics parseStatistics(JsonNode rootNode) { JsonNode node = rootNode.get("statistics"); if (node != null) { JsonNode ratingNode = node.get("rating"); if (ratingNode != null && (ratingNode.size() > 0)) { Statistics statistics = new Statistics(); for (int i = 0; i < ratingNode.size(); i++) { node = ratingNode.get(i); RatingType ratingType = new RatingType(); ratingType.setNote(getValueAsFloat(node.get("note"))); ratingType.setValue(getValueAsInt(node.get("$"))); if (statistics.getRatingStats() == null) statistics.setRatingStats(new RatingStatsType()); statistics.getRatingStats().getRating().add(ratingType); } return statistics; } } return null; } private static String parseOriginalChannel(JsonNode rootNode) { JsonNode node = rootNode.get("originalChannel"); if (node != null) { return getValueAsString(node.get("$")); } return null; } private static void parseMedia(List<Media> mediaList, JsonNode rootNode) { JsonNode node = rootNode.get("media"); if (node != null && (node.size() > 0)) { for (int i = 0; i < node.size(); i++) { JsonNode mediaNode = node.get(i); Media media = new Media(); JsonNode innerNode = mediaNode.get("type"); if (innerNode != null) { TypeType typeType = new TypeType(); typeType.setCode(getValueAsInt(innerNode.get("code"))); media.setType(typeType); } innerNode = mediaNode.get("thumbnail"); if (innerNode != null) { ThumbnailType thumbnailType = new ThumbnailType(); thumbnailType.setHref(getValueAsString(innerNode.get("href"))); media.setThumbnail(thumbnailType); } mediaList.add(media); } } } private static void parseCasting(List<CastMember> members, JsonNode rootNode) { JsonNode node = rootNode.get("castMember"); if (node != null && (node.size() > 0)) { for (int i = 0; i < node.size(); i++) { JsonNode memberNode = node.get(i); CastMember member = new CastMember(); member.setRole(getValueAsString(memberNode.get("role"))); JsonNode personNode = memberNode.get("person"); if (personNode != null) { Person person = new Person(); person.setCode(getValueAsInt(personNode.get("code"))); person.setName(getValueAsString(personNode.get("name"))); member.setPerson(person); } JsonNode activityNode = memberNode.get("activity"); if (activityNode != null) { ActivityType activityType = new ActivityType(); activityType.setCode(Integer.valueOf(getValueAsInt(activityNode.get("code")))); member.setActivity(activityType); } members.add(member); } } } private static void parseSeasonList(List<Season> seasons, JsonNode rootNode) { JsonNode node = rootNode.get("season"); if (node != null && (node.size() > 0)) { for (int i = 0; i < node.size(); i++) { JsonNode seasonNode = node.get(i); Season season = new Season(); season.setCode(getValueAsInt(seasonNode.get("code"))); season.setSeasonNumber(getValueAsInt(seasonNode.get("seasonNumber"))); season.setYearStart(getValueAsString(seasonNode.get("yearStart"))); season.setYearEnd(getValueAsString(seasonNode.get("yearEnd"))); seasons.add(season); } } } private static void parseEpisodeList(List<Episode> episodes, JsonNode rootNode) { JsonNode node = rootNode.get("episode"); if (node != null && (node.size() > 0)) { for (int i = 0; i < node.size(); i++) { JsonNode episodeNode = node.get(i); Episode episode = new Episode(); episode.setCode(getValueAsInt(episodeNode.get("code"))); episode.setTitle(getValueAsString(episodeNode.get("title"))); episode.setOriginalTitle(getValueAsString(episodeNode.get("originalTitle"))); episode.setEpisodeNumberSeries(getValueAsInt(episodeNode.get("episodeNumberSeries"))); episode.setEpisodeNumberSeason(getValueAsInt(episodeNode.get("episodeNumberSeason"))); episode.setSynopsis(getValueAsString(episodeNode.get("synopsis"))); episodes.add(episode); } } } }