radiusKm as ConfigProperty + logging

This commit is contained in:
Patrick Haßel 2024-06-17 09:20:26 +02:00
parent 98c44b7e14
commit 538e99beac
4 changed files with 45 additions and 4 deletions

14
pom.xml
View File

@ -55,4 +55,18 @@
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -23,7 +23,7 @@ import java.util.concurrent.TimeUnit;
@RequiredArgsConstructor @RequiredArgsConstructor
public class KleinanzeigenApi { public class KleinanzeigenApi {
private static final String VERSCHENKEN_EPPELBORN_30KM = "https://www.kleinanzeigen.de/s-zu-verschenken/66571/seite:%d/c192l339r30"; private static final String VERSCHENKEN_EPPELBORN_30KM = "https://www.kleinanzeigen.de/s-zu-verschenken/66571/seite:%d/c192l339r%d";
private static final int FETCH_UNTIL_DUPLICATE_MAX_PAGES = 1; private static final int FETCH_UNTIL_DUPLICATE_MAX_PAGES = 1;
@ -31,6 +31,8 @@ public class KleinanzeigenApi {
private final ApplicationEventPublisher publisher; private final ApplicationEventPublisher publisher;
private final KleinanzeigenConfig config;
@Scheduled(initialDelay = 0, fixedRate = 1, timeUnit = TimeUnit.MINUTES) @Scheduled(initialDelay = 0, fixedRate = 1, timeUnit = TimeUnit.MINUTES)
public void fetch() { public void fetch() {
fetchPagesUntilDuplicate(); fetchPagesUntilDuplicate();
@ -49,7 +51,7 @@ public class KleinanzeigenApi {
private FetchResult fetchPage(final int page) { private FetchResult fetchPage(final int page) {
final FetchResult fetchResult = new FetchResult(); final FetchResult fetchResult = new FetchResult();
final Document document; final Document document;
final URI uri = URI.create(VERSCHENKEN_EPPELBORN_30KM.formatted(page)); final URI uri = URI.create(VERSCHENKEN_EPPELBORN_30KM.formatted(page, config.getRadiusKm()));
try { try {
log.debug("Fetching page: {}", uri); log.debug("Fetching page: {}", uri);
document = Jsoup.parse(uri.toURL(), 3000); document = Jsoup.parse(uri.toURL(), 3000);

View File

@ -0,0 +1,22 @@
package de.ph87.kleinanzeigen.kleinanzeigen;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "de.ph87.kleinanzeigen")
public class KleinanzeigenConfig {
private int radiusKm = 15;
@PostConstruct
public void postConstruct() {
log.info("radiusKm: {}", radiusKm);
}
}

View File

@ -141,6 +141,7 @@ public class TelegramService {
try { try {
final EditMessageCaption edit = new EditMessageCaption(chatId + "", messageId, null, createText(offer), createKeyboard(offer), null, null); final EditMessageCaption edit = new EditMessageCaption(chatId + "", messageId, null, createText(offer), createKeyboard(offer), null, null);
edit.setParseMode("Markdown"); edit.setParseMode("Markdown");
log.info("Editing Offer: {}", offer);
bot.execute(edit); bot.execute(edit);
} catch (TelegramApiException | JsonProcessingException e) { } catch (TelegramApiException | JsonProcessingException e) {
if (e.toString().endsWith("Bad Request: message to edit not found")) { if (e.toString().endsWith("Bad Request: message to edit not found")) {
@ -158,9 +159,10 @@ public class TelegramService {
try { try {
final InputFile inputFile = offer.getImageURL() == null ? new InputFile(new ByteArrayInputStream(NO_IMAGE), "[Kein Bild]") : new InputFile(offer.getImageURL()); final InputFile inputFile = offer.getImageURL() == null ? new InputFile(new ByteArrayInputStream(NO_IMAGE), "[Kein Bild]") : new InputFile(offer.getImageURL());
final SendPhoto send = new SendPhoto(CHAT_ID + "", inputFile); final SendPhoto send = new SendPhoto(CHAT_ID + "", inputFile);
log.debug("{}", send);
send.setCaption(createText(offer)); send.setCaption(createText(offer));
send.setReplyMarkup(createKeyboard(offer)); send.setReplyMarkup(createKeyboard(offer));
log.info("Sending Offer: {}", offer);
final Message message = bot.execute(send); final Message message = bot.execute(send);
offerService.setTelegramMessageId(offer, message.getMessageId()); offerService.setTelegramMessageId(offer, message.getMessageId());
@ -205,9 +207,10 @@ public class TelegramService {
private void _remove(final Integer... messageIds) { private void _remove(final Integer... messageIds) {
try { try {
log.info("Removing messages: {}", Arrays.toString(messageIds));
bot.execute(new DeleteMessages(CHAT_ID + "", Arrays.stream(messageIds).toList())); bot.execute(new DeleteMessages(CHAT_ID + "", Arrays.stream(messageIds).toList()));
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
log.error("Failed to remove Message to #{}.", CHAT_ID, e); log.error("Failed to remove Message #{}.", CHAT_ID, e);
} }
} }