feat: add proxy server

This commit is contained in:
max_richter 2025-01-19 19:49:24 +01:00
parent 78e94ccf82
commit ce38b2be0a
6 changed files with 33 additions and 10 deletions

View File

@ -1,5 +1,9 @@
import path from "node:path"; 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 SILVERBULLET_SERVER = Deno.env.get("SILVERBULLET_SERVER");
export const TMDB_API_KEY = Deno.env.get("TMDB_API_KEY"); export const TMDB_API_KEY = Deno.env.get("TMDB_API_KEY");
export const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY"); export const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY");

View File

@ -226,5 +226,5 @@ export async function extractRecipe(content: string) {
response_format: zodResponseFormat(recipeResponseSchema, "recipe-v2"), response_format: zodResponseFormat(recipeResponseSchema, "recipe-v2"),
}); });
return recipeSchema.parse(completion.choices[0].message.parsed); return recipeResponseSchema.parse(completion.choices[0].message.parsed);
} }

View File

@ -1,6 +1,7 @@
import { firefox } from "npm:playwright-extra"; import { firefox } from "npm:playwright-extra";
import { createStreamResponse } from "@lib/helpers.ts"; import { createStreamResponse } from "@lib/helpers.ts";
import StealthPlugin from "npm:puppeteer-extra-plugin-stealth"; import StealthPlugin from "npm:puppeteer-extra-plugin-stealth";
import * as env from "@lib/env.ts";
const userAgentStrings = [ 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", "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<typeof createStreamResponse>, streamResponse: ReturnType<typeof createStreamResponse>,
): Promise<string> { ): Promise<string> {
streamResponse.enqueue("booting up playwright"); streamResponse.enqueue("booting up playwright");
const config: Parameters<typeof firefox.launch>[0] = {};
if (env.PROXY_SERVER) {
config.proxy = {
server: env.PROXY_SERVER,
username: env.PROXY_USERNAME,
password: env.PROXY_PASSWORD,
};
}
// Launch the Playwright browser // Launch the Playwright browser
const browser = await firefox.launch(); const browser = await firefox.launch(config);
streamResponse.enqueue("fetching html"); streamResponse.enqueue("fetching html");

View File

@ -44,4 +44,14 @@ const noRecipeSchema = z.object({
export const recipeResponseSchema = z.union([recipeSchema, noRecipeSchema]); 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; export default recipeSchema;

View File

@ -7,7 +7,7 @@ import * as openai from "@lib/openai.ts";
import tds from "https://cdn.skypack.dev/turndown@7.2.0"; import tds from "https://cdn.skypack.dev/turndown@7.2.0";
import { createLogger } from "@lib/log/index.ts"; import { createLogger } from "@lib/log/index.ts";
import { createRecipe, Recipe } from "@lib/resource/recipes.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 { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
import { safeFileName } from "@lib/string.ts"; import { safeFileName } from "@lib/string.ts";
import { createDocument } from "@lib/documents.ts"; import { createDocument } from "@lib/documents.ts";
@ -93,7 +93,10 @@ async function extractUsingAI(
const recipe = await openai.extractRecipe(markdown); const recipe = await openai.extractRecipe(markdown);
return recipe; if (isValidRecipe(recipe)) {
return recipe;
}
return;
} }
async function processCreateRecipeFromUrl( async function processCreateRecipeFromUrl(

View File

@ -9,6 +9,7 @@ import { KMenu } from "@islands/KMenu.tsx";
import PageHero from "@components/PageHero.tsx"; import PageHero from "@components/PageHero.tsx";
import { Star } from "@components/Stars.tsx"; import { Star } from "@components/Stars.tsx";
import { renderMarkdown } from "@lib/documents.ts"; import { renderMarkdown } from "@lib/documents.ts";
import { isValidRecipe } from "@lib/recipeSchema.ts";
export const handler: Handlers<{ recipe: Recipe; session: unknown } | null> = { export const handler: Handlers<{ recipe: Recipe; session: unknown } | null> = {
async GET(_, ctx) { 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({ function ValidRecipe({
recipe, recipe,
amount, amount,