Advertisement
GAP172

Не удалять

Apr 10th, 2021
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.06 KB | None | 0 0
  1. import org.apache.http.HttpResponse;
  2. import org.apache.http.client.methods.HttpGet;
  3. import org.apache.http.impl.client.CloseableHttpClient;
  4. import org.apache.http.impl.client.HttpClients;
  5. import org.apache.http.util.EntityUtils;
  6. import org.json.JSONArray;
  7. import org.json.JSONObject;
  8. import org.telegram.telegrambots.bots.TelegramLongPollingBot;
  9. import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
  10. import org.telegram.telegrambots.meta.api.objects.Message;
  11. import org.telegram.telegrambots.meta.api.objects.Update;
  12. import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
  13. import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
  14. import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
  15.  
  16. import java.util.ArrayList;
  17. import java.util.List;
  18.  
  19. public class Kino extends TelegramLongPollingBot {
  20.  
  21.     public Message message;
  22.     public Long chatId;
  23.  
  24.     public int id;
  25.     public String iframe_src;
  26.     public String description;
  27.  
  28.     public static String typeVideo = "";
  29.     public String jsonCdn;
  30.  
  31.  
  32.     @Override
  33.     public void onUpdateReceived(Update update) {
  34.         id = 0;
  35.         Message message = update.getMessage();
  36.         this.message = message;
  37.         if (message != null && message.hasText()) {
  38.             if (message.getText().equals("/start")) {
  39.                 try {
  40.                     start(update);
  41.                 } catch (Exception ignored) {
  42.                 }
  43.             } else
  44.                 kino(update);
  45.         }
  46.     }
  47.  
  48.     public void start(Update update) {
  49.         DatabaseHandler databaseHandler = new DatabaseHandler();
  50.         Long chatId = update.getMessage().getChatId();
  51.         this.chatId = chatId;
  52.         databaseHandler.recordChatId(chatId);
  53.         sendMsg(update.getMessage(), "\uD83D\uDC49 Отправьте мне название фильма или сериала как оно пишется в Кинопоиске.");
  54.     }
  55.  
  56.     public void kino(Update update) {
  57.         Message text = update.getMessage();
  58.         String link = text.getText().replaceAll(" ", "%20");
  59.         CloseableHttpClient httpclient = HttpClients.createDefault();
  60.         HttpGet getVideoCdn = new HttpGet("https://videocdn.tv/api/short?api_token=92nAbf1fqo3RGGHfOQkRVY2OGS6mHJ1C&title=" + link);
  61.         try {
  62.             HttpResponse responseCdn = httpclient.execute(getVideoCdn);
  63.             String jsonCdn = EntityUtils.toString(responseCdn.getEntity());
  64.             this.jsonCdn = jsonCdn;
  65.             //Video CND
  66.             JSONObject obj = new JSONObject(jsonCdn);
  67.             JSONArray arr = obj.getJSONArray("data");
  68.             for (int i = 0; i < arr.length(); i++) {
  69.                 int id = arr.getJSONObject(i).getInt("kp_id");
  70.                 this.iframe_src = arr.getJSONObject(i).getString("iframe_src");
  71.                 this.id = id;
  72.             }
  73.            
  74.  
  75.             //Kinipoisk
  76.             HttpGet httpgetKino = new HttpGet("https://kinopoiskapiunofficial.tech/api/v2.1/films/" + id);
  77.             httpgetKino.addHeader("User-Agent", "Googlebot");
  78.             httpgetKino.addHeader("X-API-KEY", "b4aeb659-f634-4db9-855e-01a79339a286");
  79.             HttpResponse response = httpclient.execute(httpgetKino);
  80.             String json = EntityUtils.toString(response.getEntity());
  81.             JSONObject objKino = new JSONObject(json);
  82.             JSONObject arrKino = objKino.getJSONObject("data");
  83.             String name = arrKino.getString("nameRu");
  84.             String year = arrKino.getString("year");
  85.             String poster = arrKino.getString("posterUrl");
  86.             Object value = arrKino.get("description");
  87.  
  88.             // Тип
  89.             String engType = arrKino.getString("type");
  90.             if (engType.equalsIgnoreCase("FILM")) {
  91.                 typeVideo = " фильм";
  92.             } else if (engType.equalsIgnoreCase("MOVIE")) {
  93.                 typeVideo = " сериал";
  94.             } else if (engType.equalsIgnoreCase("TV_SHOW")) {
  95.                 typeVideo = " тв-шоу";
  96.             }
  97.  
  98.             // описание есть или нет
  99.             if (value == JSONObject.NULL) {
  100.                 description = "";
  101.             } else {
  102.                 this.description = arrKino.getString("description");
  103.             }
  104.             SendMessage sendMesInl = sendInl(update.getMessage(), "<b>\uD83C\uDFA5 " + name + " (" + year + ")</b>"
  105.                     + "\n\n<em>" + description + "</em>\n"
  106.                     + "<a href=\"" + poster + "\">\t&#160;</a>\n");
  107.             sendMesInl.setReplyMarkup(getinlineKeyboardMarkup());
  108.             try {
  109.                 execute(sendMesInl);
  110.             } catch (TelegramApiException e) {
  111.                 e.printStackTrace();
  112.             }
  113.         } catch (Exception e) {
  114.             sendMsg(update.getMessage(), "\uD83E\uDD37\u200D♂️Результатов не найдено. Возможно фильма или сериала с таким названием нет или вы ввели название с ошибкой.\n\n" +
  115.                     "Попробуйте еще раз! Отправьте мне название фильма или сериала как оно пишется в Кинопоиске. <b>Год фильма или сериала, какой сезон и какая серия при поиске писать не нужно!!</b>");
  116.         }
  117.     }
  118.  
  119.     public void sendMsg(Message message, String text) {
  120.         SendMessage sendMessage = new SendMessage();
  121.         sendMessage.enableMarkdown(true);
  122.         sendMessage.enableHtml(true);
  123.         sendMessage.setChatId(message.getChatId().toString());
  124.         sendMessage.setText(text);
  125.         try {
  126.             execute(sendMessage);
  127.         } catch (TelegramApiException e) {
  128.             e.printStackTrace();
  129.         }
  130.     }
  131.  
  132.  
  133.     public SendMessage sendInl(Message message, String text) {
  134.         SendMessage sendMessage = new SendMessage();
  135.         sendMessage.enableMarkdown(true);
  136.         sendMessage.enableHtml(true);
  137.         sendMessage.setChatId(message.getChatId().toString());
  138.         sendMessage.setText(text);
  139.         return sendMessage;
  140.     }
  141.  
  142.     public InlineKeyboardMarkup getinlineKeyboardMarkup() {
  143.         String url = "http:" + iframe_src;
  144.         InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
  145.         InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
  146.         inlineKeyboardButton.setText("Смотреть" + typeVideo);
  147.         inlineKeyboardButton.setUrl(url);
  148.         List<InlineKeyboardButton> keyboardButtonsRow = new ArrayList<>();
  149.         keyboardButtonsRow.add(inlineKeyboardButton);
  150.         List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
  151.         rowList.add(keyboardButtonsRow);
  152.         inlineKeyboardMarkup.setKeyboard(rowList);
  153.         return inlineKeyboardMarkup;
  154.     }
  155.  
  156.     @Override
  157.     public String getBotUsername() {
  158.         return Const.TG_NAME;
  159.     }
  160.  
  161.     @Override
  162.     public String getBotToken() {
  163.         return Const.TG_TOKEN;
  164.     }
  165. }
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement