Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter explicit spotify #76

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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