Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import org.telegram.telegrambots.bots.TelegramLongPollingBot;
- import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
- import org.telegram.telegrambots.meta.api.objects.Message;
- import org.telegram.telegrambots.meta.api.objects.Update;
- import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
- import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
- import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
- import java.util.ArrayList;
- import java.util.List;
- public class Kino extends TelegramLongPollingBot {
- public Message message;
- public Long chatId;
- public int id;
- public String iframe_src;
- public String description;
- public static String typeVideo = "";
- public String jsonCdn;
- @Override
- public void onUpdateReceived(Update update) {
- id = 0;
- Message message = update.getMessage();
- this.message = message;
- if (message != null && message.hasText()) {
- if (message.getText().equals("/start")) {
- try {
- start(update);
- } catch (Exception ignored) {
- }
- } else
- kino(update);
- }
- }
- public void start(Update update) {
- DatabaseHandler databaseHandler = new DatabaseHandler();
- Long chatId = update.getMessage().getChatId();
- this.chatId = chatId;
- databaseHandler.recordChatId(chatId);
- sendMsg(update.getMessage(), "\uD83D\uDC49 Отправьте мне название фильма или сериала как оно пишется в Кинопоиске.");
- }
- public void kino(Update update) {
- Message text = update.getMessage();
- String link = text.getText().replaceAll(" ", "%20");
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpGet getVideoCdn = new HttpGet("https://videocdn.tv/api/short?api_token=92nAbf1fqo3RGGHfOQkRVY2OGS6mHJ1C&title=" + link);
- try {
- HttpResponse responseCdn = httpclient.execute(getVideoCdn);
- String jsonCdn = EntityUtils.toString(responseCdn.getEntity());
- this.jsonCdn = jsonCdn;
- //Video CND
- JSONObject obj = new JSONObject(jsonCdn);
- JSONArray arr = obj.getJSONArray("data");
- for (int i = 0; i < arr.length(); i++) {
- int id = arr.getJSONObject(i).getInt("kp_id");
- this.iframe_src = arr.getJSONObject(i).getString("iframe_src");
- this.id = id;
- }
- //Kinipoisk
- HttpGet httpgetKino = new HttpGet("https://kinopoiskapiunofficial.tech/api/v2.1/films/" + id);
- httpgetKino.addHeader("User-Agent", "Googlebot");
- httpgetKino.addHeader("X-API-KEY", "b4aeb659-f634-4db9-855e-01a79339a286");
- HttpResponse response = httpclient.execute(httpgetKino);
- String json = EntityUtils.toString(response.getEntity());
- JSONObject objKino = new JSONObject(json);
- JSONObject arrKino = objKino.getJSONObject("data");
- String name = arrKino.getString("nameRu");
- String year = arrKino.getString("year");
- String poster = arrKino.getString("posterUrl");
- Object value = arrKino.get("description");
- // Тип
- String engType = arrKino.getString("type");
- if (engType.equalsIgnoreCase("FILM")) {
- typeVideo = " фильм";
- } else if (engType.equalsIgnoreCase("MOVIE")) {
- typeVideo = " сериал";
- } else if (engType.equalsIgnoreCase("TV_SHOW")) {
- typeVideo = " тв-шоу";
- }
- // описание есть или нет
- if (value == JSONObject.NULL) {
- description = "";
- } else {
- this.description = arrKino.getString("description");
- }
- SendMessage sendMesInl = sendInl(update.getMessage(), "<b>\uD83C\uDFA5 " + name + " (" + year + ")</b>"
- + "\n\n<em>" + description + "</em>\n"
- + "<a href=\"" + poster + "\">\t </a>\n");
- sendMesInl.setReplyMarkup(getinlineKeyboardMarkup());
- try {
- execute(sendMesInl);
- } catch (TelegramApiException e) {
- e.printStackTrace();
- }
- } catch (Exception e) {
- sendMsg(update.getMessage(), "\uD83E\uDD37\u200D♂️Результатов не найдено. Возможно фильма или сериала с таким названием нет или вы ввели название с ошибкой.\n\n" +
- "Попробуйте еще раз! Отправьте мне название фильма или сериала как оно пишется в Кинопоиске. <b>Год фильма или сериала, какой сезон и какая серия при поиске писать не нужно!!</b>");
- }
- }
- public void sendMsg(Message message, String text) {
- SendMessage sendMessage = new SendMessage();
- sendMessage.enableMarkdown(true);
- sendMessage.enableHtml(true);
- sendMessage.setChatId(message.getChatId().toString());
- sendMessage.setText(text);
- try {
- execute(sendMessage);
- } catch (TelegramApiException e) {
- e.printStackTrace();
- }
- }
- public SendMessage sendInl(Message message, String text) {
- SendMessage sendMessage = new SendMessage();
- sendMessage.enableMarkdown(true);
- sendMessage.enableHtml(true);
- sendMessage.setChatId(message.getChatId().toString());
- sendMessage.setText(text);
- return sendMessage;
- }
- public InlineKeyboardMarkup getinlineKeyboardMarkup() {
- String url = "http:" + iframe_src;
- InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
- InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
- inlineKeyboardButton.setText("Смотреть" + typeVideo);
- inlineKeyboardButton.setUrl(url);
- List<InlineKeyboardButton> keyboardButtonsRow = new ArrayList<>();
- keyboardButtonsRow.add(inlineKeyboardButton);
- List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
- rowList.add(keyboardButtonsRow);
- inlineKeyboardMarkup.setKeyboard(rowList);
- return inlineKeyboardMarkup;
- }
- @Override
- public String getBotUsername() {
- return Const.TG_NAME;
- }
- @Override
- public String getBotToken() {
- return Const.TG_TOKEN;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement