Offer: zipcode,distance not nullable

This commit is contained in:
Patrick Haßel 2024-06-18 10:22:35 +02:00
parent 6de379e2fe
commit 541688a726
6 changed files with 25 additions and 35 deletions

View File

@ -42,8 +42,10 @@ public class KleinanzeigenApi {
private void tryParse(final Element article, final URI uri) { private void tryParse(final Element article, final URI uri) {
try { try {
final OfferCreate create = new OfferCreate(article, uri); final OfferCreate create = new OfferCreate(article, uri);
if (create.getDistance() <= radius) {
offerService.updateOrCreate(create); offerService.updateOrCreate(create);
} catch (NumberFormatException e) { }
} catch (NumberFormatException | DateTimeException | LocationNotFound e) {
log.error("Failed to parse Offer:\n{}\n", article.outerHtml(), e); log.error("Failed to parse Offer:\n{}\n", article.outerHtml(), e);
} }
} }

View File

@ -0,0 +1,5 @@
package de.ph87.kleinanzeigen.kleinanzeigen.offer;
public class LocationNotFound extends Exception {
}

View File

@ -50,13 +50,11 @@ public class Offer {
@Column(nullable = false) @Column(nullable = false)
private String location; private String location;
@Column @Column(nullable = false)
@Nullable private String zipcode;
private String zipcode = null;
@Column @Column(nullable = false)
@Nullable private int distance;
private Integer distance = null;
@NonNull @NonNull
@Column(nullable = false) @Column(nullable = false)

View File

@ -38,11 +38,10 @@ public class OfferCreate {
@ToString.Include @ToString.Include
private final String location; private final String location;
@Nullable @NonNull
private final String zipcode; private final String zipcode;
@Nullable private final int distance;
private final Integer distance;
@NonNull @NonNull
private final String description; private final String description;
@ -60,16 +59,13 @@ public class OfferCreate {
articleDate = parseDate(article.select(".aditem-main--top--right").text()); articleDate = parseDate(article.select(".aditem-main--top--right").text());
articleURL = uri.resolve(article.select(".aditem-image a").attr("href")).toString(); articleURL = uri.resolve(article.select(".aditem-image a").attr("href")).toString();
final String locationString = article.select(".aditem-main--top--left").text(); final String locationString = article.select(".aditem-main--top--left").text();
final Matcher locationMatcher = Pattern.compile("^(?<zipcode>\\d+) (?<location>.+) \\((:?ca.)?\\s*(?<distance>\\d+)\\s*km\\s*\\)$").matcher(locationString); final Matcher locationMatcher = Pattern.compile("^(?<zipcode>\\d+) (?<location>.+) \\((:?ca.)?\\s*(?<distance>\\d+(?:[,.]\\d+)?)\\s*km\\s*\\)$").matcher(locationString);
if (!locationMatcher.find()) { if (!locationMatcher.find()) {
zipcode = ""; throw new LocationNotFound();
location = locationString; }
distance = null;
} else {
zipcode = locationMatcher.group("zipcode"); zipcode = locationMatcher.group("zipcode");
location = locationMatcher.group("location"); location = locationMatcher.group("location");
distance = Integer.parseInt(locationMatcher.group("distance")); distance = Integer.parseInt(locationMatcher.group("distance"));
}
imageURL = getImageURL(articleURL); imageURL = getImageURL(articleURL);
} }

View File

@ -6,8 +6,6 @@ import lombok.NonNull;
import lombok.ToString; import lombok.ToString;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
@Getter @Getter
@ToString(onlyExplicitlyIncluded = true) @ToString(onlyExplicitlyIncluded = true)
@ -39,11 +37,10 @@ public class OfferDto {
@NonNull @NonNull
private final String location; private final String location;
@Nullable @NonNull
private final String zipcode; private final String zipcode;
@Nullable private final int distance;
private final Integer distance;
@NonNull @NonNull
private final String description; private final String description;
@ -71,16 +68,6 @@ public class OfferDto {
imageURL = offer.getImageURL(); imageURL = offer.getImageURL();
} }
public String combineLocation() {
final List<String> list = new ArrayList<>();
if (zipcode != null) {
list.add(zipcode);
}
list.add(location);
if (distance != null) {
list.add("(" + distance + " km)");
}
return String.join(" ", list);
} }
} }

View File

@ -188,7 +188,9 @@ public class TelegramService {
private String createText(final OfferDto offer) { private String createText(final OfferDto offer) {
return "%s\n%s\n%s\n%s\n".formatted( return "%s\n%s\n%s\n%s\n".formatted(
offer.getTitle().replaceAll("\\[", "(").replaceAll("]", ")"), offer.getTitle().replaceAll("\\[", "(").replaceAll("]", ")"),
offer.combineLocation(), offer.getZipcode(),
offer.getLocation(),
offer.getDistance(),
offer.getDescription(), offer.getDescription(),
offer.getArticleURL() offer.getArticleURL()
); );