From ce38b2be0aec28de42aeba56520cd0b7cb42fa8c Mon Sep 17 00:00:00 2001 From: Max Richter Date: Sun, 19 Jan 2025 19:49:24 +0100 Subject: [PATCH] feat: add proxy server --- lib/env.ts | 4 ++++ lib/openai.ts | 2 +- lib/playwright.ts | 14 ++++++++++++-- lib/recipeSchema.ts | 10 ++++++++++ routes/api/recipes/create/index.ts | 7 +++++-- routes/recipes/[name].tsx | 6 +----- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/env.ts b/lib/env.ts index 7379948..df897dd 100644 --- a/lib/env.ts +++ b/lib/env.ts @@ -1,5 +1,9 @@ import path from "node:path"; +export const PROXY_SERVER = Deno.env.get("PROXY_SERVER"); +export const PROXY_USERNAME = Deno.env.get("PROXY_USERNAME"); +export const PROXY_PASSWORD = Deno.env.get("PROXY_PASSWORD"); + export const SILVERBULLET_SERVER = Deno.env.get("SILVERBULLET_SERVER"); export const TMDB_API_KEY = Deno.env.get("TMDB_API_KEY"); export const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY"); diff --git a/lib/openai.ts b/lib/openai.ts index b845ec6..c11c742 100644 --- a/lib/openai.ts +++ b/lib/openai.ts @@ -226,5 +226,5 @@ export async function extractRecipe(content: string) { response_format: zodResponseFormat(recipeResponseSchema, "recipe-v2"), }); - return recipeSchema.parse(completion.choices[0].message.parsed); + return recipeResponseSchema.parse(completion.choices[0].message.parsed); } diff --git a/lib/playwright.ts b/lib/playwright.ts index e39651a..9221039 100644 --- a/lib/playwright.ts +++ b/lib/playwright.ts @@ -1,6 +1,7 @@ import { firefox } from "npm:playwright-extra"; import { createStreamResponse } from "@lib/helpers.ts"; import StealthPlugin from "npm:puppeteer-extra-plugin-stealth"; +import * as env from "@lib/env.ts"; const userAgentStrings = [ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.2227.0 Safari/537.36", @@ -16,9 +17,18 @@ export async function fetchHtmlWithPlaywright( streamResponse: ReturnType, ): Promise { streamResponse.enqueue("booting up playwright"); - + + const config: Parameters[0] = {}; + if (env.PROXY_SERVER) { + config.proxy = { + server: env.PROXY_SERVER, + username: env.PROXY_USERNAME, + password: env.PROXY_PASSWORD, + }; + } + // Launch the Playwright browser - const browser = await firefox.launch(); + const browser = await firefox.launch(config); streamResponse.enqueue("fetching html"); diff --git a/lib/recipeSchema.ts b/lib/recipeSchema.ts index 6f195c7..352048c 100644 --- a/lib/recipeSchema.ts +++ b/lib/recipeSchema.ts @@ -44,4 +44,14 @@ const noRecipeSchema = z.object({ export const recipeResponseSchema = z.union([recipeSchema, noRecipeSchema]); +export function isValidRecipe( + recipe: + | { ingredients?: unknown[]; instructions?: string[]; name?: string } + | null + | undefined, +) { + return recipe?.ingredients?.length && recipe?.instructions?.length && + recipe.name?.length; +} + export default recipeSchema; diff --git a/routes/api/recipes/create/index.ts b/routes/api/recipes/create/index.ts index 4a3e646..22028ad 100644 --- a/routes/api/recipes/create/index.ts +++ b/routes/api/recipes/create/index.ts @@ -7,7 +7,7 @@ import * as openai from "@lib/openai.ts"; import tds from "https://cdn.skypack.dev/turndown@7.2.0"; import { createLogger } from "@lib/log/index.ts"; import { createRecipe, Recipe } from "@lib/resource/recipes.ts"; -import recipeSchema from "@lib/recipeSchema.ts"; +import recipeSchema, { isValidRecipe } from "@lib/recipeSchema.ts"; import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts"; import { safeFileName } from "@lib/string.ts"; import { createDocument } from "@lib/documents.ts"; @@ -93,7 +93,10 @@ async function extractUsingAI( const recipe = await openai.extractRecipe(markdown); - return recipe; + if (isValidRecipe(recipe)) { + return recipe; + } + return; } async function processCreateRecipeFromUrl( diff --git a/routes/recipes/[name].tsx b/routes/recipes/[name].tsx index 5ecfad3..ad31b8b 100644 --- a/routes/recipes/[name].tsx +++ b/routes/recipes/[name].tsx @@ -9,6 +9,7 @@ import { KMenu } from "@islands/KMenu.tsx"; import PageHero from "@components/PageHero.tsx"; import { Star } from "@components/Stars.tsx"; import { renderMarkdown } from "@lib/documents.ts"; +import { isValidRecipe } from "@lib/recipeSchema.ts"; export const handler: Handlers<{ recipe: Recipe; session: unknown } | null> = { async GET(_, ctx) { @@ -24,11 +25,6 @@ export const handler: Handlers<{ recipe: Recipe; session: unknown } | null> = { }, }; -function isValidRecipe(recipe: Recipe | null) { - return recipe?.ingredients?.length && recipe?.instructions?.length && - recipe.name?.length; -} - function ValidRecipe({ recipe, amount,