-
Notifications
You must be signed in to change notification settings - Fork 24
/
mhlw-makeogp.mjs
92 lines (80 loc) · 2.57 KB
/
mhlw-makeogp.mjs
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
import webdriver from 'selenium-webdriver'
import fs from 'fs'
import Jimp from 'jimp'
import util from './util.mjs'
const { Builder, By, until } = webdriver;
/*
const url = 'http://localhost:8888/fukunojigjp/app/covid19/#ja'
const srcfn = '../index.html';
const dstpath = '../ogp/';
const prefix = 'covid19japan_ogp_';
const fullpath = "https://www.stopcovid19.jp/ogp/";
const delay = 5000;
*/
const url = 'http://127.0.0.1:8080/mhlw-beds.html';
const srcfn = '../mhlw-beds.html';
const dstpath = '../mhlw-ogp/';
const prefix = '';
const fullpath = "https://www.stopcovid19.jp/mhlw-ogp/";
const delay = 2000;
const capabilities = webdriver.Capabilities.chrome()
capabilities.set('chromeOptions', {
args: [
'--headless',
'--no-sandbox',
'--disable-gpu',
'--window-size=410,640'
]
})
const saveScreenShot = async function (url, fn) {
const driver = await new Builder().withCapabilities(capabilities).build()
await driver.get(url)
await driver.executeScript('document.body.style.overflow = "hidden"')
// await driver.wait(until.elementLocated(By.id('qrcode')), 10000)
await util.sleep(delay)
const base64 = await driver.takeScreenshot()
const buffer = Buffer.from(base64, 'base64')
fs.writeFileSync(fn, buffer)
driver.quit()
}
const resizeImage = async function (dstfn, fn) {
const dw = 1220
const dh = 640
const png = await Jimp.read(fn)
const h = (dw / png.bitmap.width * png.bitmap.height) >> 0
png.resize(dw, h)
png.crop(0, 0, dw, dh)
await png.write(dstfn)
}
const saveOGP = async function (url, dstfn) {
const fn = 'screenshot.png'
await saveScreenShot(url, fn)
await resizeImage(dstfn, fn)
}
const editOGP = function (srcfn, fn) {
let html = fs.readFileSync(srcfn, 'utf-8')
html = html.replace(/<meta property="og:image" content=".+">/, `<meta property="og:image" content="${fullpath}${fn}">`)
html = html.replace(/<meta name="twitter:image" content=".+">/, `<meta name="twitter:image" content="${fullpath}${fn}">`)
fs.writeFileSync(srcfn, html, 'utf-8')
}
const main = async function () {
if (process.argv.length >= 3) {
// console.log(process.argv)
const url = process.argv[2]
console.log(url)
const dstfn = process.argv.length >= 4 ? process.argv[3] : 'ss.png'
await saveOGP(url, dstfn)
//await cmd.cmd('open ' + dstfn)
} else {
const debug = false
const path = debug ? 'temp/' : dstpath;
const dstfn = prefix + util.getYMDHMS() + '.png'
console.log('write ' + dstfn)
await saveOGP(url, path + dstfn)
if (!debug) {
editOGP(srcfn, dstfn)
}
}
}
main()
export default { saveScreenShot }