summaryrefslogtreecommitdiff
path: root/src/main/java/com/mavlonerkinboev/animarfo/Bot.java
blob: aa5eab4a139ad59cf5dd37695b9ad40d3cab9496 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.mavlonerkinboev.animarfo;

import com.mavlonerkinboev.animarfo.anime.Anime;
import com.mavlonerkinboev.animarfo.parser.Parser;
import com.mavlonerkinboev.animarfo.telegram.user.TelegramUser;
import com.mavlonerkinboev.animarfo.telegram.user.callbackquery.UserCallbackQuery;
import com.mavlonerkinboev.animarfo.telegram.user.keyboard.UserKeyboard;
import com.mavlonerkinboev.animarfo.telegram.user.message.UserMessage;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.UpdatesListener;
import com.pengrad.telegrambot.model.User;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.request.SendPhoto;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Properties;

// TODO: 7/25/21 Добавить Аниме OVA, аниме по жанру, Дорамы, случайное аниме

public class Bot {
    public final static Properties PROPERTIES = loadProperties(new File("app.properties"));
    public final static TelegramBot TELEGRAM_BOT = new TelegramBot(PROPERTIES.getProperty("BOT_TOKEN"));
    public static TelegramUser telegramUser = new TelegramUser();

    public static void main(String[] args) {
        TELEGRAM_BOT.setUpdatesListener(updates -> {
            updates.forEach(update -> {
                if (update.callbackQuery() != null) {
                    try {
                        UserCallbackQuery.execute(update.callbackQuery());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (update.message() != null) {
                    User user = update.message().from();
                    SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm:ss z");
                    simpleDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT+5"));
                    String formattedDate = simpleDateFormat.format(new java.util.Date(update.message().date() * 1000L));
                    telegramUser = new TelegramUser(user.id(), user.isBot(), user.firstName(), user.lastName(), user.username(), formattedDate);
                    try {
                        UserMessage.execute(update.message());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            return UpdatesListener.CONFIRMED_UPDATES_ALL;
        });
    }

    public static Properties loadProperties(File file) {
        Properties properties = new Properties();
        try (FileInputStream fis = new FileInputStream(file)) {
            properties.load(fis);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }

    public static void sendAnime(String url) {
        Anime[] anime = new Anime[28];
        byte index = Anime.index;
        try {
            anime = Parser.getAnime(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Anime.setList(anime);
        TELEGRAM_BOT.execute(new SendPhoto(UserMessage.chatId, anime[index].getImage()).caption(anime[index].showInfo()).parseMode(ParseMode.HTML).replyMarkup(UserKeyboard.getCarousel()));
    }

    public static void sendAnimeByCallBackQuery() {
        Anime[] anime = Anime.list;
        byte index = Anime.index;
        TELEGRAM_BOT.execute(new SendPhoto(UserCallbackQuery.chatId, anime[index].getImage()).caption(anime[index].showInfo()).parseMode(ParseMode.HTML).replyMarkup(UserKeyboard.getCarousel()));
    }

    public static void sendSearchedAnime() throws IOException {
        String searchedAnime = Anime.searched;
        byte index = Anime.index;
        byte animeAtSearchSize = Parser.getSizeOfSearchedAnime("https://anime.anidub.life/?do=search&mode=advanced&subaction=search&titleonly=3&story=" + searchedAnime);
        if (animeAtSearchSize > 0) {
            Anime[] anime = new Anime[animeAtSearchSize];
            try {
                anime = Parser.getAnime("https://anime.anidub.life/?do=search&mode=advanced&subaction=search&titleonly=3&story=" + searchedAnime);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Anime.size = animeAtSearchSize;
            Anime.setList(anime);
            TELEGRAM_BOT.execute(new SendPhoto(UserMessage.chatId, anime[index].getImage()).caption(anime[index].showInfo()).parseMode(ParseMode.HTML).replyMarkup(UserKeyboard.getCarousel()));
        } else {
            TELEGRAM_BOT.execute(new SendMessage(UserMessage.chatId, "Прости, но я не смог найти аниме с таким названием...\nДавай поищем другое аниме!"));
            Anime.isSearching = true;
        }
    }
}