This is a simple tutorial of how to use Github Action and Spotify Web API to display & update spotify stats on your github readme.
Check out my github profile to see what it looks like.
- having Spotify account
- go to https://developer.spotify.com/documentation/web-api and create an app
website
: your github, namelyhttps://github.com/<user_name>
redirect_uri
:http://localhost:8888/callback
APIs uesd
: selectWeb API
- install node.js on your computer
- download
app.js
andpublic/index.html
to your working folder - run
npm install express axios cors cookie-parser
- replace these variable in
app.js
var client_id = ''; // Your clientId
var client_secret = ''; // Your client secret
var redirect_uri = 'http://localhost:8888/callback'; // Your redirect uri
var scope = 'user-top-read playlist-read-private'; // Your scope
- run
node app.js
- go to http://localhost:8888 and simply follow the instruct,
then copy&save yourrefresh_token
- Set up a new github action
- copy
workflows/main.yml
andworkflows/get_spotify.js
to your github action's folder (.github/workflows/
) - replace these variable in
get_spotify.js
var client_id = ''; // your client_id
var client_secret = ''; // your client secret
var refresh_token = ''; // your refresh token
Warning
Using this method, your spotify api keys will be place in get_spotify.js
,
and unfortunately, this file can be access by others (I haven't come up with a better way to hide the keys).
Thus it's better to only use read-only scopes, to prevent others modify your spotify contents.
Important
This config will get your
top 5 artists and top 10 tracks on spotify for last 4 weeks
and update once a week
Tip
Get any data you want by using fetchSpotifyStats(url)
Go to Spotify API document to know how to configure the url
Don't forget to do step 1 (Set up and Get token) again and modify the scope
add below snippet into your README.md
<table>
<tr>
<td align="center"><strong>Top Artists</strong></td>
<td align="center"><strong>Top Tracks</strong></td>
</tr>
<tr>
<td align="center" id="top-artist"></td>
<td id="top-track"></td>
</tr>
</table>
get_spotify.js
will replace the content of <td align="center" id="top-artist"></td>
with an unordered list of top 5 artists, and replace the content of <td id="top-track"></td>
with an ordered list of top 10 tracks.
Tip
You can check out and modify get_spotify.js
to change the format
Warning
According to Spotify Design Guidelines, you should put Spotify's logo when utilize it's content.