-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (56 loc) Β· 2.11 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
const SpotifyToYoutube = require('spotify-to-youtube')
const spotify = require('./spotify')
const youtube = require('./youtube')
const config = require('./config')
const { state, saveState } = require('./state')
const log = require('./log')
const spotifyToYoutube = SpotifyToYoutube(spotify.api)
async function isNewDay () {
const localHours = new Date(new Date().toLocaleString('en-US', { timeZone: 'America/Montreal' })).getHours()
// In case there's some party msuic after midnight?
if (localHours <= 5) {
return false
}
const date = new Date().toLocaleString('sv', { timeZone: 'America/Montreal' }).split(' ').shift()
if (date === state.date) {
return false
}
state.date = date
await saveState()
return true
}
async function main () {
await spotify.authenticateApi()
const current = await spotify.getCurrentlyPlaying()
const track = current.track.uri
log('Currently playing', current.track.artist.name, '-', current.track.name, `(${track})`)
if (track === state.lastAddedTrack) {
return
}
// Clear playlist on the first played track of the day.
if (await isNewDay()) {
log('New day, clearing Spotify playlist')
await spotify.clearPlaylist()
if (config.youtube) {
log('Clearing YouTube playlist')
await youtube.clearPlaylist()
}
}
log('Adding to Spotify playlist')
await spotify.addTrackToPlaylist(track)
state.lastAddedTrack = track
await saveState()
if (config.youtube) {
log('Adding to YouTube playlist')
const youtubeTrack = await spotifyToYoutube(track, { raw: true })
let youtubeArtist = 'artist' in youtubeTrack ? ('name' in youtubeTrack.artist ? youtubeTrack.artist.name : youtubeTrack.artist) : youtubeTrack.author
youtubeArtist = Array.isArray(youtubeArtist) ? youtubeArtist : youtubeArtist[0]
log('[spotify-to-youtube]', 'Matched', current.track.artist.name, '-', current.track.name, `(${track}) with`, youtubeArtist, '-', youtubeTrack.name, `(${youtubeTrack.videoId})`)
await youtube.addTrackToPlaylist(youtubeTrack.videoId)
}
}
main()
.catch(err => {
console.error(err.stack || err)
process.exit(1)
})