Skip to content

Commit

Permalink
fix spotify
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-parag committed Oct 18, 2021
1 parent 10bd9a2 commit f018265
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
9 changes: 1 addition & 8 deletions pages/api/podcast-playing.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ export default async (_, res) => {
);

return res.status(200).json({
isPlaying,
episodeTitle,
episodeDescription,
podcastName,
publisher,
podcastImgUrl,
podcastUrl,
explicit
podcast
});
};
24 changes: 24 additions & 0 deletions pages/api/spotify/player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getPlayer } from '@utils/spotify';

export default async (_, res) => {
const response = await getPlayer();

if (response.status === 204 || response.status > 400 || response.status === 500) {
return res.status(200).json({ isPlaying: false });
}

const playing = await response.json();

if(playing.currently_playing_type === "episode") {
return res.status(200).json({ playing });
}

res.setHeader(
'Cache-Control',
'public, s-maxage=60, stale-while-revalidate=30'
);

return res.status(200).json({
playing
});
};
14 changes: 14 additions & 0 deletions utils/spotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import querystring from 'querystring';
// https://leerob.io/blog/spotify-api-nextjs
// https://benwiz.com/blog/create-spotify-refresh-token/

// https://accounts.spotify.com/authorize?response_type=code&client_id=${CLIENT_ID}redirect_uri=http%3A%2F%2Flocalhost:3000&scope=user-read-currently-playing%20user-top-read


const client_id = process.env.SPOTIFY_CLIENT_ID
const client_secret = process.env.SPOTIFY_CLIENT_SECRET
const refresh_token = process.env.SPOTIFY_REFRESH_TOKEN
Expand All @@ -15,6 +18,7 @@ const TOP_TRACKS_ENDPOINT = `https://api.spotify.com/v1/me/top/tracks`;
const SHOWS_ENDPOINT = `https://api.spotify.com/v1/me/shows?limit=50`;
const TOKEN_ENDPOINT = `https://accounts.spotify.com/api/token`;
const PLAYLISTS_ENDPOINT = `https://api.spotify.com/v1/users/${process.env.SPOTIFY_USER_ID}/playlists`;
const PLAYBACK_ENDPOINT = `https://api.spotify.com/v1/me/player`

const basic = Buffer.from(`${client_id}:${client_secret}`).toString('base64');

Expand Down Expand Up @@ -102,4 +106,14 @@ export const getPlaylists = async () => {
Authorization: `Bearer ${access_token}`,
}
});
};

export const getPlayer = async () => {
const { access_token } = await getAccessToken();

return fetch(PLAYBACK_ENDPOINT, {
headers: {
Authorization: `Bearer ${access_token}`,
}
});
};

1 comment on commit f018265

@vercel
Copy link

@vercel vercel bot commented on f018265 Oct 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.