-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (94 loc) · 2.62 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
108
109
'use strict';
var express = require('express');
var request = require('request');
var odm = require('openhome-devices-manager');
var app = express();
var util = require('util');
// Edit this value to point to your device name
var deviceName = 'Linn Main Room'
var device = {};
app.route('/track')
.get(function(req, res) {
if(!device.name && !findDevice()) {
return res.status(200).send({
albumArtURI: '',
title: '',
artist: '',
album: ''
});
}
device.ds.currentTrackDetails(function(error, result) {
var response = {
albumArtURI: '',
title: '',
artist: '',
album: ''
};
var body = result && result.metadata ? result.metadata : null;
if(!error && body) {
if(body.indexOf('upnp:albumArtURI') !== -1) {
response.albumArtURI = resizeTidal(
deescape(
body.split('upnp:albumArtURI')[1]
.split('>')[1]
.split('<')[0]
)
);
}
if(/upnp:album[ >]/.test(body)) {
response.album = deescape(
body.split(/<upnp:album(>| [^>]*>)/)[2]
.split('</upnp:album')[0]
);
}
if(body.indexOf('upnp:artist') !== -1) {
response.artist = deescape(
body.split('<upnp:artist')[1].split('>')[1].split('</upnp:artist')[0]
);
}
if(body.indexOf('<dc:title') !== -1) {
response.title = deescape(
body.split('<dc:title')[1].split('>')[1].split('</dc:title')[0]
);
}
}
res.status(200).send(response);
});
});
app.use(express.static('app'));
var resizeTidal = function(url) {
if(url.indexOf('tidalhifi') !== -1) {
return url.split('w=250&h=250').join('w=700&h=700');
} else {
return url;
}
}
var insensitiveEquals = function(a, b) {
return a.toLowerCase().indexOf(b.toLowerCase()) !== -1;
}
var findDevice = function() {
var dev;
var devs = odm.getDevices();
while(!(dev = devs.next()).done) {
if(dev.value) {
var d = odm.getDevice(dev.value);
console.log("Found device: " + odm.getDevice(dev.value).name);
if (insensitiveEquals(odm.getDevice(dev.value).name, deviceName)) {
device = odm.getDevice(dev.value);
return true;
}
}
}
return false;
}
var deescape = function(a) {
while(a.split('&').length > 1) {
a = a.replace('&', '&');
}
while(a.split(''').length > 1) {
a = a.replace(''', '\'');
}
return a;
}
console.log("App running on port "+(process.env.PORT || 3000));
app.listen(process.env.PORT || 3000);