-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
141 lines (118 loc) · 3.72 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
require('dotenv').config()
const SpotifyWebApi = require('spotify-web-api-node');
const { Octokit } = require("@octokit/rest");
const {
CLIENT_ID: client_id,
CLIENT_SECRET: client_secret,
REFRESH_TOKEN: refresh_token,
TYPE: type,
GIST_ID: gistId,
GH_TOKEN: githubToken,
TIME_RANGE: time_range
} = process.env;
const octokit = new Octokit({
auth: `token ${githubToken}`
});
var spotifyApi = new SpotifyWebApi({
clientId: client_id,
clientSecret: client_secret,
refreshToken: refresh_token
});
function truncate(str, n){
return (str.length > n) ? str.substr(0, n-1) + '…' : str;
};
async function updateGist(lines, des) {
let gist;
try {
gist = await octokit.gists.get({ gist_id: gistId });
} catch (error) {
console.error(`Unable to get gist\n${error}`);
}
const filename = Object.keys(gist.data.files)[0];
try {
await octokit.gists.update({
gist_id: gistId,
description: `🎧 Spotify | ${des}`,
files: {
[filename]: {
content: lines
}
}
});
} catch (error) {
console.error(`Unable to update gist\n${error}`);
}
}
async function getTopTracks() {
try {
var topTracks = await spotifyApi.getMyTopTracks({ time_range: time_range, limit: 5 })
var tracks = topTracks.body.items.map((track) => ({
artist: track.artists.map((_artist) => _artist.name)[0],
title: track.name
}));
var lines = [];
tracks.forEach(track => {
lines.push(` ▶ ${truncate(track.title + " ", 35).padEnd(35, '.')} 🎵 ${truncate(track.artist + " ", 16)}`)
})
return lines.join("\n");
} catch (error) {
console.log('Something went wrong!', error);
}
}
async function getTopArtists() {
try {
var topArtists = await spotifyApi.getMyTopArtists({ time_range: time_range, limit: 5 })
var artists = topArtists.body.items.map((artist) => ({
artist: artist.name,
genres: artist.genres.slice(0, 2)
}));
var lines = [];
artists.forEach(artist => {
lines.push(` ▶ ${truncate(artist.artist + " ", 15).padEnd(15, '.')} 💽 ${truncate(artist.genres.join(", ") + " ", 40)}`)
})
return lines.join("\n");
} catch (error) {
console.log('Something went wrong!', error);
}
}
async function getRecentlyPlayed() {
try {
var recentlyPlayed = await spotifyApi.getMyRecentlyPlayedTracks({ limit: 5 })
var tracks = recentlyPlayed.body.items.map((play) => ({
artist: play.track.artists.map((_artist) => _artist.name)[0],
title: play.track.name
}));
var lines = [];
tracks.forEach(track => {
lines.push(` ▶ ${truncate(track.title + " ", 35).padEnd(35, '.')} 🎵 ${truncate(track.artist + " ", 16)}`)
})
return lines.join("\n");
} catch (error) {
console.log('Something went wrong!', error);
}
}
async function main() {
try {
var accessToken = await spotifyApi.refreshAccessToken()
spotifyApi.setAccessToken(accessToken.body['access_token']);
var res;
var des;
if(type === 'recently_played') {
res = await getRecentlyPlayed()
des = "Recently Played"
} else if (type === 'top_tracks') {
res = await getTopTracks()
des = "My Top Tracks"
} else {
res = await getTopArtists()
des = "My Top Artists"
}
console.log(res);
await updateGist(res, des)
} catch (error) {
console.log('Something went wrong!', error);
}
}
(async () => {
await main();
})();