-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·108 lines (95 loc) · 3.5 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
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
import express from 'express';
import mustache from 'mustache';
import url from 'url';
import * as utils from './utils.js';
import * as config from './config.js';
import * as screenshot from './screenshot.js';
import * as ly from './lyrics.js';
const app = express();
// STATE
let lastSong ='';
// template server, it gets all the data and returns a webpage
app.get('/', async(req, res) => {
let data = {};
console.log('* /', 'Request Received', req.url);
console.time("renderTemplate");
utils.currentSong()
.then(songRes => {
console.log('* /', 'Song Updated');
data = songRes;
const addr = req.query.addr ? req.query.addr : 'addr:111.111.77.77';
const battery = req.query.battery ? req.query.battery : 'Battery Level: 117%';
data.battery = utils.getPercent(battery);
data.addr = utils.getIp(addr);
return utils.getDurationInfo();
}).then(duration => {
data.duration = duration;
}).then(() => {
console.log('* /', 'Fetching Lyrics');
return ly.fetchLyrics(data.title, data.artist);
}).then(lyrics => {
console.log('* /', 'Sending Template');
data.lyrics = ly.processLyrics(lyrics || '');
// sets full cover url including server
data.coverurl = `${config.moodeServer}${data.coverurl}`;
var output = mustache.render(utils.loadTemplate(), data);
res.send(output);
console.timeEnd("renderTemplate");
})
.catch(err => {
console.log('** ERROR', err);
});
});
// send screenshot to client
function sendScreenshot(req, res) {
console.time("updateKindle");
const kindleStatusString = url.parse(req.url).query;
console.time("takeScreenshot");
screenshot.takeScreenshot(kindleStatusString).then((screenshot) => {
console.timeEnd("takeScreenshot");
if(screenshot) {
console.log("** SCREENSHOT OK");
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': screenshot.length,
});
console.log('* /refresh', 'Sending Screenshot to Kindle');
res.end(screenshot);
}
else {
res.writeHead(500);
res.end();
}
console.timeEnd("updateKindle");
});
}
// returns kindle sent image for debugging purpose
app.get('/image', async(req, res) => {
sendScreenshot(req, res);
});
// kindle server, if a new song is detected screenshots the webpage then sends it to kindle, code: 200
// if there are no updates returns 204 no-content
app.get('/refresh', async(req, res) => {
console.log('*');
// console.log('* /refresh', 'Request Received', req.url);
utils.currentSong()
.then(data => {
// console.log('* /refresh', 'Song Updated', req.url);
const dfile = data.file;
const songChanged = dfile && lastSong != dfile;
if (songChanged) {
console.log("** NEW SONG DETECTED");
lastSong = dfile;
sendScreenshot(req, res);
} else {
// console.log('* /refresh', 'No Updates Sent', req.url);
res.writeHead(204);
res.end();
}
});
});
// serve static files js, css, etc
app.use(express.static('template'));
app.listen(config.port, () => {
console.log(`Koode Server listening on port ${config.port}`)
});