-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
164 lines (141 loc) · 4 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const { Octokit } = require("@octokit/rest");
const fetch = require("node-fetch");
const fs = require("fs");
const {
GIST_ID: gistId,
GITHUB_TOKEN: githubToken,
SPOTIFY_CLIENT_SECRET: spotifyClientSecret,
SPOTIFY_CLIENT_ID: spotifyClientId
} = process.env;
let spotifyCode = process.env.SPOTIFY_CODE;
const API_BASE = "https://api.spotify.com/v1";
const AUTH_CACHE_FILE = "spotify-auth.json";
const octokit = new Octokit({
auth: `token ${githubToken}`
});
async function main() {
const spotifyData = await getSpotifyData();
await updateGist(spotifyData);
}
/**
* Updates cached spotify authentication tokens when necessary (1 hr expiriy)
*/
async function getSpotifyToken() {
// default env vars go in here (temp cache)
let cache = {};
let formData = {
grant_type: "authorization_code",
code: spotifyCode,
redirect_uri: "http://localhost/"
};
// try to read cache from disk if already exists
try {
const jsonStr = fs.readFileSync(AUTH_CACHE_FILE);
const c = JSON.parse(jsonStr);
Object.keys(c).forEach(key => {
cache[key] = c[key];
});
} catch (error) {
console.log(error);
}
if (cache.spotifyRefreshToken) {
console.debug(`ref: ${cache.spotifyRefreshToken.substring(0, 6)}`);
formData = {
grant_type: "refresh_token",
refresh_token: cache.spotifyRefreshToken
};
}
// get new tokens
const data = await fetch("https://accounts.spotify.com/api/token", {
method: "post",
body: encodeFormData(formData),
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization:
"Basic " +
new Buffer.from(spotifyClientId + ":" + spotifyClientSecret).toString(
"base64"
)
}
})
.then(data => data.json())
.catch(error => console.debug(error));
console.debug(data);
cache.spotifyAccessToken = data.access_token;
if (data.refresh_token) {
cache.spotifyRefreshToken = data.refresh_token;
console.debug(`ref: ${cache.spotifyRefreshToken.substring(0, 6)}`);
}
console.debug(`acc: ${cache.spotifyAccessToken.substring(0, 6)}`);
// save to disk
fs.writeFileSync(AUTH_CACHE_FILE, JSON.stringify(cache));
return cache.spotifyAccessToken;
}
const encodeFormData = data => {
return Object.keys(data)
.map(key => encodeURIComponent(key) + "=" + encodeURIComponent(data[key]))
.join("&");
};
/**
* Fetches your data from the spotify API
*/
async function getSpotifyData() {
// recent 20 played data (add other endpoints for more info as needed)
const recentlyPlayedData = await fetch(
`${API_BASE}/me/player/recently-played?limit=20`,
{
method: "get",
headers: {
Authorization: "Bearer " + (await getSpotifyToken())
}
}
)
.then(data => data.json())
.catch(error => console.debug(error));
return recentlyPlayedData;
}
async function updateGist(data) {
let gist;
let songs = [];
let lines = "";
try {
gist = await octokit.gists.get({ gist_id: gistId });
} catch (error) {
console.error(`Unable to get gist\n${error}`);
throw error;
}
data.items.forEach(item =>
songs.push(`${item.track.name} - ${item.track.artists[0].name}`)
);
console.debug(songs);
const songListenCount = songs.reduce((prev, curr) => {
prev[curr] = (prev[curr] || 0) + 1;
return prev;
}, {});
const songList = Object.keys(songListenCount);
const songOnRepeat = Object.keys(songListenCount).reduce((a, b) =>
songListenCount[a] > songListenCount[b] ? a : b
);
lines += `🎧 On Repeat Recently: ${
songListenCount[songOnRepeat] > 2 ? songOnRepeat : "Nothing..."
}\n\n`;
lines += songList.join("\n");
try {
const filename = Object.keys(gist.data.files)[0];
await octokit.gists.update({
gist_id: gistId,
files: {
[filename]: {
filename: `🎼 Kan's Spotify Activity`,
content: lines
}
}
});
} catch (error) {
console.error(`Unable to update gist\n${error}`);
throw error;
}
}
(async () => {
await main();
})();