-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
208 lines (185 loc) · 5.69 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
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
202
203
204
205
206
207
208
const { app, BrowserWindow, Tray, Menu, shell, Notification, dialog } = require('electron');
const path = require('path');
const DiscordRPC = require('discord-rpc');
const fetch = require('request-promise');
const prettyMilliseconds = require('pretty-ms');
const express = require('express');
const server = express();
const iconPath = path.join(__dirname, './icons/logo.ico');
let appIcon = null;
let status = false;
if (require('electron-squirrel-startup')) {
app.quit();
}
const createWindow = () => {
const mainWindow = new BrowserWindow({
autoHideMenuBar: false,
width: 1280,
height: 720,
minWidth: 1024,
minHeight: 550,
maximizable: true,
resizable: true,
frame: true,
icon: iconPath,
show: false,
autoHideMenuBar: true.valueOf,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
}
});
mainWindow.on('ready-to-show', mainWindow.show);
mainWindow.loadFile(path.join(__dirname, 'index.html'));
};
server.use(express.json());
const client = new DiscordRPC.Client({ transport: 'ipc' });
client.on('error', error => {
console.log(`An unexpected error occurred: ${error}`);
});
//Fetch function
server.post('/api/post-presence', (req, res) => {
const { clientid, username, key } = req.body;
if (status === true) {
client.clearActivity().then(() => {
status = false;
console.log('Stopped Rich Presence\nThank you for using Last.fm Rich Presence');
});
} else {
status = true;
console.log('Started Rich Presence');
async function fetchCurrentScrobble() {
var TrackOptions = {
uri: 'http://ws.audioscrobbler.com/2.0/',
json: true,
qs: {
method: 'user.getrecenttracks',
user: username,
api_key: key,
format: 'json',
limit: '1'
}
};
var lastTrack = await fetch(TrackOptions);
if (!lastTrack)
return console.log(
'An unexpected error occurred while fetching\nPlease check if the Last.fm username provided is correct\nRetrying in 30 seconds...'
);
let lastArtist = lastTrack.recenttracks.track[0].artist['#text'];
let lastTrackName = lastTrack.recenttracks.track[0].name;
var UserOptions = {
uri: 'http://ws.audioscrobbler.com/2.0/',
json: true,
qs: {
method: 'track.getInfo',
user: username,
track: lastTrackName,
artist: lastArtist,
api_key: key,
format: 'json'
}
};
var fetchedData = await fetch(UserOptions);
if (fetchedData.message && fetchedData.message === 'Track not found') {
var data = {
artist: lastTrack.recenttracks.track[0].artist['#text'],
album: lastTrack.recenttracks.track[0].album['#text'],
trackName: lastTrack.recenttracks.track[0].name,
trackUrl: lastTrack.recenttracks.track[0].url,
playcount: '0',
scrobbleStatus: !lastTrack.recenttracks.track[0]['@attr']
? `Last scrobbled ${prettyMilliseconds(Date.now() - lastTrack.recenttracks.track[0].date.uts * 1000)} ago
`
: 'Now scrobbling',
cover: lastTrack.recenttracks.track[0].image[lastTrack.recenttracks.track[0].image.length - 1]['#text']
};
} else {
var data = {
artist: lastTrack.recenttracks.track[0].artist['#text'],
album: lastTrack.recenttracks.track[0].album['#text'],
trackName: lastTrack.recenttracks.track[0].name,
trackUrl: lastTrack.recenttracks.track[0].url,
playcount: fetchedData.track.userplaycount ? fetchedData.track.userplaycount : '0',
scrobbleStatus: !lastTrack.recenttracks.track[0]['@attr']
? `Last scrobbled ${prettyMilliseconds(Date.now() - lastTrack.recenttracks.track[0].date.uts * 1000)} ago
`
: 'Now scrobbling',
cover: lastTrack.recenttracks.track[0].image[lastTrack.recenttracks.track[0].image.length - 1]['#text']
};
}
return data;
}
//Status update function
async function updateStatus() {
var data = await fetchCurrentScrobble();
// Verifying Data
let detailsStatus = 'Listening to';
if (data.scrobbleStatus !== 'Now scrobbling') detailsStatus = `Was ${detailsStatus}`;
let albumName = data.album;
if (data.album.length < 2) albumName = `${albumName} `;
client
.setActivity({
details: `${detailsStatus} ${data.trackName}`,
buttons: [
{
label: `View on Last.fm - ${data.playcount} Scrobbles`,
url: data.trackUrl
}
],
state: `by ${data.artist} on ${data.album}`,
largeImageKey: data.cover,
largeImageText: albumName,
smallImageKey:
'https://raw.githubusercontent.com/Monochromish/Last.fm-Discord-Rich-Presence/main/assets/play.gif',
smallImageText: data.scrobbleStatus,
instance: false
})
.then(() => {
console.log('Updating Rich Presence');
});
}
//Running the update status every 30 seconds
setInterval(function () {
updateStatus();
}, 30000);
//Logging in RPC
client.login({ clientId: clientid }).then(function () {
updateStatus();
});
}
});
function createSystemTray() {
appIcon = new Tray(iconPath);
var contextMenu = Menu.buildFromTemplate([
{
label: 'Last.fm Rich Presence',
click: function () {
shell.openPath('https://github.com/Monochromish/Last.fm-Discord-Rich-Presence');
}
},
{ label: 'Quit', type: 'normal', click: () => app.quit() }
]);
appIcon.setToolTip('Last.fm Rich Presence');
appIcon.setContextMenu(contextMenu);
}
app.on('ready', function () {
createWindow();
createSystemTray();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
app.on('new-window', function (e, url) {
e.preventDefault();
require('electron').shell.openExternal(url);
});
server.listen(3000, () => {
console.log('Listening at http://localhost:3000');
});