This repository has been archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·95 lines (73 loc) · 1.91 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
#!/usr/bin/env node
const ora = require('ora')
const chalk = require('chalk')
const getLyrics = require('./lyrics')
const renderScreen = require('./screen')
const {
getTrack,
metadataChangeListener
} = require('./spotify-dbus')
const spinner = ora('Starting...').start()
let mainScreen
let lastTrackID
const getCurrentSong = () => {
return new Promise(async (resolve) => {
try {
const track = await getTrack()
spinner.succeed()
resolve({
trackID: track.trackID,
artist: track.artist,
title: track.title
})
} catch (exception) {
spinner.fail(exception.message)
process.exit(1)
}
})
}
const fetchLyrics = (currentArtist, currentTitle) => {
return new Promise(async (resolve) => {
try {
const currentTrack = chalk.bold(`${currentArtist} - ${currentTitle}`)
spinner.text = `Current song: ${currentTrack}`
spinner.succeed()
spinner.text = 'Searching lyrics...'
spinner.start()
const {
artist,
title,
lyrics
} = await getLyrics(currentArtist, currentTitle)
spinner.succeed()
resolve({ artist, title, lyrics })
} catch (exception) {
spinner.fail(exception.message)
process.exit(1)
}
})
}
const drawScreen = (artist, title, lyrics) => {
mainScreen = renderScreen(artist, title, lyrics)
}
(async () => {
const currentSong = await getCurrentSong()
lastTrackID = currentSong.trackID
const { artist, title, lyrics } = await fetchLyrics(
currentSong.artist,
currentSong.title
)
drawScreen(artist, title, lyrics)
})()
// ALPHA: Auto-reload lyrics
metadataChangeListener(async (_, currentSong) => {
if (lastTrackID === currentSong.trackID || !mainScreen) {
return
}
mainScreen.realloc()
const { artist, title, lyrics } = await fetchLyrics(
currentSong.artist,
currentSong.title
)
drawScreen(artist, title, lyrics)
})