Skip to content

Commit

Permalink
Add filter explicit spotify (#76)
Browse files Browse the repository at this point in the history
* Refactor Spotify data storage to exclude explicit content

* Refactor Spotify API authentication and response handling

* Update font file reference in base layout
  • Loading branch information
afnizarnur committed May 5, 2024
1 parent 1740aed commit 2876ec3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
Binary file removed src/assets/fonts/Mona-Sans.woff2
Binary file not shown.
10 changes: 6 additions & 4 deletions src/assets/scripts/modules/spotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ class SpotifyFetcher {
const url = data.url
this.updateParagraph(songTitle, artistName, url)

localStorage.setItem(
"spotifyData",
JSON.stringify({ songTitle, artistName, url })
)
if (songTitle !== "Explicit Content") {
localStorage.setItem(
"spotifyData",
JSON.stringify({ songTitle, artistName, url })
)
}
})

.catch((error) => {
Expand Down
53 changes: 32 additions & 21 deletions src/functions/spotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ const axios = require("axios")
const dotenv = require("dotenv")
dotenv.config()

exports.handler = async (event, context) => {
const refreshToken = process.env.SPOTIFY_REFRESH_TOKEN
const auth = Buffer.from(
`${process.env.SPOTIFY_CLIENT_ID}:${process.env.SPOTIFY_CLIENT_SECRET}`
).toString("base64")
const tokenEndpoint = "https://accounts.spotify.com/api/token"
const playerEndpoint =
"https://api.spotify.com/v1/me/player/recently-played"

const options = {
method: "POST",
headers: {
Authorization: `Basic ${auth}`,
"Content-Type": "application/x-www-form-urlencoded"
},
data: `grant_type=refresh_token&refresh_token=${refreshToken}&redirect_uri=${encodeURI(
process.env.URL,
+"/.netlify/functions/callback"
)}`
}
let nonExplicitSongData = null

exports.handler = async (event, context) => {
try {
const refreshToken = process.env.SPOTIFY_REFRESH_TOKEN
const auth = Buffer.from(
`${process.env.SPOTIFY_CLIENT_ID}:${process.env.SPOTIFY_CLIENT_SECRET}`
).toString("base64")
const tokenEndpoint = "https://accounts.spotify.com/api/token"
const playerEndpoint =
"https://api.spotify.com/v1/me/player/recently-played"

const options = {
method: "POST",
headers: {
Authorization: `Basic ${auth}`,
"Content-Type": "application/x-www-form-urlencoded"
},
data: `grant_type=refresh_token&refresh_token=${refreshToken}&redirect_uri=${encodeURI(
process.env.URL + "/.netlify/functions/callback"
)}`
}

const {
data: { access_token: accessToken }
} = await axios(tokenEndpoint, options)
Expand All @@ -34,6 +35,14 @@ exports.handler = async (event, context) => {
}
})

const isExplicit = response.data.items[0].track.explicit

if (isExplicit && !nonExplicitSongData) {
return {
statusCode: 204
}
}

const {
artists: artistsArray,
name,
Expand All @@ -45,9 +54,11 @@ exports.handler = async (event, context) => {
}))
const url = urls.spotify

nonExplicitSongData = { artists, name, url }

return {
statusCode: 200,
body: JSON.stringify({ artists, name, url })
body: JSON.stringify(nonExplicitSongData)
}
} catch (err) {
if (process.env.ELEVENTY_ENV === "development") {
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/base.njk
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{% include "meta.njk" %}
<link
rel="preload"
href="{{ '/assets/fonts/Mona-Sans.woff2' | url }}"
href="{{ '/assets/fonts/Aksen-Condensed-SemiBold.woff2' | url }}"
as="font"
type="font/woff2"
crossorigin
Expand Down

0 comments on commit 2876ec3

Please sign in to comment.