-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (107 loc) · 3.53 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
110
111
112
113
114
115
116
117
118
119
120
121
"use strict";
const { net } = require('electron');
const { performance } = require('perf_hooks');
class TunaNowPlayingPlugin {
#lastTimeUpdate;
constructor() {
/*
* Base Plugin Details
*/
this.name = "Tuna Now Playing Updater";
this.description = "Makes the currently playing Song information available to Tuna";
this.version = "0.1.0";
this.author = "ePirat";
this.#lastTimeUpdate = performance.now();
}
/**
* Returns a Tuna-compatible Now Playing Information
* object from the given Cider Now Playing attributes.
* @param attributes Music Attributes
*/
static #attributesToTunaInfo(attributes) {
if (!attributes || !attributes.name) {
return {
'status' : 'unknown',
};
}
let coverInfo = {}
if (attributes.artwork?.url) {
let coverUrl = attributes.artwork.url.replace('{h}', attributes.artwork.height).replace('{w}', attributes.artwork.width);
coverInfo = { 'cover_url' : coverUrl };
}
return {
'status' : (attributes.status) ? 'playing' : 'stopped',
'title' : attributes.name,
'album' : attributes.albumName,
'artists': [ attributes.artistName ],
'album_artist': attributes.primaryArtist,
'duration' : Math.floor(attributes.durationInMillis),
'progress' : Math.floor(attributes.currentPlaybackTime) * 1000,
...coverInfo
}
//console.log(attributes);
}
/**
* Update Tuna with the given Cider Now Playing attributes
* @param attributes Music Attributes
*/
#updateTuna(attributes) {
// Get Tuna object
let data = TunaNowPlayingPlugin.#attributesToTunaInfo(attributes);
console.log('Sending data', data);
const request = net.request({
method: 'POST',
url: 'http://localhost:1608/',
});
request.on('response', (response) => {
if (response.statusCode !== 200) {
console.error(`[TunaNowPlaying] Tuna rejected status update, HTTP status: ${response.statusCode}`);
return;
}
console.log(`[TunaNowPlaying] Successfully updated Tuna Now Playing info`);
});
request.on('error', (error) => {
console.error(`[TunaNowPlaying] Unable to contact Tuna API: ${JSON.stringify(error)}`);
});
request.end(JSON.stringify({ 'data' : data, 'date' : Date.now() }));
}
/**
* Runs on app ready
*/
onReady(win) {
console.log("[TunaNowPlaying] Ready");
}
/**
* Runs on app stop
*/
onBeforeQuit() {
console.log("[TunaNowPlaying] Terminating");
}
/**
* Runs on playback State Change
* @param attributes Music Attributes
*/
onPlaybackStateDidChange(attributes) {
this.#updateTuna(attributes);
}
/**
* Runs on song change
* @param attributes Music Attributes
*/
onNowPlayingItemDidChange(attributes) {
this.#updateTuna(attributes);
}
/**
* Runs on playback time change
* @param attributes Music Attributes
*/
playbackTimeDidChange(attributes) {
const monotonicNow = performance.now();
if (monotonicNow - this.#lastTimeUpdate < 2500) {
return;
}
this.#updateTuna(attributes);
this.#lastTimeUpdate = performance.now();
}
}
module.exports = TunaNowPlayingPlugin;