-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
201 lines (177 loc) · 5.77 KB
/
player.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
198
199
200
201
const fs = require('fs');
const EventEmitter = require('events').EventEmitter;
const unix = require(__dirname + '/unix');
const mpd = require('mpd');
const commandSockDef = '/tmp/command.sock';
const lofiURLFileDef = process.env.HOME + '/.mpd/lofi.lst';
function player(commandSock=commandSockDef, lofiURLFile=lofiURLFileDef) {
let self = this;
this.videos = [];
this.mode = 'likes';
// Player's attributes
this.emitter = new EventEmitter();
try {
const raw = fs.readFileSync(lofiURLFile);
this.lofiURLs = raw.toString('utf-8').split('\n');
} catch {
console.log('failed to read the lofi URLs', lofiURLFile);
this.lofiURLs = [];
}
// MPD's attributes
this.mpd_ready = false;
this.mpd_state = 'stop';
this.cmd = mpd.cmd;
this.mpd = mpd.connect({
path: process.env.HOME+ "/.mpd/socket"
});
this._print_title = function(title) {
title = title.trim();
console.log("Title: " + title);
path = process.env.HOME + '/.mpd/current_title';
fs.writeFile(path, title, function (err) {
if (err) return console.log(err);
});
}
this.print_title = function() {
self.mpd_command('currentsong', [], function(err, msg) {
if (err) throw err;
const re = /^file: ([a-z0-9\.\-\_]*)$/im;
let found = msg.match(re);
if (found == null || found.length < 2) {
if (self.mode == 'lofi') {
self._print_title("Playing lofi music");
} else {
self._print_title("Unknown title");
}
return;
}
let filename = found[1];
found = self.videos.find(elem => elem.filename == filename);
let towrite = filename;
if (found != undefined) {
towrite = found.title;
}
self._print_title(towrite);
});
}
this.mpd_update_state = function() {
self.mpd_command("status", [], function(err, msg) {
if (err) throw err;
const re = /^state: ([a-z]*)$/im;
found = msg.match(re);
if (found == null || found.length < 2) {
self.mpd_set_state('stop');
} else {
self.mpd_set_state(found[1]);
}
self.print_title();
});
}
this.mpd.on('ready', function() {
console.log("mpd ready");
self.mpd_ready = true;
self.mpd_update_state();
self.mpd_command('random', ['1'])
self.mpd_command('repeat', ['1'])
});
this.mpd.on('system-player', function(name) {
self.mpd_update_state();
});
// MPD wrapper
this.mpd_command = function(cmd, args=[], callback=function () {}) {
if (!self.mpd_ready)
return;
return self.mpd.sendCommand(self.cmd(cmd, args), callback);
}
this.add = function(id, title) {
filename = id + '.webm';
if (self.videos.findIndex(elem => elem.filename == filename) != -1) {
return;
}
self.videos.push({'title': title, 'filename': filename});
self.mpd_command('update')
if (self.mode == 'likes')
self.mpd_command('add', [filename])
}
this.start = function() {
if (self.mpd_check_state('stop')) {
self.reload();
}
self.mpd_command('play');
}
this.stop = function() {
self.mpd_command('stop');
}
this.mpd_check_state = function(s) {
return self.mpd_state == s;
}
this.mpd_set_state = function(state) {
console.log('change the state to ', state);
self.mpd_state = state;
}
this.startstop = function() {
if (self.mpd_check_state('stop') || self.mpd_check_state('pause')) {
self.start();
} else {
self.stop();
}
}
this.reload = function() {
self.mpd_command('clear');
self.mpd_command('update');
if (self.mode == 'likes') {
self.videos.forEach(function (video) {
self.mpd_command('add', [video.filename]);
});
}
}
this.next = function() {
self.mpd.sendCommand('next');
}
this.mode_change = function() {
if (self.mode == 'likes') {
console.log('change mode to lofi');
self.mode = 'lofi';
self.mpd_command('clear');
self.lofiURLs.forEach(function(URL) {
if (URL.length == 0)
return;
console.log('adding URL', URL);
const exec = require('child_process').exec;
// TODO: refactoring to use downloader
// yt_downloader = 'youtube-dl';
yt_downloader = 'yt-dlp';
cmd = yt_downloader + ' -g ' + URL + ' | tail -n 1';
console.log(cmd);
exec(cmd, function (err, stdout, stderr) {
if (err) {
console.log(err);
return;
}
stdout = stdout.trim();
console.log(stdout);
if (stdout.length == 0) {
console.log("URL is broken", URL);
return;
}
self.mpd_command('add', [stdout]);
});
});
} else {
console.log('change mode to likes');
self.mode = 'likes';
self.reload();
}
}
// Register event handlers
this.emitter.on('start', self.startstop);
this.emitter.on('stop', self.startstop);
this.emitter.on('reload', self.reload);
this.emitter.on('next', self.next);
this.emitter.on('mode-change', self.mode_change);
// Launch sockets
this.handler = unix.handler(self.emitter, commandSock);
// Now we are ready
return this
}
module.exports.player = player;