-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhumix-spotify.js
98 lines (75 loc) · 2.5 KB
/
humix-spotify.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
var HumixSense = require('humix-sense');
var SpotifyPlayer = require('./index');
var fs = require('fs');
//var moduleConfig = require('./config.js');
var config = {
"moduleName": "humix-spotify-module",
"commands": ["play-spotify", "pause-spotify", "resume-spotify", "stop-spotify", "next-spotify"],
"events": [],
"log": {
file: 'humix-spotify-module.log',
fileLevel: 'info',
consoleLevel: 'debug'
}
};
var humix = new HumixSense(config);
var spotify;
var hsm;
var logger;
console.log('========= starting ===========');
humix.on('connection', function(humixSensorModule) {
hsm = humixSensorModule;
logger = hsm.getLogger();
logger.info('access config');
var conf = hsm.getDefaultConfig();
if (!conf) {
if (fs.existsSync('./config.js')) {
logger.info('using local config file')
conf = require('./config.js');
} else {
logger.error('fail to load conversation config. Exit');
process.exit(1);
}
}
logger.info('loading config: ' + JSON.stringify(conf));
spotify = new SpotifyPlayer(conf, logger)
spotify.init();
logger.info('Communication with humix-sense is now ready.');
hsm.on("play-spotify", function(data) {
logger.info('received play-spotify data: ' + data);
// TODO : Check the type of data.
var re = /\{.*\}/;
if (data.match(re)) {
var obj = JSON.parse(data);
if (obj.songName || obj.artistName) {
logger.info("obj.songName = " + obj.songName);
logger.info("obj.artistName = " + obj.artistName);
spotify.playSong(obj.songName, obj.artistName);
return;
} else if (obj.ownerId && obj.playlistId) {
spotify.playByPlaylist(obj.ownerId, obj.playlistId);
return;
} else if (obj.searchPlaylist) {
spotify.playPlaylistBySearch(obj.searchPlaylist);
return;
} else if (obj.artistId) {
spotify.playTopTracksForArtist(obj.artistId);
return;
}
} else {
spotify.playSong(data);
}
});
hsm.on("pause-spotify", () => {
spotify.pausePlaying();
});
hsm.on("resume-spotify", () => {
spotify.resumePlaying();
});
hsm.on("stop-spotify", () => {
spotify.stopPlaying();
});
hsm.on("next-spotify", () => {
spotify.nextSong();
});
});