From 170ec3f440aa91e047fd3d46fb88059b5d6cca33 Mon Sep 17 00:00:00 2001 From: Vincenzo Date: Mon, 20 May 2024 10:31:23 +0200 Subject: [PATCH] Multiple locale search (#20) --- VERSION | 2 +- .../retrosearch/configuration/BeanConfig.java | 8 +++++++- .../retrosearch/configuration/SearchSettings.java | 14 ++++++++++++++ .../retrosearch/ddg/client/DDGClientImpl.java | 10 ++++++++-- src/main/resources/application.properties | 1 + src/main/resources/templates/fragments/header.html | 13 ++++++++++++- 6 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/main/java/it/garambo/retrosearch/configuration/SearchSettings.java diff --git a/VERSION b/VERSION index 39e898a..7486fdb 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.7.1 +0.7.2 diff --git a/src/main/java/it/garambo/retrosearch/configuration/BeanConfig.java b/src/main/java/it/garambo/retrosearch/configuration/BeanConfig.java index 04fb85a..bba85dd 100644 --- a/src/main/java/it/garambo/retrosearch/configuration/BeanConfig.java +++ b/src/main/java/it/garambo/retrosearch/configuration/BeanConfig.java @@ -37,13 +37,19 @@ public ApplicationSettings applicationSettings( return new ApplicationSettings(HTMLVersion.getByVersionName(htmlVersionValue), encodingValue); } + @Bean + public SearchSettings searchSettings( + @Value("${retrosearch.search.locales:}") List localeList) { + List locales = localeList.stream().map(StringUtils::parseLocaleString).toList(); + return new SearchSettings(locales); + } + @Bean public NewsSettings newsSettings( @Value("${retrosearch.news.enable}") boolean enabled, @Value("${retrosearch.news.api.locales}") List localeList, @Value("${retrosearch.news.api.rate.limiter}") long rateLimiter) { List locales = localeList.stream().map(StringUtils::parseLocaleString).toList(); - return new NewsSettings(enabled, rateLimiter, locales); } diff --git a/src/main/java/it/garambo/retrosearch/configuration/SearchSettings.java b/src/main/java/it/garambo/retrosearch/configuration/SearchSettings.java new file mode 100644 index 0000000..93ed06f --- /dev/null +++ b/src/main/java/it/garambo/retrosearch/configuration/SearchSettings.java @@ -0,0 +1,14 @@ +package it.garambo.retrosearch.configuration; + +import java.util.List; +import java.util.Locale; +import lombok.Getter; + +@Getter +public class SearchSettings { + private final List locales; + + public SearchSettings(List locales) { + this.locales = locales; + } +} diff --git a/src/main/java/it/garambo/retrosearch/ddg/client/DDGClientImpl.java b/src/main/java/it/garambo/retrosearch/ddg/client/DDGClientImpl.java index c5b3389..49de395 100644 --- a/src/main/java/it/garambo/retrosearch/ddg/client/DDGClientImpl.java +++ b/src/main/java/it/garambo/retrosearch/ddg/client/DDGClientImpl.java @@ -29,8 +29,10 @@ public SearchResults search(String query) throws IOException, URISyntaxException public SearchResults search(String query, String locale) throws IOException, URISyntaxException { Map searchParams = Map.of( - DDGConstants.QUERY_PARAM_NAME, query, - DDGConstants.REGION_PARAM_NAME, locale); + DDGConstants.QUERY_PARAM_NAME, + query, + DDGConstants.REGION_PARAM_NAME, + getDDGLocale(locale)); String resultPage = httpService.get(URI.create(DDGConstants.BASE_URL), searchParams); List resultEntryList = ddgScraper.scrapeResults(resultPage); @@ -41,4 +43,8 @@ public SearchResults search(String query, String locale) throws IOException, URI .resultEntries(resultEntryList) .build(); } + + private String getDDGLocale(String locale) { + return locale.toLowerCase().replace("_", "-"); + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 04262f7..c7ffc99 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,6 +3,7 @@ server.compression.enabled=true server.error.whitelabel.enabled=false retrosearch.encoding=UTF-8 retrosearch.html.version=3.2 +retrosearch.search.locales=${SEARCH_LOCALES:en_US,en_GB,it_IT,de_DE} retrosearch.news.enable=${NEWS_ACTIVE:false} retrosearch.news.api.key=${NEWS_API_KEY:} retrosearch.news.api.rate.limiter=${NEWS_API_RATE_LIMITER:3000} diff --git a/src/main/resources/templates/fragments/header.html b/src/main/resources/templates/fragments/header.html index 8dfb0f8..1abd73c 100644 --- a/src/main/resources/templates/fragments/header.html +++ b/src/main/resources/templates/fragments/header.html @@ -20,6 +20,17 @@

- Search Query: + Search Query: + + + + + +
\ No newline at end of file