-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
197 lines (150 loc) · 4.98 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require('dotenv').config();
const {
LASTFM_API,
LASTFM_SECRET,
LASTFM_USERAGENT,
LASTFM_USERNAME,
PRINTER_USB,
PRINTER_BAUDRATE,
PRINTER_ROTATION
} = process.env;
if (
!LASTFM_API ||
!LASTFM_SECRET ||
!LASTFM_USERAGENT ||
!LASTFM_USERNAME ||
!PRINTER_USB ||
!PRINTER_BAUDRATE ||
!PRINTER_ROTATION
) {
return console.error('Missing environment variabes.');
}
const SerialPort = require('serialport');
const serialPort = new SerialPort(PRINTER_USB, { baudrate: +PRINTER_BAUDRATE });
const Printer = require('thermalprinter');
const PRINT_WIDTH = 384;
const request = require('request');
const LastFmNode = require('lastfm').LastFmNode;
const lastfm = new LastFmNode({
api_key: LASTFM_API, // sign-up for a key at http://www.last.fm/api
secret: LASTFM_SECRET,
useragent: LASTFM_USERAGENT
});
const gm = require('gm');
serialPort.on('open', _ => {
console.log('Serial port open!');
/*
maxPrintingDots = 0-255. Max heat dots, Unit (8dots), Default: 7 (64 dots)
heatingTime = 3-255. Heating time, Unit (10us), Default: 80 (800us)
heatingInterval = 0-255. Heating interval, Unit (10µs), Default: 2 (20µs)
The more max heating dots, the more peak current will cost when printing,
the faster printing speed. The max heating dots is 8*(n+1).
The more heating time, the more density, but the slower printing speed.
If heating time is too short, blank page may occur.
The more heating interval, the more clear, but the slower printing speed.
*/
const printer = new Printer(serialPort, {
maxPrintingDots: 10,
heatingTime: 150,
heatingInterval: 120,
commandDelay: 2
});
printer.on('ready', _ => {
console.log('Printer ready!');
const trackStream = lastfm.stream(LASTFM_USERNAME);
trackStream.on('error', error => {
if (error.message) {
return console.error('Error: ', error.message);
}
});
trackStream.on('nowPlaying', track => {
console.log('Paused track stream');
trackStream.stop();
getTrackInfo(track)
.then(getAndDitherImage)
.then(imagePath => {
console.log(`Image save to: ${imagePath}`);
printer.printImage(imagePath).lineFeed(2).print(_ => {
console.log('Printed image');
console.log('Resumed track stream');
trackStream.start();
});
})
.catch(error => {
console.error(error);
console.log('Resumed track stream');
trackStream.start();
});
});
trackStream.start();
});
});
const getAndDitherImage = (track) => new Promise((resolve, reject) => {
console.log('Dithering album art');
if (!track) {
return reject('Missing `track`');
}
const imageFilename = track.imageUrl.substr(track.imageUrl.lastIndexOf('/'));
const imagePath = `${__dirname}/images/${imageFilename}`;
const FONT_SIZE = 16;
const TEXT_MAX_LENGTH = 22;
function processText(text) {
text = text.toUpperCase();
if (text.length <= TEXT_MAX_LENGTH) {
return text;
}
return text.substr(0, TEXT_MAX_LENGTH - 1) + '…';
}
function niceDate() {
let date = new Date().toISOString();
date = date.replace('T', ' ').replace('Z', ' ');
date = date.substr(0, date.lastIndexOf(':'));
return date.trim();
}
gm(request(track.imageUrl), imageFilename)
// make the image cripser, black and white and then dither
.sharpen(5)
.monochrome()
.dither()
.borderColor('#000')
.border(1, 1)
// center on a white background the size of the printer paper
.gravity('Center')
.extent(PRINT_WIDTH, PRINT_WIDTH)
.fill('#000')
.font(`${__dirname}/fonts/source-sans-pro-700.ttf`, FONT_SIZE)
// put text on each side
.drawText(0, FONT_SIZE, processText(track.name), 'North').rotate('#fff', 90)
.font(`${__dirname}/fonts/source-sans-pro-700-italic.ttf`, FONT_SIZE)
.drawText(0, FONT_SIZE, processText(track.artist), 'North').rotate('#fff', -180)
.drawText(0, FONT_SIZE, processText(track.album), 'North').rotate('#fff', 90)
.font(`${__dirname}/fonts/source-sans-pro-600.ttf`, FONT_SIZE)
.drawText(0, FONT_SIZE, niceDate(), 'South')
// finally rotate, depending on the orientation of the printer
.rotate('#fff', PRINTER_ROTATION)
.write(imagePath, error => {
if (error) {
return reject(error);
}
resolve(imagePath);
});
});
const getTrackInfo = (data) => new Promise((resolve, reject) => {
console.log('Getting track info');
if (!data || !data.image || data.image.length <= 0) {
return reject('No album art');
}
const track = {
name: data.name,
imageUrl: data.image[3]['#text'],
artist: data.artist['#text'],
album: data.album['#text']
};
// if album art
if (!track || track.imageUrl.length <= 0) {
return reject('No album art');
}
console.log(`Track info: ${track.name} - ${track.artist} - ${track.album}`);
console.log(`Track image url: ${track.imageUrl}`);
resolve(track);
});