-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastfm.js
158 lines (121 loc) · 6.38 KB
/
lastfm.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
var tessel = require('tessel');
var thermalprinter = require('../'); // Replace '../' with 'tessel-thermalprinter' in your own code
var printer = thermalprinter.use(tessel.port['A']);
var http = require('http');
var util = require('util');
var lastfm = {
apiKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', // get your key here http://www.last.fm/api
username: 'zaccolley'
};
printer.on('ready', function(){
console.log('Printer ready!');
console.log('Printing intro...');
printer
.setLineSpacing(24)
.lineFeed(3)
.parseSpecialLine(" ╔══════════════════════╗ ")
.parseSpecialLine(" ║▒╔══════════════════╗▒║ ")
.parseSpecialLine(" ║▒║ ║▒║ ")
.parseSpecialLine("╔═══╩═╩══════════════════╩═╩═══╗")
.parseSpecialLine("║▓▒░░░░ ░░░░▒▓║")
.parseSpecialLine("│▒ last.fm tracks ▒│")
.parseSpecialLine("│ │")
.parseSpecialLine("│ 'get' for latest track │")
.parseSpecialLine("│ 'quit' to........ quit │")
.parseSpecialLine("│ │")
.parseSpecialLine("╟──────────────────────────────╢")
.parseSpecialLine("│ · · · │")
.parseSpecialLine("│ You listened to: │")
.parseSpecialLine("└┐ · · · ┌┘")
.print(function(){
console.log('Printed!');
console.log("Type 'get' for latest track");
console.log("Type 'quit' to quit");
});
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (text){
console.log('Received input: ', util.inspect(text));
if(text === 'get\n'){
getLatestTrack(function(track){
console.log("You're listening to: ", track);
console.log("Printing latest track...");
printer
.parseSpecialLine(" ├ ─ ─ ─ ─ ─ ─ ── ─ ─ ─ ─ ─ ─ ┤ ")
.parseSpecialLine("┌┘ └┐")
.parseSpecialLine("│ ∞ ≈ ⁿ ² · ÷ │")
.parseSpecialLine("│ │")
.parseSpecialLine("│ " + pad(28, truncate(track.name, 28), ' ') + " │")
.parseSpecialLine("│ " + pad(28, truncate(track.artist, 28), ' ') + " │")
.parseSpecialLine("│ " + pad(28, truncate(track.album, 28), ' ') + " │")
.parseSpecialLine("│ │")
.parseSpecialLine("│ ² · ÷ ∞ ≈ ⁿ │")
.parseSpecialLine("└┐ ┌┘")
.print(function(){
console.log('Printed!');
console.log('Waiting for input...');
console.log("Type 'get' for latest track");
console.log("Type 'quit' to quit");
});
});
}else if(text === 'quit\n'){
console.log("Printing bottom...");
printer
.parseSpecialLine(" ├ ─ ─ ─ ─ ─ ─ ── ─ ─ ─ ─ ─ ─ ┤ ")
.parseSpecialLine("┌┘▒ · · ·░░▒└┐")
.parseSpecialLine("║▓▒░┌────┐░░░░░░░░░░░░┌────┐░▒▓║")
.parseSpecialLine("║▓▒┌┘▒░░▒└┐░╔══════╗░┌┘▒░░▒└┐▒▓║")
.parseSpecialLine("║▓▒│▒÷▒▒÷▒│░║ Θ¥¥Θ ║░│▒÷▒▒÷▒│▒▓║")
.parseSpecialLine("║▓▒└┐▒░░▒┌┘░╚══════╝░└┐▒░░▒┌┘▒▓║")
.parseSpecialLine("║▓▒░└────┘░░░░░░░░░░░░└────┘░▒▓║")
.parseSpecialLine("║▓▒▒░░░░░░░░░░░░░░░░░░░░░░░░▒▒▓║")
.parseSpecialLine("╚══════════════════════════════╝")
.lineFeed(3)
.print(function(){
console.log('Printed!');
console.log('Quitting now. Bye bye. :¬)');
process.exit();
});
}
});
});
// string manipulation
function truncate(string, length){
if( string.length > length ){
return string.substring(0, length - 3) + '...';
}else{
return string;
}
}
function pad(width, string, padding){
return (width <= string.length) ? string : pad(width, string + padding, padding);
}
// last.fm junk
function getLatestTrack(callback){
console.log("Getting the latest track!");
// recent tracks
var url = 'http://ws.audioscrobbler.com/2.0/' +
'?method=user.getrecenttracks' +
'&limit=1' +
'&user=zaccolley' +
'&api_key=fa5687767b9d45951f19973b88ff46d9' +
'&format=json';
http.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var json = JSON.parse(body);
if(typeof json.recenttracks.track !== 'undefined'){ // if the json does exist (last.fm isn't borked)
callback({
name: json.recenttracks.track[0].name,
artist: json.recenttracks.track[0].artist["#text"],
album: json.recenttracks.track[0].album["#text"]
});
}
});
}).on('error', function(e){
console.log("Got error: ", e);
});
}