fix(parsers): increase request interval to prevent rate limiting issues (#2282)
Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
+1
-1
@@ -46,7 +46,7 @@ public class ComicvineBookParser implements BookParser {
|
|||||||
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
|
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
|
||||||
private static final Pattern SPECIAL_ISSUE_PATTERN = Pattern.compile("(annual|special|one-?shot)\\s+(\\d+)", Pattern.CASE_INSENSITIVE);
|
private static final Pattern SPECIAL_ISSUE_PATTERN = Pattern.compile("(annual|special|one-?shot)\\s+(\\d+)", Pattern.CASE_INSENSITIVE);
|
||||||
private static final Pattern YEAR_PATTERN = Pattern.compile("\\(?(\\d{4})\\)?");
|
private static final Pattern YEAR_PATTERN = Pattern.compile("\\(?(\\d{4})\\)?");
|
||||||
private static final long MIN_REQUEST_INTERVAL_MS = 1000;
|
private static final long MIN_REQUEST_INTERVAL_MS = 2000;
|
||||||
|
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
private final AppSettingService appSettingService;
|
private final AppSettingService appSettingService;
|
||||||
|
|||||||
+1
-1
@@ -133,7 +133,7 @@ public class GoodReadsParser implements BookParser {
|
|||||||
if (detailedMetadata != null) {
|
if (detailedMetadata != null) {
|
||||||
fetchedMetadata.add(detailedMetadata);
|
fetchedMetadata.add(detailedMetadata);
|
||||||
}
|
}
|
||||||
Thread.sleep(Duration.ofSeconds(1));
|
Thread.sleep(Duration.ofSeconds(2));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error fetching metadata for book: {}", preview.getGoodreadsId(), e);
|
log.error("Error fetching metadata for book: {}", preview.getGoodreadsId(), e);
|
||||||
}
|
}
|
||||||
|
|||||||
+20
@@ -24,6 +24,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -35,10 +36,12 @@ public class GoogleParser implements BookParser {
|
|||||||
private static final Pattern FOUR_DIGIT_YEAR_PATTERN = Pattern.compile("\\d{4}");
|
private static final Pattern FOUR_DIGIT_YEAR_PATTERN = Pattern.compile("\\d{4}");
|
||||||
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
|
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
|
||||||
private static final Pattern SPECIAL_CHARACTERS_PATTERN = Pattern.compile("[.,\\-\\[\\]{}()!@#$%^&*_=+|~`<>?/\";:]");
|
private static final Pattern SPECIAL_CHARACTERS_PATTERN = Pattern.compile("[.,\\-\\[\\]{}()!@#$%^&*_=+|~`<>?/\";:]");
|
||||||
|
private static final long MIN_REQUEST_INTERVAL_MS = 1500;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
private final AppSettingService appSettingService;
|
private final AppSettingService appSettingService;
|
||||||
private final HttpClient httpClient = HttpClient.newHttpClient();
|
private final HttpClient httpClient = HttpClient.newHttpClient();
|
||||||
private static final String GOOGLE_BOOKS_API_URL = "https://www.googleapis.com/books/v1/volumes";
|
private static final String GOOGLE_BOOKS_API_URL = "https://www.googleapis.com/books/v1/volumes";
|
||||||
|
private final AtomicLong lastRequestTime = new AtomicLong(0);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BookMetadata fetchTopMetadata(Book book, FetchMetadataRequest fetchMetadataRequest) {
|
public BookMetadata fetchTopMetadata(Book book, FetchMetadataRequest fetchMetadataRequest) {
|
||||||
@@ -57,6 +60,8 @@ public class GoogleParser implements BookParser {
|
|||||||
|
|
||||||
private List<BookMetadata> getMetadataListByIsbn(String isbn) {
|
private List<BookMetadata> getMetadataListByIsbn(String isbn) {
|
||||||
try {
|
try {
|
||||||
|
waitForRateLimit();
|
||||||
|
|
||||||
URI uri = UriComponentsBuilder.fromUriString(getApiUrl())
|
URI uri = UriComponentsBuilder.fromUriString(getApiUrl())
|
||||||
.queryParam("q", "isbn:" + isbn.replace("-", ""))
|
.queryParam("q", "isbn:" + isbn.replace("-", ""))
|
||||||
.build()
|
.build()
|
||||||
@@ -86,6 +91,8 @@ public class GoogleParser implements BookParser {
|
|||||||
|
|
||||||
public List<BookMetadata> getMetadataListByTerm(String term) {
|
public List<BookMetadata> getMetadataListByTerm(String term) {
|
||||||
try {
|
try {
|
||||||
|
waitForRateLimit();
|
||||||
|
|
||||||
URI uri = UriComponentsBuilder.fromUriString(getApiUrl())
|
URI uri = UriComponentsBuilder.fromUriString(getApiUrl())
|
||||||
.queryParam("q", term)
|
.queryParam("q", term)
|
||||||
.build()
|
.build()
|
||||||
@@ -225,4 +232,17 @@ public class GoogleParser implements BookParser {
|
|||||||
.toUri()
|
.toUri()
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void waitForRateLimit() {
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
long timeSinceLastRequest = now - lastRequestTime.get();
|
||||||
|
if (timeSinceLastRequest < MIN_REQUEST_INTERVAL_MS) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(MIN_REQUEST_INTERVAL_MS - timeSinceLastRequest);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lastRequestTime.set(System.currentTimeMillis());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user