feat: Implement quick import feature for recipes
- Added QuickImportController and QuickImportService to handle recipe imports from URLs and file paths. - Created QuickImportModule to encapsulate the quick import functionality. - Developed frontend ImportFilePage for users to upload files or enter URLs for recipe import. - Integrated API proxy to communicate with the backend for quick import requests. - Implemented WriteRecipePage for users to manually input recipes with Markdown support. - Added page routing for the new import and write recipe functionalities.
This commit is contained in:
@@ -0,0 +1,218 @@
|
||||
import { Injectable, BadRequestException } from '@nestjs/common';
|
||||
|
||||
interface QuickImportResult {
|
||||
markdown: string;
|
||||
source: 'ica' | 'pdf' | 'other';
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class QuickImportService {
|
||||
/**
|
||||
* Detekterar typ av input (URL eller filsökväg) och importerar från lämplig källa
|
||||
*/
|
||||
async importFromInput(input: string): Promise<QuickImportResult> {
|
||||
input = input.trim();
|
||||
|
||||
if (!input) {
|
||||
throw new BadRequestException('Du måste ange en URL eller filsökväg');
|
||||
}
|
||||
|
||||
// Detektera typ
|
||||
const isUrl = this.isUrl(input);
|
||||
const isPdf = this.isPdfPath(input);
|
||||
|
||||
if (isUrl) {
|
||||
// Försök detektera webbplats
|
||||
if (input.includes('ica.se')) {
|
||||
return this.scrapeIcaRecipe(input);
|
||||
} else {
|
||||
throw new BadRequestException(
|
||||
'Endast ICA-recept stöds för närvarande. Försök med en ICA-länk (ica.se)'
|
||||
);
|
||||
}
|
||||
} else if (isPdf) {
|
||||
throw new BadRequestException(
|
||||
'PDF-import är under utveckling. Använd snabbimport för ICA-recept eller skriv in receptet manuellt.'
|
||||
);
|
||||
} else {
|
||||
throw new BadRequestException(
|
||||
'Ogültig input. Ange en gyltig URL (t.ex. ica.se/recept/...) eller filsökväg'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Kontrollerar om input är en URL
|
||||
*/
|
||||
private isUrl(input: string): boolean {
|
||||
try {
|
||||
new URL(input);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Kontrollerar om input är en PDF-filsökväg
|
||||
*/
|
||||
private isPdfPath(input: string): boolean {
|
||||
const normalized = input.toLowerCase();
|
||||
return normalized.endsWith('.pdf');
|
||||
}
|
||||
|
||||
/**
|
||||
* Skrapar recept från ICA.se
|
||||
*
|
||||
* Försöker hämta:
|
||||
* - Recepttitel (från h1 eller meta title)
|
||||
* - Ingredienser (från ingrediens-lista)
|
||||
* - Instruktioner (från steg-lista eller beskrivning)
|
||||
*
|
||||
* @param url ICA-receptlänk
|
||||
* @returns Markdown-format
|
||||
*/
|
||||
private async scrapeIcaRecipe(url: string): Promise<QuickImportResult> {
|
||||
try {
|
||||
// Hämta HTML från URL
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
'User-Agent':
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const html = await response.text();
|
||||
|
||||
// Extrahera receptinformation från HTML
|
||||
const recipe = this.parseIcaHtml(html);
|
||||
|
||||
if (!recipe.name) {
|
||||
throw new Error('Kunde inte hitta receptnamn på sidan. Försök med en annan länk.');
|
||||
}
|
||||
|
||||
// Konvertera till Markdown-format
|
||||
const markdown = this.recipeToMarkdown(recipe);
|
||||
|
||||
return {
|
||||
markdown,
|
||||
source: 'ica',
|
||||
};
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Okänt fel vid scraping';
|
||||
throw new BadRequestException(
|
||||
`Kunde inte hämta recept från ICA: ${message}. Kontrollera att länken är korrekt och försök igen.`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parsa ICA-receptsida (HTML)
|
||||
*
|
||||
* Denna är en simplified version. För full produrktion behöver du:
|
||||
* - Headless browser (Puppeteer/Playwright)
|
||||
* - API-integration eller scraping-bibliotek
|
||||
* - Proper error handling för sidstruktur-ändringar
|
||||
*/
|
||||
private parseIcaHtml(html: string): {
|
||||
name: string;
|
||||
description?: string;
|
||||
ingredients: Array<{
|
||||
quantity: number;
|
||||
unit: string;
|
||||
name: string;
|
||||
}>;
|
||||
instructions?: string;
|
||||
} {
|
||||
// Extrahera titel
|
||||
let name = '';
|
||||
const titleMatch = html.match(/<h1[^>]*>([^<]+)<\/h1>/i);
|
||||
if (titleMatch) {
|
||||
name = titleMatch[1].trim();
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
const ogTitleMatch = html.match(/<meta\s+property="og:title"\s+content="([^"]+)"/i);
|
||||
if (ogTitleMatch) {
|
||||
name = ogTitleMatch[1].trim();
|
||||
}
|
||||
}
|
||||
|
||||
// Extrahera ingredienser (en enkel regex - kan behöva anpassas)
|
||||
const ingredients: Array<{ quantity: number; unit: string; name: string }> = [];
|
||||
const ingredientRegex = /(?:ingredients?|<li[^>]*>)([^<]*?(\d+(?:[.,]\d+)?)\s*([a-zåäö]*)\s*([^<]+))/gi;
|
||||
let match;
|
||||
while ((match = ingredientRegex.exec(html)) !== null) {
|
||||
const quantity = parseFloat(match[2].replace(',', '.'));
|
||||
const unit = match[3].toLowerCase().trim() || 'st';
|
||||
const name = match[4].trim();
|
||||
if (name) {
|
||||
ingredients.push({ quantity, unit, name });
|
||||
}
|
||||
}
|
||||
|
||||
// Extrahera instruktioner (första paragraf eller instruktions-sektion)
|
||||
let instructions = '';
|
||||
const instructionsMatch = html.match(
|
||||
/<(?:div|section)[^>]*class="[^"]*instruction[^"]*"[^>]*>([^<]*)<\/(?:div|section)>/is
|
||||
);
|
||||
if (instructionsMatch) {
|
||||
instructions = instructionsMatch[1].replace(/<[^>]+>/g, '').trim();
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
ingredients: ingredients.length > 0 ? ingredients : [],
|
||||
instructions,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Konvertera receptobjekt till Markdown-format
|
||||
*/
|
||||
private recipeToMarkdown(recipe: {
|
||||
name: string;
|
||||
description?: string;
|
||||
ingredients: Array<{
|
||||
quantity: number;
|
||||
unit: string;
|
||||
name: string;
|
||||
}>;
|
||||
instructions?: string;
|
||||
}): string {
|
||||
const lines: string[] = [];
|
||||
|
||||
// Titel
|
||||
lines.push(`# ${recipe.name}`);
|
||||
lines.push('');
|
||||
|
||||
// Beskrivning
|
||||
if (recipe.description) {
|
||||
lines.push(recipe.description);
|
||||
lines.push('');
|
||||
}
|
||||
|
||||
// Ingredienser
|
||||
if (recipe.ingredients.length > 0) {
|
||||
lines.push('## Ingredienser');
|
||||
for (const ing of recipe.ingredients) {
|
||||
const quantity = ing.quantity > 0 ? `${ing.quantity} ` : '';
|
||||
const unit = ing.unit ? `${ing.unit} ` : '';
|
||||
lines.push(`- ${quantity}${unit}${ing.name}`);
|
||||
}
|
||||
lines.push('');
|
||||
}
|
||||
|
||||
// Instruktioner
|
||||
if (recipe.instructions) {
|
||||
lines.push('## Tillvägagångssätt');
|
||||
lines.push(recipe.instructions);
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user