|
9 | 9 | import com.pengrad.telegrambot.model.Update;
|
10 | 10 | import com.pengrad.telegrambot.model.request.ParseMode;
|
11 | 11 | import com.pengrad.telegrambot.request.GetFile;
|
| 12 | +import com.pengrad.telegrambot.request.GetUpdates; |
12 | 13 | import com.pengrad.telegrambot.request.SendMessage;
|
13 | 14 | import java.awt.image.BufferedImage;
|
14 | 15 | import java.io.IOException;
|
@@ -41,33 +42,44 @@ public void setAdminId(long adminId) {
|
41 | 42 |
|
42 | 43 | public void run() {
|
43 | 44 | bot.setUpdatesListener(updates -> {
|
44 |
| - final List<Message> channelPosts = updates.stream() |
45 |
| - .map(Update::channelPost) |
46 |
| - .filter(Objects::nonNull) |
47 |
| - .filter(msg -> msg.photo() != null) |
48 |
| - .collect(Collectors.toList()); |
| 45 | + processUpdates(updates); |
| 46 | + return UpdatesListener.CONFIRMED_UPDATES_ALL; |
| 47 | + }); |
| 48 | + } |
49 | 49 |
|
50 |
| - final var similarImagesInfos = new ArrayList<SimilarImagesInfo>(); |
51 |
| - for (var post : channelPosts) { |
52 |
| - final PhotoSize photo = getSmallestPhoto(post.photo()); |
53 |
| - try { |
54 |
| - final var tgFile = bot.execute(new GetFile(photo.fileId())).file(); |
55 |
| - final var url = new URL(bot.getFullFilePath(tgFile)); |
56 |
| - final BufferedImage image = ImageIO.read(url); |
57 |
| - final var originalPost = new Post(post.chat().id(), post.messageId()); |
58 |
| - final SimilarImagesInfo info = indexer.processImage(originalPost, image); |
59 |
| - if (info.hasResults()) { |
60 |
| - similarImagesInfos.add(info); |
61 |
| - } |
62 |
| - } catch (IOException | SQLException e) { |
63 |
| - System.err.format("Error while processing photo in %s%n", linkToMessage(post)); |
| 50 | + public void runOnce() { |
| 51 | + processUpdates(bot.execute(new GetUpdates()).updates()); |
| 52 | + } |
| 53 | + |
| 54 | + private void processUpdates(List<Update> updates) { |
| 55 | + final List<Message> channelPosts = getChannelPostsWithPhotos(updates); |
| 56 | + final var similarImagesInfos = new ArrayList<SimilarImagesInfo>(); |
| 57 | + for (var post : channelPosts) { |
| 58 | + final PhotoSize photo = getSmallestPhoto(post.photo()); |
| 59 | + try { |
| 60 | + final var tgFile = bot.execute(new GetFile(photo.fileId())).file(); |
| 61 | + final var url = new URL(bot.getFullFilePath(tgFile)); |
| 62 | + final BufferedImage image = ImageIO.read(url); |
| 63 | + final var originalPost = new Post(post.chat().id(), post.messageId()); |
| 64 | + final SimilarImagesInfo info = indexer.processImage(originalPost, image); |
| 65 | + if (info.hasResults()) { |
| 66 | + similarImagesInfos.add(info); |
64 | 67 | }
|
| 68 | + } catch (IOException | SQLException e) { |
| 69 | + System.err.format("Error while processing photo in %s%n", linkToMessage(post)); |
65 | 70 | }
|
66 |
| - if (!similarImagesInfos.isEmpty()) { |
67 |
| - sendReport(similarImagesInfos); |
68 |
| - } |
69 |
| - return UpdatesListener.CONFIRMED_UPDATES_ALL; |
70 |
| - }); |
| 71 | + } |
| 72 | + if (!similarImagesInfos.isEmpty()) { |
| 73 | + sendReport(similarImagesInfos); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private List<Message> getChannelPostsWithPhotos(List<Update> updates) { |
| 78 | + return updates.stream() |
| 79 | + .map(Update::channelPost) |
| 80 | + .filter(Objects::nonNull) |
| 81 | + .filter(msg -> msg.photo() != null) |
| 82 | + .collect(Collectors.toList()); |
71 | 83 | }
|
72 | 84 |
|
73 | 85 | private void sendReport(List<SimilarImagesInfo> infos) {
|
|
0 commit comments