-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (92 loc) · 2.76 KB
/
app.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
const Pageres = require('pageres')
const fetch = require('node-fetch')
const cheerio = require('cheerio')
const cronJob = require('cron').CronJob
const http = require('http')
// get from https://suzuri.jp/developer/apps
const API_KEY = "YOUR_SUZURI_API_KEY"
// page url you want to archive
const URL = 'http://page.to.archive/page/to/archive'
// target DOM to archive. (if you want to all of page, set to 'body')
const SELECTOR = '#content.you-want-to-archive'
// cron time. '0 0 0 * * *' is means that this app archive target page on 00:00 every day
const CRON_TIME = '0 0 0 * * *'
const options = {
selector: SELECTOR
}
var content = ''
var prevContent = ''
function archive() {
var chunks = []
fetch(URL)
.then(res => res.text())
.then(text => {
$ = cheerio.load(text)
content = $(SELECTOR).html()
if (content === prevContent || content === '') {
return Promise.reject()
}
return new Pageres(options)
.src(URL, ['1920x1080'])
.run()
})
.then(result => {
return new Promise((resolve, reject) => {
result[0].on('data', data => {
chunks.push(data)
})
result[0].on('end', () => {
var result = Buffer.concat(chunks)
resolve('data:image/png;base64,' + result.toString('base64'))
})
})
})
.then(img => {
var date = new Date
var itemId = [1,2,3,5,6,7,9][Math.floor(Math.random() * 7)]
var product = {
itemId: itemId,
published: true,
resizeMode: 'contain',
exemplaryAngle: 'left'
}
if (itemId === 3) {
product.exemplaryAngle = 'left'
}
var params = {
texture: img,
title: date.getFullYear() + '年' + ('0' + (date.getMonth() + 1)).slice(-2) + '月' + date.getDate() + '日' + ('0' + date.getHours()).slice(-2) + '時' + ('0' + date.getMinutes()).slice(-2) + '分',
description: content,
products: [ product ]
}
return fetch('https://suzuri.jp/api/v1/materials', {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + API_KEY
},
method: 'POST',
body: JSON.stringify(params)
})
})
.then(res => {
prevContent = content
return res.text()
})
.then(text => {console.log(text)})
}
var job = new cronJob({
cronTime: CRON_TIME,
onTick: archive,
start: true
});
job.start();
http.createServer((request, response) => {
if (request.url === '/favicon.ico') {
response.writeHead(200, {'Content-Type': 'image/x-icon'} );
response.end();
console.log('favicon requested');
return;
}
response.writeHead(200, {'Content-Type': 'text/plain'})
response.end(`${prevContent}\n`)
}).listen(process.env.PORT || 8080)