Skip to content

Commit 16e5f38

Browse files
committed
Refactor. Ability to run once
1 parent 55743fb commit 16e5f38

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

src/main/java/com/annimon/similarimagesbot/BotHandler.java

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.pengrad.telegrambot.model.Update;
1010
import com.pengrad.telegrambot.model.request.ParseMode;
1111
import com.pengrad.telegrambot.request.GetFile;
12+
import com.pengrad.telegrambot.request.GetUpdates;
1213
import com.pengrad.telegrambot.request.SendMessage;
1314
import java.awt.image.BufferedImage;
1415
import java.io.IOException;
@@ -41,33 +42,44 @@ public void setAdminId(long adminId) {
4142

4243
public void run() {
4344
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+
}
4949

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);
6467
}
68+
} catch (IOException | SQLException e) {
69+
System.err.format("Error while processing photo in %s%n", linkToMessage(post));
6570
}
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());
7183
}
7284

7385
private void sendReport(List<SimilarImagesInfo> infos) {

src/main/java/com/annimon/similarimagesbot/Main.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ public static void main(String[] args) {
1010
final ImageIndexer indexer = new ImageIndexer();
1111
final var handler = new BotHandler(botToken, indexer);
1212
handler.setAdminId(longProp("ADMIN_ID").orElse(0L));
13-
handler.run();
13+
if (args.length == 1 && args[0].equalsIgnoreCase("once")) {
14+
handler.runOnce();
15+
} else {
16+
handler.run();
17+
}
1418
}
1519

1620
private static Optional<String> stringProp(String name) {

0 commit comments

Comments
 (0)