44 lines
1.2 KiB
Java
44 lines
1.2 KiB
Java
package de.ph87.kleinanzeigen.api;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
|
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
|
|
@Slf4j
|
|
@SuppressWarnings({"InfiniteLoopStatement", "SameParameterValue", "SynchronizationOnLocalVariableOrMethodParameter"})
|
|
public class Main {
|
|
|
|
private static Bot bot;
|
|
|
|
private static final Kleinanzeigen kleinanzeigen = new Kleinanzeigen(offer -> bot.remove(List.of(offer)));
|
|
|
|
public static void main(String[] args) throws IOException, TelegramApiException {
|
|
bot = new Bot(kleinanzeigen::ignore, kleinanzeigen::findByTelegramMessageId, kleinanzeigen::remember);
|
|
try {
|
|
while (true) {
|
|
handle(bot);
|
|
waitSeconds(60);
|
|
}
|
|
} catch (InterruptedException e) {
|
|
log.warn(e.toString());
|
|
} finally {
|
|
bot.stop();
|
|
}
|
|
}
|
|
|
|
private static void handle(final Bot bot) {
|
|
kleinanzeigen.fetchUntilDuplicate(5);
|
|
kleinanzeigen.findAll().stream().filter(offer -> offer.getTelegramMessageId() == null).forEach(bot::send);
|
|
}
|
|
|
|
private static void waitSeconds(final long seconds) throws InterruptedException {
|
|
final Object lock = new Object();
|
|
synchronized (lock) {
|
|
lock.wait(seconds * 1000);
|
|
}
|
|
}
|
|
|
|
}
|