This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
/
index.ts
176 lines (154 loc) · 5.81 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import axios from 'axios';
import * as domino from 'domino';
// @ts-ignore
import microdata from 'microdata-node';
import { validate } from 'jsonschema';
import { BaseSchema, Data, Options, RootSchema } from './types';
import { isValidHttpUrl, propertyTransformerMap } from './utilities';
import schema from './requiredProps.json';
const defaultOptions = {
maxRedirects: 5, // Maximum number of redirects to follow
timeout: 10000, // Request timeout in miliseconds
};
const consolidateRecipeProperties = (prospectiveProperties: BaseSchema) => {
const {
url,
name,
image,
photo,
thumbnailUrl,
description,
cookTime,
prepTime,
totalTime,
recipeYield,
yield: rYield,
recipeIngredients,
recipeIngredient,
ingredients,
ingredient,
recipeInstructions,
instructions,
step,
recipeCategory,
recipeCuisine,
recipeType,
keywords,
tag,
} = prospectiveProperties;
return {
url,
name,
image: image || photo || thumbnailUrl,
description,
cookTime,
cookTimeOriginalFormat: cookTime,
prepTime,
prepTimeOriginalFormat: prepTime,
totalTime,
totalTimeOriginalFormat: totalTime,
recipeYield: recipeYield || rYield,
recipeIngredients: recipeIngredient || recipeIngredients || ingredients || ingredient,
recipeInstructions: recipeInstructions || instructions || step,
recipeCategories: recipeCategory,
recipeCuisines: recipeCuisine,
recipeTypes: recipeType,
keywords: keywords || tag,
};
};
const convert_json_ld_recipe = (rec: BaseSchema, nonStandard_attrs: boolean = false, url: string | undefined) => {
const consolidatedRecipe = consolidateRecipeProperties(rec);
const transformedRecipe: BaseSchema = {} as BaseSchema;
transformedRecipe['url'] = url as string;
if (nonStandard_attrs) {
transformedRecipe['_format'] = 'json-ld';
} else {
transformedRecipe['_format'] = 'microdata';
}
Object.entries(consolidatedRecipe).forEach(([key, value]) => {
const propertyTransformer = propertyTransformerMap[key];
if (propertyTransformer && value) {
transformedRecipe[key as keyof BaseSchema] = propertyTransformer(value, key);
}
});
return transformedRecipe;
};
const validateRecipeSchema = (rec: RootSchema) => {
const validator = { name: rec.name, recipeIngredients: rec.recipeIngredients };
const response = validate(validator, schema);
if (!response.valid) {
return { status: false, data: undefined, message: 'Recipe not found on page' };
}
return { status: true, data: rec, message: 'success' };
};
const getRecipeData = async (input: string | Partial<Options>, inputOptions: Partial<Options> = {}): Promise<Data> => {
let siteUrl: string, html, recipe: RootSchema | undefined;
if (typeof input === 'object') {
inputOptions = input;
siteUrl = input.url as string;
} else {
siteUrl = input;
}
const options = Object.assign({}, defaultOptions, inputOptions);
if (!isValidHttpUrl(siteUrl as string) && !options.html) {
throw new Error('url must start with http:// or https://');
}
try {
if (!options.html) {
const response = await axios.get(siteUrl as string, {
responseType: 'text',
headers: {
'Accept-Language': options.lang,
},
timeout: options.timeout,
maxRedirects: options.maxRedirects,
});
html = await response.data;
} else {
html = options.html as string;
}
} catch (error) {
throw new Error('Something went wrong');
}
// search for json-ld tags first, then search for micro data if json-ld tags not present
try {
const window = domino.createWindow(html).document;
const jsonLds = Object.values(window.querySelectorAll("script[type='application/ld+json']"));
if (jsonLds.length > 0) {
jsonLds.forEach(json => {
if (json.textContent) {
const data = JSON.parse(json.textContent);
if (data['@graph'] && Array.isArray(data['@graph'])) {
data['@graph'].forEach(g => {
if (g['@type'] === 'Recipe') {
recipe = convert_json_ld_recipe(g, true, siteUrl);
}
});
}
if (data['@type'] === 'Recipe') {
recipe = convert_json_ld_recipe(data, true, siteUrl);
}
if (Array.isArray(data['@type']) && data['@type'].includes('Recipe')) {
recipe = convert_json_ld_recipe(data, true, siteUrl);
}
}
});
} else {
throw new Error('JSON+LD Schema not found. trying to search for microdata.');
}
} catch {
try {
const meta = microdata.toJson(html);
if (!meta || !meta.items || !meta.items[0]) {
return { status: false, data: undefined, message: 'No Microdata tags present on page' };
}
const isRecipe: any = Object.values(meta.items).find((item: any) => item.type[0].indexOf('Recipe') > -1);
recipe = isRecipe ? convert_json_ld_recipe(isRecipe.properties, false, siteUrl) : undefined;
} catch {}
}
if (recipe !== undefined) {
return validateRecipeSchema(recipe);
}
return { status: false, data: undefined, message: 'No JSON+LD or Microdata recipe tags present on page' };
};
export default getRecipeData;