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

feat: Add serving size scraping and customisation #488

Merged
merged 2 commits into from
Apr 24, 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
6 changes: 6 additions & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"cmdk": "^1.0.0",
"drizzle-orm": "^0.30.6",
"drizzle-orm": "^0.30.9",
"jsdom": "^23.0.1",
"lucide-react": "^0.368.0",
"next": "^14.0.3",
Expand Down Expand Up @@ -75,7 +75,7 @@
"@typescript-eslint/parser": "^7.4.0",
"autoprefixer": "^10.4.14",
"dotenv-cli": "^7.3.0",
"drizzle-kit": "^0.20.14",
"drizzle-kit": "^0.20.17",
"eslint": "^8.57.0",
"eslint-config-next": "13.0.0",
"eslint-config-prettier": "^8.8.0",
Expand Down
51 changes: 34 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions src/app/(dashboard)/recipes/[id]/recipeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import { Controller, FormProvider, useForm } from "react-hook-form"
import { toast } from "sonner"

import { Button } from "~/components/ui/button"
import {
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "~/components/ui/form"
import { Input } from "~/components/ui/input"
import { IngredientTable } from "~/components/ingredient-table"
import { RecipeTitleInput } from "~/components/recipe-title-input"

Expand Down Expand Up @@ -52,9 +60,10 @@ function RecipeFormInner({
}
}),
recipeName: recipe.name,
method: recipe.steps || undefined,
imageUrl: recipe.imageUrl || undefined,
method: recipe.steps ?? undefined,
imageUrl: recipe.imageUrl ?? undefined,
recipeBuddyRecipeId: recipe.id,
servings: recipe.servings ?? undefined,
},
})

Expand All @@ -81,6 +90,19 @@ function RecipeFormInner({
name="recipeName"
control={form.control}
/>
<FormField
render={({ field }) => (
<FormItem className="flex items-center gap-2">
<FormLabel>Servings</FormLabel>
<FormControl>
<Input className="max-w-[70px]" type="number" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
name="servings"
control={form.control}
/>
<Link
href={recipe.url}
className="text-muted-foreground pl-1 text-lg"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const createRecipeInGrocyProcedure = protectedProcedure
name: input.recipeName,
description: input.method,
picture_file_name: imageFilename,
base_servings: input.servings,
}

const recipeResponse = await grocyFetch("/objects/recipes", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,27 @@ const IngredientSchema = z.union([
UnignoredIngredientSchema,
IgnoredIngredientSchema,
])

const numberLike = z.union([z.number(), z.string()])
const numberLikeToNumberAtLeastOne = numberLike
.pipe(
z.coerce
.number()
.int("Must be a whole number")
.min(0, "Must be a positive number")
)
.transform((a) => {
if (a < 1) return 1
return a
})

export const CreateRecipeInGrocyCommandSchema = z.object({
recipeBuddyRecipeId: z.number(),
recipeName: z.string().trim().min(1),
ingredients: IngredientSchema.array(),
method: z.string().optional(),
imageUrl: z.string().url().optional(),
servings: numberLikeToNumberAtLeastOne.optional(),
})

export type CreateRecipeInGrocyCommand = z.infer<
Expand Down
20 changes: 19 additions & 1 deletion src/server/api/modules/recipes/service/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,23 @@ export const RecipeImageUrlSchema = z
})

export const JsonLdRecipeSchema = z.object({
"@type": z.string(),
"@type": z.union([z.string(), z.tuple([z.string()]).transform((a) => a[0])]),
})

export const ExtractNumberSchema = z.coerce.string().transform((val, ctx) => {
const numberRegex = /\d+/g

const regexResult = numberRegex.exec(val)

if (!regexResult) {
ctx.addIssue({
message: "No numbers found in servings",
code: "custom",
})
return z.NEVER
}

const [first] = regexResult

return parseInt(first)
})
5 changes: 5 additions & 0 deletions src/server/api/modules/recipes/service/scraper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TRPCError } from "@trpc/server"
import {
ExtractNumberSchema,
JsonLdRecipeSchema,
RecipeImageUrlSchema,
RecipeStepSchema,
Expand Down Expand Up @@ -62,6 +63,7 @@ function getSchemaRecipeFromNodeList(nodeList: NodeList) {
}

if (Array.isArray(parsedNodeContent)) {
console.log("its an array")
for (const metadataObject of parsedNodeContent) {
if (jsonObjectIsRecipe(metadataObject)) {
return metadataObject
Expand Down Expand Up @@ -106,11 +108,14 @@ export async function hydrateRecipe(url: string) {
(a) => ({ scrapedName: a })
)

const servings = ExtractNumberSchema.safeParse(recipeData.recipeYield)

const recipe: InsertRecipe = {
name: recipeData.name,
url,
steps: steps.data.join("\n"),
imageUrl: image.success ? image.data : undefined,
servings: servings.success ? servings.data : undefined,
}

return { recipe, ingredients: ings }
Expand Down
1 change: 1 addition & 0 deletions src/server/db/drizzle/0001_worthless_aqueduct.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `recipe-buddy_recipe` ADD `servings` integer;
Loading
Loading