Omertron / api-themoviedb

API for TheMovieDb.org website
GNU General Public License v3.0
154 stars 65 forks source link

Empty Genre #40

Closed faxe1008 closed 7 years ago

faxe1008 commented 7 years ago

I noticed that the GetGenres() method of the MovieInfo class always returns null. Did they change anything in their JSON encoding?

Omertron commented 7 years ago

Does this happen to all movies? or just some?

faxe1008 commented 7 years ago

As far as I can tell, yes

Omertron commented 7 years ago

Can you give me a sample of your code and I will try and look at it today. Thanks

faxe1008 commented 7 years ago
private static final String apiKey = "<KEY>";
 TmdbSearch apiSearch = new TmdbSearch(apiKey, new HttpTools(httpClient));
 result = apiSearch.searchMovie("batman", nextPage, "de", true, null, null, null);

List<Genre> lg = result[0].getGenres();
if(lg!=null && lg.size()>0)
       System.out.println(lg.get(0).getName());
Omertron commented 7 years ago

Ok, the genres are now listed as IDs rather than specific strings. Your application will need to pre-load and cache a map of the genre IDs and Names, e.g.

    private Map<Integer, String> mapGenres(String language) throws MovieDbException {
        TmdbGenres tmdbGenres = new TmdbGenres(getApiKey(), getHttpTools());
        List<Genre> genreList = tmdbGenres.getGenreMovieList(language).getResults();
        Map<Integer, String> genreMap = new HashMap<>();

        for(Genre g:genreList) {
            genreMap.put(g.getId(), g.getName());
        }

        return genreMap;
    }

Then you can do something like:

    ResultList<MovieInfo> movieList = apiSearch.searchMovie("Blade Runner", 0, "", null, 0, 0, SearchType.PHRASE);
    MovieInfo result = movieList.getResults().get(0);

    Map<Integer, String> genreMap = mapGenres("de");

    for (int g : result.getGenreIds()) {
        LOG.info("{} = {}", g, genreMap.get(g));
    }