Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple locale search #20

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading