/
index.js
118 lines (99 loc) · 3.82 KB
/
index.js
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
const axios = require('axios');
const cheerio = require('cheerio');
const { response } = require('express');
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const getFo = async (response) => {
const $ = cheerio.load(response.data);
const drinkCategories = $('.drink .c-menu');
const reallyGoodList = $('.menu_title_spacer')[$('.menu_title_spacer').length - 1];
const finalList = [];
drinkCategories.each(async (index, element) => {
let beerType, beerName, beerDescription;
element.children.forEach(async(c) => {
c.attribs?.name && typeof c.attribs.name !== 'undefined' ? beerType = c.attribs.name : null
if(typeof await beerType !== 'undefined' && await beerType !== 'White/Orange/Pink Wine' && await beerType !== 'Sparkling Wine' && await beerType !== 'Red Wine') {
$(c).find('.c-dish__title').text() ? (
beerName = $(c).find('.c-dish__title')?.text(),
beerDescription = $(c).find('.c-dish__title')?.siblings().first().next().next()?.text(),
finalList.push({type: beerType.replace(/\.\s*$/, '').replace(/\s+$/, ''), name: beerName.replace(/\.\s*$/, ''), description: beerDescription})
) : null
}
if($(c).text() === 'The Really Good Sh...') {
beerType = $(c).text();
const reallyGoodBeers = $(reallyGoodList).siblings().find('.c-dish');
reallyGoodBeers.prevObject.each(async (index, element) => {
const beerName = $(element).find('.c-dish__title')?.text();
beerDescription = $(element).find('.c-dish__title')?.siblings().first().next().next()?.text(),
finalList.push({type: beerType.replace(/\.\s*$/, '').replace(/\s+$/, ''), name: beerName.replace(/\.\s*$/, ''), description: beerDescription})
})
}
});
});
return finalList;
}
const getMonkish = async (response) => {
const $ = cheerio.load(response.data);
const pageH2s = $('h2');
const finalList = {
"here": {
title: "At Taproom",
list: []
},
"away": {
title: "To Go",
list: []
}
};
pageH2s.each((index, element) => {
const text = $(element).text();
if(text === 'BEER MENU || ENJOY HERE' || text === 'BEER MENU || ENJOY AWAY') {
const location = text.includes('HERE') ? 'here' : 'away';
const beerList = element.parent.parent.parent.nextSibling;
beerList.children[0].children.forEach(async(c, i) => {
let beerType, beerName, beerDescription;
$(c).children().find('.menu-section-title').each(async (index, element) => {
beerType = $(element).text().replace(/\b(\w)(\w*)\b/g, (match, p1, p2) => p1 + p2.toLowerCase());
$(c).children().find('.menu-item-title').each(async (index, element) => {
beerName = $(element).text();
beerDescription = $(element).next().text();
finalList[location].list.push({type: beerType, name: beerName, description: beerDescription});
})
})
})
}
})
return finalList;
}
app.get('/api/:website', async(req, res) => {
const { website } = req.params;
let url;
switch (website) {
case 'fo':
url = 'https://fathersoffice.com/menus/santa-monica/';
try {
axios.get(url)
.then(async(response) => getFo(response))
.then(list => res.send(list))
} catch(err) {
console.log(err)
res.sendStatus(500); // Internal Server Error
}
break;
case 'monkish':
url = 'https://www.monkishbrewing.com/tastingroom';
try {
axios.get(url)
.then(async(response) => getMonkish(response))
.then(list => res.send(list))
} catch(err) {
console.log(err)
res.sendStatus(500); // Internal Server Error
}
break;
}
});
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});