Skip to content

Commit

Permalink
- 'date published' accepts more formats
Browse files Browse the repository at this point in the history
- started writing readme
  • Loading branch information
joethei committed Feb 11, 2021
1 parent 27b969e commit 84ec4a2
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 12 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# RecipeParser
![t](https://shields.io/github/v/release/joethei/RecipeParser?include_prereleases)

Parse Recipes from Websites and files that use the json-ld format

## Getting started

```xml
<dependency>
<groupId>xyz.joethei</groupId>
<artifactId>receipe-parser</artifactId>
<version>${version}</version>
</dependency>
```

```java
var url = //Website url / path to text file containing recipe data
Recipe recipe = RecipeParser.parse(url);
```
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>xyz.joethei</groupId>
<artifactId>recipe-parser</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -175,6 +175,12 @@
<version>1.8</version>
</dependency>

<dependency>
<groupId>com.github.sisyphsu</groupId>
<artifactId>dateparser</artifactId>
<version>1.0.7</version>
</dependency>


<!--Testing-->
<dependency>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/xyz/joethei/recipe/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.immutables.value.Value;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -39,7 +40,7 @@ public interface Recipe {

String recipeInstructions();

Optional<LocalDate> datePublished();
Optional<LocalDateTime> datePublished();

List<String> recipeIngredient();

Expand Down
13 changes: 8 additions & 5 deletions src/main/java/xyz/joethei/recipe/RecipeDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@

package xyz.joethei.recipe;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Optional;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.github.sisyphsu.dateparser.DateParserUtils;
import org.apache.commons.text.StringEscapeUtils;

import java.io.IOException;
import java.time.LocalDate;
import java.util.Optional;

/**
* @author Johannes Theiner
* @version 0.1
* @since 2020-09-29
**/
public class RecipeDeserializer extends StdDeserializer<Recipe> {

private static final long serialVersionUID = -7183438859870030590L;

public RecipeDeserializer() {
this(null);
}
Expand Down Expand Up @@ -68,7 +71,7 @@ public Recipe deserialize(JsonParser jsonParser, DeserializationContext deserial
if (node.get("datePublished") != null) {
var date = getIfExists(node, "datePublished");
date.ifPresent(s -> {
var localDate = LocalDate.parse(s);
LocalDateTime localDate = DateParserUtils.parseDateTime(s);
builder.datePublished(localDate);
});
}
Expand Down
16 changes: 11 additions & 5 deletions src/test/java/xyz/joethei/recipe/test/TextParsing.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

package xyz.joethei.recipe.test;

import org.junit.jupiter.api.Test;
import xyz.joethei.recipe.Recipe;
import xyz.joethei.recipe.RecipeParser;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

import org.junit.jupiter.api.Test;
import xyz.joethei.recipe.Recipe;
import xyz.joethei.recipe.RecipeParser;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

Expand All @@ -27,12 +27,18 @@ public class TextParsing {

@Test
public void validParsing() throws IOException, URISyntaxException {

Recipe recipe = RecipeParser.parse(Path.of(Objects.requireNonNull(getClass().getClassLoader().getResource("recipe.json")).toURI()));
assertNotNull(recipe);
assertEquals("Tomatenspaghetti", recipe.name());
assertEquals(Arrays.asList("300 g Spaghetti", "40 g Butter", "400 g passierte Tomaten"), recipe.recipeIngredient());
assertEquals(Optional.empty(), recipe.cookTime());
assertEquals(Optional.of("P0DT0H15M"), recipe.totalTime());
}

@Test
public void parse() throws URISyntaxException, IOException {
var recipe = RecipeParser.parse(Path.of(Objects.requireNonNull(getClass().getClassLoader().getResource("recipe1.json")).toURI()));
assertNotNull(recipe);
assertEquals("Tomatensuppe", recipe.name());
}
}
43 changes: 43 additions & 0 deletions src/test/resources/recipe1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"@context": "http://schema.org",
"@type": "Recipe",
"name": "Tomatensuppe",
"author": {
"@type": "Person",
"name": "Mustermann"
},
"image": "https://example.com/image.png",
"datePublished": "2017-07-10T09:09:10+02:00",
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "3.47902",
"reviewCount": "42"
},
"prepTime": "PT10M",
"totalTime": "PT25M",
"recipeYield": "4 servings",
"nutrition": {
"@type": "NutritionInformation",
"servingSize": "1 bowl"
},
"recipeIngredient": [
"80 g Zwiebel",
"600 g Tomate",
"4 EL Oliven\u00f6l",
"2 EL Tomatenmark",
"20 g Mehl",
"60 ml Fleischbr\u00fche",
"8 Stk. Pfefferkorn",
"1 Lorbeerblatt",
"1 EL Zucker",
"1 EL Zitronensaft",
" etwas Salz",
"60 ml Gem\u00fcsebr\u00fche",
"100 ml Sahne"
],
"recipeInstructions": [
"1. Zwiebeln sch\u00e4len und fein w\u00fcrfelig schneiden. Tomaten waschen, Strunk entfernen und w\u00fcrfelig schneiden.\r\n",
"2. \u00d6l in einem Suppentopf erhitzen. Zwiebeln darin glasig schwitzen. Mehl hinzuf\u00fcgen und kurz mitr\u00f6sten. Tomaten und \u2013Mark unterr\u00fchren. Mit Br\u00fche aufgie\u00dfen und Gew\u00fcrze hinzuf\u00fcge.\r\n",
"3. Etwa 30 Minuten k\u00f6cheln lassen. Suppe durch ein Sieb streichen. Nochmals kurz aufkochen und mit Sahne verfeinern und nochmals aufmixen.\r\n"
]
}

0 comments on commit 84ec4a2

Please sign in to comment.