-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (46 loc) · 1.34 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
const http = require('http')
const url = require('url')
const promise = require('bluebird')
const gm = require('gm').subClass({imageMagick: true})
const fetch = require('node-fetch')
promise.promisifyAll(gm.prototype)
async function getImage(url) {
const res = await fetch(url).catch(() => null)
if (res) {
const buffer = res.buffer()
return buffer
} else {
return null
}
}
async function detectBpm(buffer) {
const duration = await gm(buffer, 'image.gif').identifyAsync('%T,').catch(() => '0')
return convertDurationToBpm(duration.split(',').reduce((acc, cur) => Number(acc) + Number(cur)))
}
function convertDurationToBpm(duration) {
console.log(duration)
return roundBpm(60 * 100 / duration)
}
function roundBpm(bpm) {
if (bpm > 180) {
return (roundBpm(bpm / 2))
} else if (bpm < 70) {
return (roundBpm(bpm * 2))
} else {
return bpm
}
}
const server = http.createServer(async (request, response) => {
const urlInfo = url.parse(request.url, true)
const targetUrl = urlInfo.query.url
let result = 0
if (targetUrl) {
const imageBuffer = await getImage(targetUrl)
if (imageBuffer) {
result = await detectBpm(imageBuffer).catch(() => 0)
}
}
response.writeHead(200, {'Content-Type': 'text/plain'})
response.write(`${result}`)
response.end()
}).listen(process.env.PORT || 4000)