-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from mst-mkt/feat/get-recipes-from-web_#6
Feat/get recipes from web #6
- Loading branch information
Showing
12 changed files
with
247 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { load } from 'cheerio' | ||
import type { ingredients } from './schemas/ingredientsSchema' | ||
import { type Recipes, recipeSchema } from './schemas/recipesSchema' | ||
import { createSearchRecipesUrl } from './utils/createSearchUrl' | ||
|
||
const fetchRecipes = async (url: string): Promise<Recipes> => { | ||
const res = await fetch(url) | ||
if (!res.ok) throw new Error(`Failed to fetch the recipes: ${res.statusText}`) | ||
const data = await res.text() | ||
|
||
const $ = load(data) | ||
const recipes = $('.recipe-list .recipe-preview') | ||
.map((index, element) => { | ||
const recipeImage = $(element).find('.recipe-image img').attr('src') | ||
const recipeTitle = $(element).find('.recipe-title').text().trim() | ||
const ingredients = $(element) | ||
.find('.ingredients') | ||
.text() | ||
.trim() | ||
.split('、') | ||
.map((ingredient) => ingredient.trim()) | ||
.filter((ingredient) => !ingredient.includes('\n...')) | ||
const newRecipe = { recipeImage, recipeTitle, ingredients } | ||
|
||
const validatedRecipe = recipeSchema.safeParse(newRecipe) | ||
if (!validatedRecipe.success) { | ||
console.error(`Invalid recipe at index ${index}:`, validatedRecipe.error) | ||
return null | ||
} | ||
return validatedRecipe.data | ||
}) | ||
.get() | ||
.filter((recipe) => recipe !== null) | ||
|
||
return recipes | ||
} | ||
|
||
const getRecipesByIngredients = async (ingredients: ingredients): Promise<Recipes> => { | ||
const searchUrl = createSearchRecipesUrl(ingredients) | ||
return await fetchRecipes(searchUrl) | ||
} | ||
|
||
export { getRecipesByIngredients } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
import { zValidator } from '@hono/zod-validator' | ||
import { honoFactory } from '../factory' | ||
import { recipesReqSchema } from '../schemas/recipesSchema' | ||
import { getRecipesByIngredients } from '../getRecipes' | ||
import { ingredientsSchema } from '../schemas/ingredientsSchema' | ||
|
||
const recipesRouter = honoFactory | ||
.createApp() | ||
.get('/', zValidator('query', recipesReqSchema), async (c) => { | ||
.get('/', zValidator('query', ingredientsSchema), async (c) => { | ||
const { ingredients } = c.req.valid('query') | ||
|
||
return c.json({ message: `hello ${ingredients.join(', ')}` }) | ||
try { | ||
const recipes = await getRecipesByIngredients({ ingredients }) | ||
return c.json(recipes) | ||
} catch (error) { | ||
return c.json({ error }, 500) | ||
} | ||
}) | ||
|
||
export { recipesRouter } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { z } from 'zod' | ||
const ingredientsSchema = z.object({ | ||
ingredients: z.array(z.string()).describe('食材のリスト'), | ||
}) | ||
|
||
type ingredients = z.infer<typeof ingredientsSchema> | ||
|
||
export { ingredientsSchema, type ingredients } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { z } from 'zod' | ||
import { ingredientsSchema } from './ingredientsSchema' | ||
|
||
const recipesReqSchema = z | ||
.object({ | ||
ingredients: z.array(z.string()), | ||
}) | ||
.strict() | ||
const recipeSchema = ingredientsSchema.extend({ | ||
recipeImage: z.string().url().describe('レシピの画像のURL'), | ||
recipeTitle: z.string().describe('レシピのタイトル'), | ||
}) | ||
const recipesSchema = z.array(recipeSchema) | ||
|
||
export { recipesReqSchema } | ||
type Recipes = z.infer<typeof recipesSchema> | ||
|
||
export { recipeSchema, recipesSchema, type Recipes } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { ingredients } from '../schemas/ingredientsSchema' | ||
|
||
const createSearchRecipesUrl = (ingredients: ingredients): string => { | ||
const baseSearchUrl = 'https://cookpad.com/search/' | ||
const searchParams = ingredients.ingredients | ||
.map((ingredients) => encodeURIComponent(ingredients)) | ||
.join('%20') | ||
return `${baseSearchUrl}${searchParams}` | ||
} | ||
|
||
export { createSearchRecipesUrl } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.