+++ /dev/null
-package net.frustratedfunctor;
-
-
-import jakarta.ws.rs.GET;
-import jakarta.ws.rs.Path;
-import jakarta.ws.rs.Produces;
-import jakarta.ws.rs.core.MediaType;
-import org.jboss.resteasy.reactive.RestQuery;
-
-@Path("/search")
-public class WordDefinitionResource {
-
- private final WordDefinitionService words;
-
- public WordDefinitionResource(WordDefinitionService words) {
- this.words = words;
- }
-
- @GET
- @Produces(MediaType.APPLICATION_JSON)
- public WordDefinition search(@RestQuery String word) {
- final var definition = words.search(word);
- return definition;
- }
-
-}
+++ /dev/null
-package net.frustratedfunctor;
-
-import jakarta.enterprise.context.ApplicationScoped;
-
-@ApplicationScoped
-public class WordDefinitionService {
-
- public WordDefinition search(String word) {
- if (word == null || word.isEmpty()) {
- throw new InvalidWordException("The word cannot be empty");
- }
- return null;
- }
-
-}
--- /dev/null
+package net.frustratedfunctor;
+
+
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.core.MediaType;
+import org.jboss.resteasy.reactive.RestQuery;
+
+@Path("/translate")
+public class WordTranslationResource {
+
+ private final WordTranslationService words;
+
+ public WordTranslationResource(WordTranslationService words) {
+ this.words = words;
+ }
+
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public WordDefinition translate(@RestQuery("q") String word) throws Exception {
+ final var definition = words.translate(word);
+ return definition;
+ }
+
+}
--- /dev/null
+package net.frustratedfunctor;
+
+import jakarta.enterprise.context.ApplicationScoped;
+import org.jsoup.Jsoup;
+
+import java.net.URI;
+import java.util.ArrayList;
+
+@ApplicationScoped
+public class WordTranslationService {
+
+ public WordDefinition translate(String word) throws Exception {
+ if (word == null || word.isEmpty()) {
+ throw new InvalidWordException("The word cannot be empty");
+ }
+
+ final var larousseUrl = URI.create("https://www.larousse.fr/dictionnaires/francais-anglais/" + word).toURL();
+ final var doc = Jsoup.parse(larousseUrl, 10_000);
+
+ final var defs = doc.select("li.itemZONESEM");
+ final var wordDefs = new ArrayList<String>();
+
+ for (final var def : defs) {
+ final var translation = def.selectFirst("span.Traduction");
+ if (translation != null) {
+ wordDefs.add(translation.text());
+ }
+ }
+
+ return new WordDefinition(
+ word,
+ wordDefs
+ );
+ }
+
+}