-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer.js
162 lines (136 loc) · 4.76 KB
/
renderer.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
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
const puppeteer = require('puppeteer');
(async () => {
let browser = null
try {
browser = await puppeteer.launch()
const page = await browser.newPage()
page.setViewport({ width: 1920, height: 1080, deviceScaleFactor: 2 })
await page.goto(process.env.COMEDORES_URL)
await page.waitForSelector('.inline')
let data = await page.evaluate(() => {
const sanitizeDate = date => {
return date.replace(/\s/g, '').toLowerCase()
}
const includesDay = (container, dayStr) => {
let trs = container.querySelectorAll('tr')
for (let i = 0; i < trs.length; i++) {
let th = trs[i].querySelector('th.leftalign > strong')
let date = th === null ? null : sanitizeDate(th.textContent)
if (date !== null && date.includes(dayStr)) return true
}
return false
}
let days = [
'domingo',
'lunes',
'martes',
'miércoles',
'jueves',
'viernes',
'sábado'
]
let tables = []
let currentDate = new Date()
// sunday (0) - saturday (6)
let dayOfWeek = currentDate.getDay()
if (dayOfWeek === 0) { // If sunday, load next week menu
currentDate.setDate(currentDate.getDate() + 1)
dayOfWeek = currentDate.getDay()
}
// set current date as monday of the current week
let diff = (dayOfWeek - 1) * 24 * 60 * 60 * 1000
currentDate.setTime(currentDate.getTime() - diff)
let containers = document.querySelectorAll('div.content_doku > div.level1')
let container = null
for (let i = 0; i < containers.length; i++) {
let dateAux = new Date(currentDate.getTime())
let found = false
// Test all days of the week until we find one of them on the tables
let dayOffset = 0
while (dayOffset !== 7) {
dateAux.setDate(dateAux.getDate() + (dayOffset === 0 ? 0 : 1))
let dayOfMonth = dateAux.getDate()
let dayStr = days[dateAux.getDay()] + ',' + dayOfMonth + 'd'
if (includesDay(containers[i], dayStr)) {
container = containers[i]
found = true
}
if (found) break
dayOffset++
}
if (found) break
}
// If the menu of the current week couldn't be found, just return
if (container === null) return tables
let trs = container.querySelectorAll('tr')
let dateIdxs = []
for (let i = 0; i < trs.length; i++) {
let th = trs[i].querySelector('th.leftalign')
let date = th === null ? null : sanitizeDate(th.textContent)
if (date !== null && days.includes(date.substring(0, date.indexOf(',')))) {
dateIdxs.push(i)
}
}
let out = []
for (let i = 0; i < dateIdxs.length; i++) {
let dateIdx = dateIdxs[i]
let date = sanitizeDate(trs[dateIdx].querySelector('th.leftalign').textContent)
let outTable = document.createElement('table')
let outTableBody = document.createElement('tbody')
outTable.append(outTableBody)
let endIndex = i === dateIdxs.length - 1 ? trs.length : dateIdxs[i + 1]
for (let j = dateIdx; j < endIndex; j++) {
outTableBody.append(trs[j])
}
out.push({ table: outTable, date: date })
}
// Add all tables to DOM before getting their client rect
for (let i = 0; i < out.length; i++) {
container.appendChild(out[i].table)
}
for (let i = 0; i < out.length; i++) {
const { x, y, width, height } = out[i].table.getBoundingClientRect()
tables.push({
rect: {
left: x,
top: y,
width,
height
},
date: out[i].date
})
}
return tables
})
console.log('INFO Rendering ' + data.length + ' images')
// Render only tables with menu of each day
let screenshots = []
for (let i = 0; i < data.length; i++) {
let tableRect = data[i].rect
let tableDate = data[i].date
if (tableDate === null) continue
let imagePath = 'images-new/' + tableDate + '.png'
screenshots.push(page.screenshot({
path: imagePath,
clip: {
x: tableRect.left,
y: tableRect.top,
width: tableRect.width,
height: tableRect.height
}
}).then(i => {
console.log('INFO Image ' + imagePath + ' rendered successfully')
}))
}
await Promise.all(screenshots)
await browser.close()
console.log('INFO Browser closed')
} catch (e) {
if (browser !== null) {
await browser.close()
console.error("ERROR Can't render menu images", e.stack)
} else {
console.error("ERROR Can't render menu images. Browser not initialized.", e.stack)
}
}
})()