Skip to content

Commit

Permalink
Made Genre class
Browse files Browse the repository at this point in the history
Fixed episodes and seasons not appearing
  • Loading branch information
heze8 committed Oct 26, 2019
1 parent cfc3606 commit e07c06d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
51 changes: 41 additions & 10 deletions src/main/java/seedu/ezwatchlist/api/ApiMain.java
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import info.movito.themoviedbapi.TmdbApi;
Expand All @@ -11,6 +12,7 @@
import info.movito.themoviedbapi.TmdbTvSeasons;
import info.movito.themoviedbapi.TvResultsPage;
import info.movito.themoviedbapi.model.Credits;
import info.movito.themoviedbapi.model.Genre;
import info.movito.themoviedbapi.model.MovieDb;
import info.movito.themoviedbapi.model.core.MovieResultsPage;
import info.movito.themoviedbapi.model.people.PersonCast;
Expand All @@ -24,11 +26,13 @@
import seedu.ezwatchlist.model.show.Date;
import seedu.ezwatchlist.model.show.Description;
import seedu.ezwatchlist.model.show.Episode;
import seedu.ezwatchlist.model.show.Genres;
import seedu.ezwatchlist.model.show.IsWatched;
import seedu.ezwatchlist.model.show.Movie;
import seedu.ezwatchlist.model.show.Name;
import seedu.ezwatchlist.model.show.Poster;
import seedu.ezwatchlist.model.show.RunningTime;
import seedu.ezwatchlist.model.show.Show;
import seedu.ezwatchlist.model.show.TvShow;

/**
Expand Down Expand Up @@ -142,6 +146,9 @@ public List<Movie> getMovieByName(String name) throws OnlineConnectionException
String imagePath = instance.retrieveImage(movieName);
toAdd.setPoster(new Poster(imagePath));

//genres
setGenres(movie.getGenres(), toAdd);

movies.add(toAdd);
}
return movies;
Expand All @@ -163,19 +170,23 @@ public List<TvShow> getTvShowByName(String name) throws OnlineConnectionExceptio

try {
TvResultsPage page = apiCall.getSearch().searchTv(name, null, 1);
TmdbTV apiCallTvSeries = apiCall.getTvSeries();

for (TvSeries tv : page.getResults()) {
final int tvId = tv.getId();
List<TvSeason> seasons = tv.getSeasons();
ArrayList<seedu.ezwatchlist.model.show.TvSeason> seasonsList = new ArrayList<>();
TvSeries series = apiCallTvSeries.getSeries(tvId, null);
TmdbTvSeasons tvSeasons = apiCall.getTvSeasons();
final int numberOfSeasons = tv.getNumberOfSeasons();
final int numberOfSeasons = series.getNumberOfSeasons();

//runtime
List<Integer> episodeRuntime = series.getEpisodeRuntime();
int runTime = episodeRuntime.isEmpty()? 0: getAverageRuntime(episodeRuntime);
ArrayList<seedu.ezwatchlist.model.show.TvSeason> seasonsList = new ArrayList<>();

//seasons
for (int seasonNo = 1; seasonNo < numberOfSeasons; seasonNo++) {
TvSeason tvSeason = tvSeasons.getSeason(tvId, seasonNo,
null, TmdbTvSeasons.SeasonMethod.values());

List<TvEpisode> episodes = tvSeason.getEpisodes();
ArrayList<Episode> episodeList = new ArrayList<>();

Expand All @@ -190,20 +201,21 @@ public List<TvShow> getTvShowByName(String name) throws OnlineConnectionExceptio
seasonsList.add(tvS);
}

Credits credits = apiCall.getTvSeries().getCredits(tvId, null);
Date date = new Date(apiCall.getTvSeries().getSeries(tvId, null,
TmdbTV.TvMethod.external_ids).getFirstAirDate());
Credits credits = apiCallTvSeries.getCredits(tvId, null);
Date date = new Date(series.getFirstAirDate());
//actors
Set<Actor> actors = getActors(credits.getCast());

TvShow tvShowToAdd = new TvShow(new Name(tv.getName()), new Description(tv.getOverview()),
new IsWatched(false), date, new RunningTime(20),
new IsWatched(false), date, new RunningTime(runTime),
actors, 0, tv.getNumberOfEpisodes(), seasonsList);


//image
ImageRetrieval instance = new ImageRetrieval(apiCall, tv.getPosterPath());
tvShowToAdd.setPoster(new Poster(instance.retrieveImage(tv.getName())));
//tvShowToAdd.setPoster(new Poster(instance.retrieveImage(tv.getName())));

//genres
setGenres(series.getGenres(), tvShowToAdd);

tvShows.add(tvShowToAdd);
}
Expand All @@ -214,6 +226,25 @@ public List<TvShow> getTvShowByName(String name) throws OnlineConnectionExceptio
}
}

private void setGenres(List<Genre> genres, Show tvShowToAdd) {
ArrayList<String> genreList = new ArrayList<>();
genres.forEach(x -> genreList.add(x.getName()));
tvShowToAdd.setGenres(new Genres(genreList));
}

private int getAverageRuntime(List<Integer> episodesRuntime) {
int totalRuntime = 0;
int noOfEpisodes = episodesRuntime.size();

for (int i = 0; i < noOfEpisodes; i++) {
int individualRuntime = episodesRuntime.get(i);
totalRuntime += individualRuntime;
}

int averageRunTime = Math.round(totalRuntime / noOfEpisodes);
return averageRunTime;
}

private Set<Actor> getActors(List<PersonCast> cast) {
Set<Actor> actors = new HashSet<>();
for (PersonCast personCast: cast) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/ezwatchlist/api/ImageRetrieval.java
Expand Up @@ -101,7 +101,7 @@ private void downloadImage(String fileName) throws OnlineConnectionException {
} catch (FileAlreadyExistsException f) {
System.err.println("Duplicate image");
} catch (IOException e) {
System.err.println(e.getCause());
System.err.println(e + " in ImageRetrieval line 104");
throw new OnlineConnectionException("No internet connection at downloading image");
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/seedu/ezwatchlist/model/show/Genres.java
@@ -0,0 +1,20 @@
package seedu.ezwatchlist.model.show;

import java.util.ArrayList;
import java.util.List;

public class Genres {
private List<String> genres;

public Genres(List<String> genres) {
this.genres = genres;
}

public List<String> getGenres() {
return genres;
}

public void setGenres(List<String> genres) {
this.genres = genres;
}
}
9 changes: 9 additions & 0 deletions src/main/java/seedu/ezwatchlist/model/show/Show.java
Expand Up @@ -27,6 +27,7 @@ public abstract class Show {
private final RunningTime runningTime;
private final Set<Actor> actors = new HashSet<>();
private Poster poster;
private Genres genres;

public Show(Name name, Description description, IsWatched isWatched, Date dateOfRelease,
RunningTime runningTime, Set<Actor> actors) {
Expand Down Expand Up @@ -191,4 +192,12 @@ public String toString() {
getActors().forEach(builder::append);
return builder.toString();
}

public Genres getGenres() {
return genres;
}

public void setGenres(Genres genres) {
this.genres = genres;
}
}
1 change: 0 additions & 1 deletion src/main/java/seedu/ezwatchlist/model/show/TvShow.java
Expand Up @@ -12,7 +12,6 @@
*/
public class TvShow extends Show {

private static final Image imageOfShow = null;
private int numOfEpisodesWatched;
private List<TvSeason> tvSeasons;
private final int totalNumOfEpisodes;
Expand Down

0 comments on commit e07c06d

Please sign in to comment.