Skip to content

Commit

Permalink
- Optimized recipe scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
Chip3211 committed Sep 17, 2024
1 parent 22e6363 commit 6e04ce3
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 330 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.12.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.flavormate.ad_configurations.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import de.flavormate.ba_entities.scrape.model.ImageObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ImageDeserializer extends JsonDeserializer<List<String>> {
@Override
public List<String> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
List<String> result = new ArrayList<>();

JsonToken currentToken = p.currentToken();
if (currentToken == JsonToken.START_ARRAY) {
// If the token is the start of an array, iterate through it
while (p.nextToken() != JsonToken.END_ARRAY) {
if (p.currentToken() == JsonToken.VALUE_STRING) {
// Add string images to the list
result.add(p.getValueAsString());
} else if (p.currentToken() == JsonToken.START_OBJECT) {

// Deserialize ImageObject and extract the URL
ImageObject imageObject = p.readValueAs(ImageObject.class);
result.add(imageObject.url());
}
}
} else if (currentToken == JsonToken.VALUE_STRING) {
// If it's a single string, just add it to the result
result.add(p.getValueAsString());
} else if (currentToken == JsonToken.START_OBJECT) {
// Deserialize ImageObject if it's an object
ImageObject imageObject = p.readValueAs(ImageObject.class);
result.add(imageObject.url());
} else {
// Handle unexpected types by throwing an error or logging
ctxt.reportInputMismatch(
List.class,
"Expected either a string, array, or an ImageObject, but got: " + currentToken
);
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.flavormate.ad_configurations.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ListOrStringDeserializer extends JsonDeserializer<List<Object>> {

@Override
public List<Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
List<Object> result = new ArrayList<>();

JsonToken currentToken = p.currentToken();
if (currentToken == JsonToken.START_ARRAY) {
// If the token is the start of an array, read it as a list
result = p.readValueAs(List.class);
} else if (currentToken == JsonToken.VALUE_STRING) {
// If the token is a single string, add it to the list
result.add(p.getValueAsString());
} else {
// Handle unexpected cases by throwing an exception
ctxt.reportInputMismatch(
List.class,
"Expected either an array or a string for recipeIngredient or recipeInstructions, but got: " + currentToken
);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import de.flavormate.ba_entities.recipe.enums.RecipeDiet;
import de.flavormate.ba_entities.recipe.model.Recipe;
import de.flavormate.ba_entities.recipe.service.RecipeService;
import de.flavormate.ba_entities.recipe.service.ScraperService;
import de.flavormate.ba_entities.recipe.wrapper.ChangeOwnerForm;
import de.flavormate.ba_entities.recipe.wrapper.RecipeDraft;
import de.flavormate.ba_entities.recipe.wrapper.ScrapeResponse;
import de.flavormate.ba_entities.scrape.service.ScrapeService;
import de.flavormate.utils.RequestUtils;
import org.springframework.data.domain.Page;
import org.springframework.security.access.annotation.Secured;
Expand All @@ -25,16 +25,16 @@
public class RecipeController implements ICRUDController<Recipe, RecipeDraft>, ISearchDietController<Recipe>, IPageableDietController<Recipe> {

private final RecipeService service;
private final ScraperService scraperService;
private final ScrapeService scrapeService;

protected RecipeController(RecipeService service, ScraperService scraperService) {
protected RecipeController(RecipeService service, ScrapeService scrapeService) {
this.service = service;
this.scraperService = scraperService;
this.scrapeService = scrapeService;
}

@GetMapping("/crawl")
public ScrapeResponse crawl(@RequestParam String url) throws Exception {
return scraperService.scrape(url);
return scrapeService.fetchAndParseRecipe(url);
}

@GetMapping("/random/{diet}")
Expand All @@ -59,7 +59,6 @@ public Boolean changeOwner(@PathVariable Long id, @RequestBody ChangeOwnerForm f
return service.changeOwner(id, form);
}


@Secured({"ROLE_USER", "ROLE_ANONYMOUS"})
@GetMapping("/{id}/bring/{serving}")
public String getBring(@PathVariable Long id, @PathVariable("serving") Integer serving)
Expand Down
Loading

0 comments on commit 6e04ce3

Please sign in to comment.