Skip to content

Commit

Permalink
Added new info in 2 requests
Browse files Browse the repository at this point in the history
  • Loading branch information
aqerd committed Nov 17, 2024
1 parent f7e492b commit d62fa54
Showing 1 changed file with 65 additions and 16 deletions.
81 changes: 65 additions & 16 deletions src/main/java/org/oopproject/TelegramBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
//import com.google.gson.Gson;
//import com.google.gson.reflect.TypeToken;
import feign.FeignException;
import org.oopproject.deserializers.CreditsDeserializer;
import org.oopproject.deserializers.ListDeserializer;
import org.oopproject.deserializers.PersonDeserializer;
import org.oopproject.utils.CommandWaiter;
import org.oopproject.utils.Genres;
import org.oopproject.parameters.MovieParameters;
import org.oopproject.parameters.ParametersBuilder;
import org.oopproject.deserializers.FilmDeserializer;

import static java.lang.Integer.parseInt;
import static org.oopproject.utils.CommandWaiter.*;
import static org.oopproject.utils.Config.*;
import static org.oopproject.utils.Validators.isCommand;
Expand Down Expand Up @@ -44,7 +47,7 @@ public class TelegramBot implements LongPollingSingleThreadUpdateConsumer {
// private final Database database = new Database();
// private final Gson gson = new Gson();

private final int constNumber = 10;
private final int constNumber = 15;

private final HashMap<Integer, Integer> yearMovieIndexMap = new HashMap<>();
private final HashMap<String, Integer> genreMovieIndexMap = new HashMap<>();
Expand Down Expand Up @@ -134,8 +137,22 @@ public void handleButtons(Update update) {
long chatId = update.getCallbackQuery().getMessage().getChatId();

if (callbackData.startsWith("movie_")) {
String film = callbackData.substring(6);
String responseMessage = "Выбранный фильм: " + film;
int id = parseInt(callbackData.substring(6));
FilmDeserializer film = tmdbService.getMovieById(TMDB_TOKEN, id);

StringBuilder filmBuilder = new StringBuilder(film.title);
if (!Objects.equals(film.original_language, "en")) {
filmBuilder.append(" / ").append(film.original_title);
}
filmBuilder.append(" (").append(film.release_date, 0, 4).append(", ")
.append(film.origin_country[0]).append(")").append("\n\n")
.append(film.overview).append("\n\n")
.append("Vote average: ").append(film.vote_average).append("/10\n")
.append("Runtime: ").append(film.runtime).append(" min \n");
if (film.homepage != null) {
filmBuilder.append("Link: ").append(film.homepage).append("\n");
}
String responseMessage = filmBuilder.toString();

SendMessage response = SendMessage
.builder()
Expand All @@ -153,7 +170,7 @@ public void handleButtons(Update update) {

AnswerCallbackQuery answerCallbackQuery = AnswerCallbackQuery.builder()
.callbackQueryId(update.getCallbackQuery().getId())
.text("Вы выбрали фильм " + film)
.text("Вы выбрали фильм " + film.title)
.showAlert(false)
.build();
answerCallbackQuery.setCallbackQueryId(update.getCallbackQuery().getId());
Expand All @@ -164,8 +181,38 @@ public void handleButtons(Update update) {
e.printStackTrace();
}
} else if (callbackData.startsWith("actor_")) {
String actor = callbackData.substring(6);
String responseMessage = "Выбранный актёр: " + actor;
int id = parseInt(callbackData.substring(6));
String responseMessage;
List<FilmDeserializer> movies;
CreditsDeserializer actorsFilms = tmdbService.getActorsFilms(TMDB_TOKEN, id);
PersonDeserializer actor = tmdbService.getActor(TMDB_TOKEN, id);

StringBuilder actorsData = new StringBuilder(actor.name).append(" (").append(actor.birthday, 0, 4);
if (actor.deathday != null) {
actorsData.append(" - ").append(actor.deathday, 0, 4);
}
actorsData.append(")").append("\n").append("Place of birth: ").append(actor.place_of_birth)
.append("\n").append("Popularity: ").append(actor.popularity)
.append("\n").append("ID: ").append(actor.id).append("\n\n")
.append(actor.biography).append("\n\n");

if ((actorsFilms.cast != null || actorsFilms.crew != null) && actor.known_for_department != null) {
StringBuilder actorsFilmsBuilder = new StringBuilder("Фильмы с участием " + actor.name + ":\n");
if (Objects.equals(actor.known_for_department, "Acting")) {
movies = actorsFilms.cast;
} else {
movies = actorsFilms.crew;
}

for (int i = 0; i < constNumber; i++) {
FilmDeserializer currentMovie = movies.get(i);
actorsFilmsBuilder.append(i + 1).append(". ").append(currentMovie.title).append("\n");
}

responseMessage = actorsData.append(actorsFilmsBuilder).toString();
} else {
responseMessage = "Данные не найдены";
}

SendMessage response = SendMessage
.builder()
Expand All @@ -183,7 +230,7 @@ public void handleButtons(Update update) {

AnswerCallbackQuery answerCallbackQuery = AnswerCallbackQuery.builder()
.callbackQueryId(update.getCallbackQuery().getId())
.text("Вы выбрали актёра " + actor)
.text("Вы выбрали актёра " + actor.name)
.showAlert(false)
.build();
answerCallbackQuery.setCallbackQueryId(update.getCallbackQuery().getId());
Expand Down Expand Up @@ -333,7 +380,7 @@ protected String handleYear(String messageText, long chatId) {
String responseMessage;

try {
int userYear = Integer.parseInt(messageText);
int userYear = parseInt(messageText);
int currentYear = java.time.Year.now().getValue();

if (userYear < 1900 || userYear > currentYear) {
Expand Down Expand Up @@ -394,10 +441,11 @@ protected String handleMovieSearch(String messageText, long chatId) {
}

for (int i = 0; i < filmsToProcess; i++) {
String currentId = String.valueOf(movies.get(i).id);
String currentTitle = String.valueOf(movies.get(i).title);
InlineKeyboardButton button = InlineKeyboardButton.builder()
.text(currentTitle)
.callbackData("movie_" + currentTitle)
.callbackData("movie_" + currentId)
.build();
InlineKeyboardRow row = new InlineKeyboardRow();
row.add(button);
Expand Down Expand Up @@ -440,10 +488,11 @@ protected String handleActorSearch(String messageText, long chatId) {
}

for (int i = 0; i < actorsToProcess; i++) {
String currentTitle = String.valueOf(people.get(i).name);
String currentName = String.valueOf(people.get(i).name);
String currentId = String.valueOf(people.get(i).id);
InlineKeyboardButton button = InlineKeyboardButton.builder()
.text(currentTitle)
.callbackData("actor_" + currentTitle)
.text(currentName)
.callbackData("actor_" + currentId)
.build();
InlineKeyboardRow row = new InlineKeyboardRow();
row.add(button);
Expand Down Expand Up @@ -477,7 +526,7 @@ protected String handleSimilar(String messageText, long chatId) {
String responseMessage;

try {
int filmId = Integer.parseInt(messageText);
int filmId = parseInt(messageText);

ListDeserializer<FilmDeserializer> films = tmdbService.getSimilarMovies(TMDB_TOKEN, filmId);
FilmDeserializer requestedFilm = tmdbService.getMovieById(TMDB_TOKEN, filmId);
Expand Down Expand Up @@ -509,7 +558,7 @@ protected String handleRecommended(String messageText, long chatId) {
String responseMessage;

try {
int filmId = Integer.parseInt(messageText);
int filmId = parseInt(messageText);

// вызов реквеста
ListDeserializer<FilmDeserializer> films = tmdbService.getRecommendationsForMovie(TMDB_TOKEN, filmId);
Expand Down Expand Up @@ -606,7 +655,7 @@ protected String handleFindById(String messageText, long chatId) {
String responseMessage;

try {
int filmID = Integer.parseInt(messageText);
int filmID = parseInt(messageText);
FilmDeserializer film = tmdbService.getMovieById(TMDB_TOKEN, filmID);

if (film != null) {
Expand Down Expand Up @@ -643,7 +692,7 @@ protected String handleSetAge(String messageText, long chatId) {
String responseMessage;

try {
int userAge = Integer.parseInt(messageText);
int userAge = parseInt(messageText);

if (userAge >= 0 && userAge <= 100) {
responseMessage = "Спасибо! Учтем ваш ответ";
Expand Down

0 comments on commit d62fa54

Please sign in to comment.