-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathog-image.js
More file actions
55 lines (49 loc) · 1.33 KB
/
Copy pathog-image.js
File metadata and controls
55 lines (49 loc) · 1.33 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
const puppeteer = require('puppeteer-core')
const chrome = require('chrome-aws-lambda')
const { PrismaClient } = require('@prisma/client')
const fsSync = require('fs')
const fs = require('fs/promises')
const path = require('path')
const prisma = new PrismaClient()
let browser
async function main() {
browser = await puppeteer.launch({
args: chrome.args,
executablePath:
'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
headless: chrome.headless
})
let slugs = await prisma.post.findMany({
select: {
slug: true
},
where: {
published: true
}
})
slugs.forEach(async ({ slug }) => {
let page = await browser.newPage()
await page.setViewport({ width: 1200, height: 627 })
console.log('navigating...')
await page.goto(`http://localhost:3000/og/${slug}`, {
waitUntil: 'domcontentloaded'
})
let buffer = await page.screenshot({
type: 'png',
clip: { x: 0, y: 0, width: 1200, height: 630 }
})
let imagePath = path.join(__dirname, 'public', 'og', `${slug}.png`)
if (fsSync.existsSync(imagePath)) {
console.log(`skipped!`)
} else {
if (buffer) {
await fs.writeFile(imagePath, buffer)
console.log(`wrote ${imagePath}`)
}
}
await page.close()
})
}
main().then(() => {
console.log('done')
})