-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.js
More file actions
72 lines (61 loc) · 2.84 KB
/
scraper.js
File metadata and controls
72 lines (61 loc) · 2.84 KB
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
import axios from 'axios'
import {parse} from 'node-html-parser'
import {v4 as uuidv4} from 'uuid'
let dom = ''
let recipeObj
export default function findRecipe(setRecipe, course, setLoading){
if(setLoading) setLoading(true)
const pageNum = Math.floor(Math.random()*500)
let recipeIndex
switch(course){
case 'All':
recipeIndex = `https://www.foodnetwork.com/search/p/${pageNum}/CUSTOM_FACET:RECIPE_FACET/rating`
break;
case 'Entrees':
recipeIndex = `https://www.foodnetwork.com/search/p/${pageNum}/7D/CUSTOM_FACET:RECIPE_FACET/COURSE_DFACET:0/tag%23meal-part:main-dish/rating`
break;
case 'Desserts':
recipeIndex = `https://www.foodnetwork.com/search/p/${pageNum}/7D/CUSTOM_FACET:RECIPE_FACET/COURSE_DFACET:0/tag%23meal-part:dessert/rating`
break;
case 'Sides':
recipeIndex = `https://www.foodnetwork.com/search/p/${pageNum}/7D/CUSTOM_FACET:RECIPE_FACET/COURSE_DFACET:0/tag%23meal-part:side-dish/rating`
break;
case 'Appetizers':
recipeIndex = `https://www.foodnetwork.com/search/p/${pageNum}/7D/CUSTOM_FACET:RECIPE_FACET/COURSE_DFACET:0/tag%23meal-part:appetizer/rating`
}
axios.get(recipeIndex)
.then(res => dom = parse(res.data))
.then(() => openRecipe(setRecipe, setLoading))
}
function openRecipe(setRecipe, setLoading){
const recipes = dom.querySelectorAll('h3 a')
const num = Math.floor(Math.random()*recipes.length)
const recipe = recipes[num]
axios.get("https://" + recipe.attrs.href.slice(2))
.then(res => recipeObj = createRecipeObj(parse(res.data)))
.then(() => setRecipe(recipeObj))
.then(() =>{ if(setLoading) setLoading(false)})
}
function createRecipeObj(recipe){
const id = uuidv4()
let title = recipe.querySelector(".o-AssetTitle__a-HeadlineText")
title = title ? title.innerHTML : "Recipe Title Unavailable"
let chef = recipe.querySelector(".o-Attribution__a-Name a")
chef = chef ? chef.innerHTML : "Chef Unkown"
let ingredients = recipe.querySelectorAll(".o-Ingredients__a-Ingredient .o-Ingredients__a-Ingredient--CheckboxLabel")
ingredients = ingredients.length>0 ? ingredients.slice(1).map(i => i.innerHTML.trim()) : ["Ingredients Unavailable"]
let directions = recipe.querySelectorAll(".o-Method__m-Body ol li")
directions = directions.length>0 ? directions.map(d => d.innerHTML.trim()) : ["Directions Unavailable"]
const image = "https:" + recipe.querySelector(".m-MediaBlock__a-Image").attrs.src
let tags = recipe.querySelectorAll(".o-Capsule__m-TagList a").map(t => t.innerHTML)
tags = tags.filter(t => t.match(/Main Dish|Dessert|Appetizer|Side Dish/))
return {
id,
title,
chef,
ingredients,
directions,
image,
tags
}
}