feat: add language restriction option for google parser and improve search metadata by title/author query (#1502)

* feat: add language restriction option for google parser and improve search metadata by title/author query

* Fix syntax error.
This commit is contained in:
Yann
2025-11-20 21:14:12 -07:00
committed by GitHub
parent 3a8a46b3cc
commit 27033560c0
5 changed files with 55 additions and 5 deletions
@@ -21,6 +21,7 @@ public class MetadataProviderSettings {
@Data
public static class Google {
private boolean enabled;
private String language;
}
@Data
@@ -5,6 +5,7 @@ import com.adityachandel.booklore.model.dto.BookMetadata;
import com.adityachandel.booklore.model.dto.response.GoogleBooksApiResponse;
import com.adityachandel.booklore.model.dto.request.FetchMetadataRequest;
import com.adityachandel.booklore.model.enums.MetadataProvider;
import com.adityachandel.booklore.service.appsettings.AppSettingService;
import com.adityachandel.booklore.util.BookUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
@@ -35,6 +36,7 @@ public class GoogleParser implements BookParser {
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
private static final Pattern SPECIAL_CHARACTERS_PATTERN = Pattern.compile("[.,\\-\\[\\]{}()!@#$%^&*_=+|~`<>?/\";:]");
private final ObjectMapper objectMapper;
private final AppSettingService appSettingService;
private final HttpClient httpClient = HttpClient.newHttpClient();
private static final String GOOGLE_BOOKS_API_URL = "https://www.googleapis.com/books/v1/volumes";
@@ -55,7 +57,7 @@ public class GoogleParser implements BookParser {
private List<BookMetadata> getMetadataListByIsbn(String isbn) {
try {
URI uri = UriComponentsBuilder.fromUriString(GOOGLE_BOOKS_API_URL)
URI uri = UriComponentsBuilder.fromUriString(getApiUrl())
.queryParam("q", "isbn:" + isbn.replace("-", ""))
.build()
.toUri();
@@ -84,7 +86,7 @@ public class GoogleParser implements BookParser {
public List<BookMetadata> getMetadataListByTerm(String term) {
try {
URI uri = UriComponentsBuilder.fromUriString(GOOGLE_BOOKS_API_URL)
URI uri = UriComponentsBuilder.fromUriString(getApiUrl())
.queryParam("q", term)
.build()
.toUri();
@@ -175,11 +177,11 @@ public class GoogleParser implements BookParser {
if (searchTerm != null) {
searchTerm = SPECIAL_CHARACTERS_PATTERN.matcher(searchTerm).replaceAll("").trim();
searchTerm = truncateToMaxLength(searchTerm, 60);
searchTerm = "intitle:" + truncateToMaxLength(searchTerm, 60);
}
if (searchTerm != null && request.getAuthor() != null && !request.getAuthor().isEmpty()) {
searchTerm += " " + request.getAuthor();
searchTerm += " inauthor:" + request.getAuthor();
}
return searchTerm;
@@ -209,4 +211,18 @@ public class GoogleParser implements BookParser {
return null;
}
}
private String getApiUrl() {
String language = appSettingService.getAppSettings().getMetadataProviderSettings().getGoogle().getLanguage();
if (language == null || language.isEmpty()) {
return GOOGLE_BOOKS_API_URL;
}
return UriComponentsBuilder.fromUriString(GOOGLE_BOOKS_API_URL)
.queryParam("langRestrict", language)
.build()
.toUri()
.toString();
}
}
@@ -65,6 +65,19 @@
<label class="provider-label">Google</label>
</div>
</div>
<div class="provider-config">
<div class="config-field">
<label class="config-label">Language restriction</label>
<p-select
[options]="googleLanguages"
[(ngModel)]="selectedGoogleLanguage"
[showClear]="!!selectedGoogleLanguage"
placeholder="Select Language"
class="config-select">
</p-select>
</div>
</div>
</div>
<div class="provider-item">
@@ -52,6 +52,21 @@ export class MetadataProviderSettingsComponent implements OnInit {
selectedAmazonDomain = 'com';
googleLanguages = [
{label: 'Dutch', value: 'nl'},
{label: 'English', value: 'en'},
{label: 'French', value: 'fr'},
{label: 'German', value: 'de'},
{label: 'Italian', value: 'it'},
{label: 'Japanese', value: 'ja'},
{label: 'Polish', value: 'pl'},
{label: 'Portuguese', value: 'pt'},
{label: 'Spanish', value: 'es'},
{label: 'Swedish', value: 'sv'}
];
selectedGoogleLanguage = '';
hardcoverToken: string = '';
amazonCookie: string = '';
hardcoverEnabled: boolean = false;
@@ -80,6 +95,7 @@ export class MetadataProviderSettingsComponent implements OnInit {
this.selectedAmazonDomain = metadataProviderSettings?.amazon?.domain ?? 'com';
this.goodreadsEnabled = metadataProviderSettings?.goodReads?.enabled ?? false;
this.googleEnabled = metadataProviderSettings?.google?.enabled ?? false;
this.selectedGoogleLanguage = metadataProviderSettings?.google?.language ?? '';
this.hardcoverToken = metadataProviderSettings?.hardcover?.apiKey ?? '';
this.hardcoverEnabled = metadataProviderSettings?.hardcover?.enabled ?? false;
this.comicvineEnabled = metadataProviderSettings?.comicvine?.enabled ?? false;
@@ -119,7 +135,10 @@ export class MetadataProviderSettingsComponent implements OnInit {
},
goodReads: {enabled: this.goodreadsEnabled},
google: {enabled: this.googleEnabled},
google: {
enabled: this.googleEnabled,
language: this.selectedGoogleLanguage,
},
hardcover: {
enabled: this.hardcoverEnabled,
apiKey: this.hardcoverToken.trim()
@@ -60,6 +60,7 @@ export interface Amazon {
export interface Google {
enabled: boolean;
language: string;
}
export interface Goodreads {