-
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.
- Loading branch information
Showing
21 changed files
with
928 additions
and
722 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
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,12 +1,62 @@ | ||
export interface HubRecipeData { | ||
id: string; | ||
title: string; | ||
prelude: any; | ||
prelude: { | ||
type: 'doc'; | ||
content: ParagraphNode[]; | ||
}; | ||
mainImageUrl: string; | ||
ingredients: any[]; | ||
instructions: any[]; | ||
ingredients: GnocchiIngredient[]; | ||
// tiptap/prosemirror content | ||
instructions: { | ||
type: 'doc'; | ||
content: (StepNode | SectionTitleNode)[]; | ||
}; | ||
publisher: { | ||
fullName: string; | ||
}; | ||
note?: string; | ||
servings?: number; | ||
prepTimeMinutes?: number; | ||
cookTimeMinutes?: number; | ||
totalTimeMinutes?: number; | ||
} | ||
|
||
type GnocchiIngredient = { | ||
text: string; | ||
comments: string[]; | ||
quantity: number; | ||
unit?: string; | ||
isSectionHeader: boolean; | ||
food: string; | ||
id: string; | ||
note: string | null; | ||
}; | ||
|
||
type StepNode = { | ||
type: 'step'; | ||
content: TextNode[]; | ||
attrs: { | ||
id: string; | ||
note?: string; | ||
}; | ||
}; | ||
|
||
type SectionTitleNode = { | ||
type: 'sectionTitle'; | ||
content: TextNode[]; | ||
attrs: { | ||
id: string; | ||
note?: string; | ||
}; | ||
}; | ||
|
||
type TextNode = { | ||
type: 'text'; | ||
text: string; | ||
}; | ||
|
||
type ParagraphNode = { | ||
type: 'paragraph'; | ||
content: TextNode[]; | ||
}; |
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,40 +1,42 @@ | ||
import { Cheerio, CheerioAPI } from 'cheerio'; | ||
|
||
type UnwrapPromise<T extends Promise<any>> = T extends Promise<infer U> | ||
? U | ||
: never; | ||
type UnwrapPromise<T extends Promise<any>> = | ||
T extends Promise<infer U> ? U : never; | ||
|
||
import * as extractors from './extractors/index.js'; | ||
import { ExtractorData } from './extractors/types.js'; | ||
|
||
type Extractor = ($: CheerioAPI) => Promise<ExtractorData | null>; | ||
|
||
const extractorOrdering: [RegExp, Extractor][] = [ | ||
[/.*/, extractors.microdata], | ||
[/.*/, extractors.schemaOrg], | ||
[/.*/, extractors.wprm], | ||
[/.*/, extractors.tasty], | ||
[/.*/, extractors.naive], | ||
[/gnocchi\.biscuits\.club/, extractors.gnocchi], | ||
[/localhost:6124/, extractors.gnocchi], | ||
[/.*/, extractors.microdata], | ||
[/.*/, extractors.schemaOrg], | ||
[/.*/, extractors.wprm], | ||
[/.*/, extractors.tasty], | ||
[/.*/, extractors.naive], | ||
]; | ||
|
||
async function tryParse($: CheerioAPI, pageUrl: string) { | ||
for (const [filter, extractor] of extractorOrdering) { | ||
if (!filter.test(pageUrl)) { | ||
continue; | ||
} | ||
const result = await extractor($); | ||
if (result) { | ||
return result; | ||
} | ||
} | ||
for (const [filter, extractor] of extractorOrdering) { | ||
if (!filter.test(pageUrl)) { | ||
continue; | ||
} | ||
const result = await extractor($); | ||
if (result) { | ||
return result; | ||
} | ||
} | ||
} | ||
|
||
export async function extract($: CheerioAPI, pageUrl: string) { | ||
const result = await tryParse($, pageUrl); | ||
return { | ||
...result, | ||
url: result?.url || pageUrl, | ||
}; | ||
const result = await tryParse($, pageUrl); | ||
return { | ||
scanner: 'none', | ||
...result, | ||
url: result?.url || pageUrl, | ||
}; | ||
} | ||
|
||
export type ScanResult = UnwrapPromise<ReturnType<typeof extract>>; |
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,49 @@ | ||
import { CheerioAPI } from 'cheerio'; | ||
import { ExtractorData } from './types.js'; | ||
|
||
const SNAPSHOT_MATCH = /window\.__SNAPSHOT__\s+=\s+(.*);/; | ||
|
||
export async function gnocchi($: CheerioAPI): Promise<ExtractorData | null> { | ||
const scripts = $('script'); | ||
|
||
let s = scripts.filter((i, el) => { | ||
const text = $(el).text(); | ||
return SNAPSHOT_MATCH.test(text); | ||
}); | ||
|
||
if (!s.length) { | ||
return null; | ||
} | ||
|
||
const snapshot = SNAPSHOT_MATCH.exec($(s).text())?.[1]; | ||
if (!snapshot) { | ||
return null; | ||
} | ||
|
||
const data = JSON.parse(snapshot); | ||
|
||
return { | ||
scanner: 'gnocchi', | ||
title: data.title, | ||
image: data.mainImageUrl, | ||
author: data.publisher.fullName, | ||
detailedIngredients: data.ingredients.map((i: any) => ({ | ||
original: i.text, | ||
quantity: i.quantity, | ||
unit: i.unit, | ||
foodName: i.food, | ||
note: i.note, | ||
comments: i.comments, | ||
})), | ||
detailedSteps: data.instructions.content.map((i: any) => ({ | ||
type: i.type === 'sectionTitle' ? 'sectionTitle' : 'step', | ||
content: i.content.reduce((acc: string, j: any) => acc + j.text, ''), | ||
note: i.attrs.note, | ||
})), | ||
servings: data.servings, | ||
prepTimeMinutes: data.prepTimeMinutes, | ||
cookTimeMinutes: data.cookTimeMinutes, | ||
totalTimeMinutes: data.totalTimeMinutes, | ||
note: data.note, | ||
}; | ||
} |
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,68 +1,71 @@ | ||
import { Cheerio, CheerioAPI, Element } from 'cheerio'; | ||
import { | ||
detailedInstructionsToSimple, | ||
extractNumber, | ||
isoToMinutes, | ||
parseInstructionInternalText, | ||
detailedInstructionsToSimple, | ||
extractNumber, | ||
isoToMinutes, | ||
parseInstructionInternalText, | ||
} from './utils.js'; | ||
import { ExtractorData } from './types.js'; | ||
|
||
export async function microdata($: CheerioAPI): Promise<ExtractorData | null> { | ||
let elems = $('[itemscope][itemtype="http://schema.org/Recipe"]'); | ||
if (elems.length === 0) { | ||
elems = $('[itemscope][itemtype="https://schema.org/Recipe"]'); | ||
} | ||
if (elems.length === 0) { | ||
return null; | ||
} | ||
let elems = $('[itemscope][itemtype="http://schema.org/Recipe"]'); | ||
if (elems.length === 0) { | ||
elems = $('[itemscope][itemtype="https://schema.org/Recipe"]'); | ||
} | ||
if (elems.length === 0) { | ||
return null; | ||
} | ||
|
||
const first = $(elems.get(0)!); | ||
const name = first.find(' > [itemprop="name"]').text().trim(); | ||
const author = first.find('[itemprop="author"]').text().trim(); | ||
const copyrightHolder = first | ||
.find('[itemprop="copyrightHolder"]') | ||
.text() | ||
.trim(); | ||
const copyrightYear = first.find('[itemprop="copyrightYear"]').text().trim(); | ||
const description = first.find('[itemprop="description"]').text().trim(); | ||
const image = first.find('[itemprop="image"]').attr('src'); | ||
const datePublished = first.find('[itemprop="datePublished"]').text().trim(); | ||
const cookTime = first.find('[itemprop="cookTime"]').text().trim(); | ||
const prepTime = first.find('[itemprop="prepTime"]').text().trim(); | ||
const totalTime = first.find('[itemprop="totalTime"]').text().trim(); | ||
const cookingMethod = first.find('[itemprop="cookingMethod"]').text().trim(); | ||
const recipeCategory = first | ||
.find('[itemprop="recipeCategory"]') | ||
.text() | ||
.trim(); | ||
const recipeCuisine = first.find('[itemprop="recipeCuisine"]').text().trim(); | ||
const recipeYield = first.find('[itemprop="recipeYield"]').text().trim(); | ||
const recipeIngredient = first | ||
.find('[itemprop="recipeIngredient"]') | ||
.map((i, e) => $(e).text().trim()) | ||
.get(); | ||
const recipeInstructionElements = first | ||
.find('[itemprop="recipeInstructions"]') | ||
.get(); | ||
const recipeInstructionsDetailed = recipeInstructionElements | ||
.map((e) => { | ||
return parseInstructionInternalText($(e)); | ||
}) | ||
.flat(); | ||
const first = $(elems.get(0)!); | ||
const name = first.find(' > [itemprop="name"]').text().trim(); | ||
const author = first.find('[itemprop="author"]').text().trim(); | ||
const copyrightHolder = first | ||
.find('[itemprop="copyrightHolder"]') | ||
.text() | ||
.trim(); | ||
const copyrightYear = first.find('[itemprop="copyrightYear"]').text().trim(); | ||
const description = first.find('[itemprop="description"]').text().trim(); | ||
const image = first.find('[itemprop="image"]').attr('src'); | ||
const datePublished = first.find('[itemprop="datePublished"]').text().trim(); | ||
const cookTime = first.find('[itemprop="cookTime"]').text().trim(); | ||
const prepTime = first.find('[itemprop="prepTime"]').text().trim(); | ||
const totalTime = first.find('[itemprop="totalTime"]').text().trim(); | ||
const cookingMethod = first.find('[itemprop="cookingMethod"]').text().trim(); | ||
const recipeCategory = first | ||
.find('[itemprop="recipeCategory"]') | ||
.text() | ||
.trim(); | ||
const recipeCuisine = first.find('[itemprop="recipeCuisine"]').text().trim(); | ||
const recipeYield = first.find('[itemprop="recipeYield"]').text().trim(); | ||
const recipeIngredient = first | ||
.find('[itemprop="recipeIngredient"]') | ||
.map((i, e) => $(e).text().trim()) | ||
.get(); | ||
const recipeInstructionElements = first | ||
.find('[itemprop="recipeInstructions"]') | ||
.get(); | ||
const recipeInstructionsDetailed = recipeInstructionElements | ||
.map((e) => { | ||
return parseInstructionInternalText($(e)); | ||
}) | ||
.flat(); | ||
const note = first.find('[itemprop="note"]').text().trim(); | ||
|
||
return { | ||
title: name, | ||
description, | ||
image, | ||
copyrightHolder, | ||
copyrightYear, | ||
author, | ||
cookTimeMinutes: isoToMinutes(cookTime), | ||
prepTimeMinutes: isoToMinutes(prepTime), | ||
totalTimeMinutes: isoToMinutes(totalTime), | ||
rawIngredients: recipeIngredient, | ||
steps: detailedInstructionsToSimple(recipeInstructionsDetailed), | ||
detailedSteps: recipeInstructionsDetailed, | ||
servings: extractNumber(recipeYield), | ||
}; | ||
return { | ||
scanner: 'microdata', | ||
title: name, | ||
description, | ||
image, | ||
copyrightHolder, | ||
copyrightYear, | ||
author, | ||
cookTimeMinutes: isoToMinutes(cookTime), | ||
prepTimeMinutes: isoToMinutes(prepTime), | ||
totalTimeMinutes: isoToMinutes(totalTime), | ||
rawIngredients: recipeIngredient, | ||
steps: detailedInstructionsToSimple(recipeInstructionsDetailed), | ||
detailedSteps: recipeInstructionsDetailed, | ||
servings: extractNumber(recipeYield), | ||
note, | ||
}; | ||
} |
Oops, something went wrong.