forked from robfromca/jukenode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress.js
156 lines (135 loc) · 3.41 KB
/
express.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
var express = require('express');
var util = require('util'),
exec = require('child_process').exec,
child;
var sys = require('sys');
var queue = require('./queue.js');
function runApplescriptFile(osascript, args, next) {
commandLine = 'osascript ' + osascript + " '" + args + "'";
exec(commandLine,
function(error, stdout, stderr) {
if (error !== null) {
next('NodeError: ' + error + "\nargs:[" + commandLine + "]");
} else {
next(stdout);
}
});
}
function runApplescript(osascript, next) {
exec('osascript -e \'tell application "iTunes"\' -e "' + osascript + '" -e "end tell"',
function (error, stdout, stderr) {
if (error !== null) {
next('NodeError: ' + error);
} else {
next(stdout);
}
});
}
var app = express.createServer();
app.configure(function() {
app.use(express.logger());
app.use(express.errorHandler({
dumpExceptions:true,
showTack:true
}));
app.use(express.static(__dirname + '/static'));
});
app.set('views', __dirname + '/views');
var songs = require('./songs');
app.get('/', function(request, response) {
response.render('root.jade');
});
app.get('/status.json', function(req,res) {
res.contentType('application/json');
runApplescriptFile('status.applescript', '', function(data) {
if (data == 'paused') {
res.send({status:"paused"});
} else {
item = data.split('\\');
res.send(
{id:item[0],
name:item[1],
album:item[2],
artist:item[3],
year:item[4],
bitrate:item[5],
runningtime:item[6],
duration:item[7],
playerstate:item[8],
playerposition:item[9]
});
}
});
});
app.get('/status', function(req,res) {
runApplescriptFile('status.applescript', '', function(data) {
if (data == 'paused') {
res.send({status:"paused"});
} else {
item = data.split('\\');
status =
{id:item[0],
name:item[1],
album:item[2],
artist:item[3],
year:item[4],
bitrate:item[5],
runningtime:item[6],
duration:item[7],
playerstate:item[8],
playerposition:item[9]
};
res.partial('status.jade', status);
}
});
});
app.get('/songs', function(req,res) {
res.render('songindex.jade', {locals: {
songs:songs.all
}});
});
app.get('/playlist/:name', function(req,res) {
safe_playlist = req.params.name.replace(/[^A-Za-z-_0-9 \-]/g, "")
runApplescriptFile('listTracks.applescript', safe_playlist, function(data) {
songlist = [];
data.split('\n').forEach(function(line_item) {
item = line_item.split('\\');
songlist.push(
{id:item[0],
name:item[1],
album:item[2],
artist:item[3],
year:item[4],
bitrate:item[5],
runningtime:item[6],
duration:item[7]
});
});
res.render('songindex.jade', {locals: {
songs:songlist
}});
});
});
app.get('/play/:id', function(req, res) {
safe_id = req.params.id.replace(/[^0-9]/g, "")
playstring = 'play (every track whose database ID is ' + safe_id + ')';
runApplescript(playstring, function(data) {
res.render('play.jade');
});
});
app.get('/stop', function(req, res) {
runApplescript('pause', function(data) {
res.render('play.jade');
});
});
app.get('/queue/add/:id', function(req, res) {
queue.add(req.params.id);
res.json("", 204);
});
app.get('/queue', function(req, res) {
res.json(queue.contents());
});
app.get('/queue/random', function(req, res) {
res.json(queue.random());
});
app.listen(4000);