Skip to content

Commit

Permalink
blastermak changes to music api
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Feb 22, 2025
1 parent 458b2fa commit 6837da8
Showing 1 changed file with 31 additions and 51 deletions.
82 changes: 31 additions & 51 deletions src/extension/music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class Music {
this.nodecg.log.info('[Music] Attempting connection');
const resp = await this.request(
'get',
'/query/updates?player=true&trcolumns=%artist%,%title%',
'/query?player=true&trcolumns=%artist%,%title%',
);
this.musicData.value.connected = true;
this.nodecg.log.info('[Music] Connection successful');
Expand All @@ -123,65 +123,45 @@ class Music {
throw new Error('body was null');
}

const readable = Readable.from(resp.body);
// const readable = Readable.from(resp.body);

readable.on('data', (chunk: Buffer) => {
let msg: Foobar2000.UpdateMsg | undefined;
try {
const cleaned = chunk.toString().slice(6).replace(/(\r\n|\n|\r)/gm, '');
msg = JSON.parse(cleaned);
} catch (err) {
this.nodecg.log.warn('[Music] Error parsing message on connection');
this.nodecg.log.debug('[Music] Error parsing message on connection:', err);
}
const msg = resp.body;

if (!msg || !msg.player) {
return;
}
// this.nodecg.log.debug('[Music] messageadata', msg);

if (this.positionInterval) {
clearInterval(this.positionInterval);
}
if (!msg || !msg.player) {
return;
}

const isPlaying = msg.player.playbackState === 'playing';
if (this.positionInterval) {
clearInterval(this.positionInterval);
}

this.musicData.value.playing = isPlaying;
const isPlaying = msg.player.playbackState === 'playing';

if (msg.player.playbackState === 'stopped') {
delete this.musicData.value.track;
return;
}
this.musicData.value.playing = isPlaying;

if (msg.player.activeItem.duration > 0) {
this.musicData.value.track = {
artist: msg.player.activeItem.columns[0] || undefined,
title: msg.player.activeItem.columns[1] || undefined,
position: msg.player.activeItem.position,
duration: msg.player.activeItem.duration,
};

if (isPlaying) {
this.positionInitial = msg.player.activeItem.position;
this.positionTimestamp = Date.now();
this.positionInterval = setInterval(() => this.updatePosition(), 1000);
}
}
});

readable.on('close', () => {
this.nodecg.log.warn('[Music] Connection closed');
});
if (msg.player.playbackState === 'stopped') {
delete this.musicData.value.track;
return;
}

readable.on('error', (err) => {
this.nodecg.log.warn('[Music] Connection error');
this.nodecg.log.debug('[Music] Connection error:', err);
});
if (msg.player.activeItem.duration > 0) {
this.musicData.value.track = {
artist: msg.player.activeItem.columns[0] || undefined,
title: msg.player.activeItem.columns[1] || undefined,
position: msg.player.activeItem.position,
duration: msg.player.activeItem.duration,
};

if (isPlaying) {
this.positionInitial = msg.player.activeItem.position;
this.positionTimestamp = Date.now();
this.positionInterval = setInterval(() => this.updatePosition(), 1000);
}
}

readable.on('end', () => {
this.musicData.value.connected = false;
this.nodecg.log.warn('[Music] Connection ended, retrying in 5 seconds');
setTimeout(() => this.setup(), 5 * 1000);
});
setTimeout(() => this.setup(), 5 * 1000);

// Listen to OBS transitions to play/pause correctly.
this.obs.conn.on('TransitionBegin', (data) => {
Expand Down

0 comments on commit 6837da8

Please sign in to comment.