-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cjs
More file actions
91 lines (72 loc) · 2.46 KB
/
main.cjs
File metadata and controls
91 lines (72 loc) · 2.46 KB
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
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const fetch = require('node-fetch');
const { getLyrics } = require('./lyrics.cjs');
require('dotenv').config();
const { screen } = require('electron');
let mainWindow;
let accessToken = '';
async function updateAccessToken() {
try {
const res = await fetch('http://127.0.0.1:8888/token');
const data = await res.json();
accessToken = data.access_token;
console.log('Access token updated.');
} catch (err) {
console.error('Failed to update access token:', err);
}
}
// run once at startup, then every 55 minutes
updateAccessToken();
setInterval(updateAccessToken, 55 * 60 * 1000);
function createWindow() {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
mainWindow = new BrowserWindow({
width: 300,
height: 550, // more height to fit track, artist, and album art
x: width - 320, // near bottom-right
y: height - 580,
frame: false,
transparent: true,
alwaysOnTop: true,
resizable: false,
hasShadow: false,
skipTaskbar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
});
mainWindow.setIgnoreMouseEvents(true, { forward: true });
mainWindow.loadFile('index.html');
// remove scrollbar artifacts
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.insertCSS('body { overflow: hidden !important; }');
});
mainWindow.setAlwaysOnTop(true, 'floating');
}
app.whenReady().then(createWindow);
// every 5 seconds, get the current Spotify track
setInterval(async () => {
if (!accessToken || !mainWindow) return;
try {
const res = await fetch('https://api.spotify.com/v1/me/player/currently-playing', {
headers: { Authorization: `Bearer ${accessToken}` }
});
if (res.status === 204) return; // no track playing
const data = await res.json();
// const lyrics = await getLyrics(data.item?.name, data.item?.artists?.[0]?.name);
// console.log(lyrics);
if (mainWindow && mainWindow.webContents) {
const lyrics = await getLyrics(data.item?.name, data.item?.artists?.[0]?.name);
mainWindow.webContents.send('update-track', {
name: data.item?.name,
artist: data.item?.artists?.map(a => a.name).join(', '),
albumArt: data.item?.album?.images[0]?.url,
lyrics
});
//console.log(lyrics);
}
} catch (err) {
console.error(err);
}
}, 5000);