forked from izayl/spotify-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (67 loc) · 1.73 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
require('dotenv').config()
const eaw = require('eastasianwidth')
const { Octokit } = require('@octokit/rest')
const { getTopTracks } = require('./spotify')
const { GH_TOKEN: github_token, GIST_ID: gist_id } = process.env
const octo = new Octokit({
auth: `token ${github_token}`,
})
async function main() {
const json = await getTopTracks()
await updateTopTracks(json)
}
async function updateTopTracks(json) {
let gist
try {
gist = await octo.gists.get({
gist_id,
})
} catch (error) {
console.error(
`spotify-box ran into an issue for getting your gist:\n${error}`
)
}
const tracks = json.items.map(item => ({
name: item.name,
artist: item.artists.map(artist => artist.name.trim()).join(' & '),
}))
if (!tracks.length) return
const lines = []
for (let index = 0; index < Math.min(tracks.length, 10); index++) {
let { name, artist } = tracks[index]
name = truncate(name, 25)
artist = truncate(artist, 19)
const line = [
name.padEnd(34 + name.length - eaw.length(name)),
artist.padStart(20 + artist.length - eaw.length(artist)),
]
lines.push(line.join(''))
}
try {
const filename = Object.keys(gist.data.files)[0]
await octo.gists.update({
gist_id,
files: {
[filename]: {
filename: '🎵 My Spotify Top Track',
content: lines.join('\n'),
},
},
})
} catch (error) {
console.error(
`spotify-box ran into an issue for updating your gist:\n${error}`
)
}
}
function truncate(str, len) {
// string longer than `len`
for (let i = len - 2; i >= 0; i--) {
if (eaw.length(str) <= len) break
str = str.substring(0, i)
}
return str.trim()
}
;(async () => {
await main()
})()