Skip to content

Commit

Permalink
Multiple locale search (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
garamb1 authored May 20, 2024
1 parent 0061626 commit 170ec3f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.7.1
0.7.2
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ public ApplicationSettings applicationSettings(
return new ApplicationSettings(HTMLVersion.getByVersionName(htmlVersionValue), encodingValue);
}

@Bean
public SearchSettings searchSettings(
@Value("${retrosearch.search.locales:}") List<String> localeList) {
List<Locale> 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<String> localeList,
@Value("${retrosearch.news.api.rate.limiter}") long rateLimiter) {
List<Locale> locales = localeList.stream().map(StringUtils::parseLocaleString).toList();

return new NewsSettings(enabled, rateLimiter, locales);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Locale> locales;

public SearchSettings(List<Locale> locales) {
this.locales = locales;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public SearchResults search(String query) throws IOException, URISyntaxException
public SearchResults search(String query, String locale) throws IOException, URISyntaxException {
Map<String, String> 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<ResultEntry> resultEntryList = ddgScraper.scrapeResults(resultPage);
Expand All @@ -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("_", "-");
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
13 changes: 12 additions & 1 deletion src/main/resources/templates/fragments/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ <h4>
</th:block>

<form action="/search" method="get">
Search Query:<input type="text" th:value="${searchResults?.query}" name="query">
Search Query:
<input type="text" th:value="${searchResults?.query}" name="query">

<th:block th:unless="${#lists.isEmpty(@newsSettings.getLocales())}">
<select name="locale">
<option th:each="locale: ${@newsSettings.getLocales()}"
th:text="${locale.getDisplayCountry()}"
th:value="${locale.toString()}"
th:selected="${searchResults?.locale == locale.toString()}">
</select>
</th:block>

<input type="submit" value="Go!">
</form>

0 comments on commit 170ec3f

Please sign in to comment.