0
0
mirror of https://github.com/yude-jp/yude.jp synced 2024-12-22 12:10:11 +09:00
This commit is contained in:
yude 2021-12-11 06:58:32 +09:00
parent 49bd87b276
commit 8513ee795f
Signed by: yude
GPG Key ID: EB0FE5D925C4A968
3 changed files with 1 additions and 65 deletions

View File

@ -1,60 +0,0 @@
import querystring from 'querystring';
const {
SPOTIFY_CLIENT_ID: client_id,
SPOTIFY_CLIENT_SECRET: client_secret,
SPOTIFY_REFRESH_TOKEN: refresh_token,
} = process.env;
const basic = Buffer.from(`${client_id}:${client_secret}`).toString('base64');
const NOW_PLAYING_ENDPOINT = `https://api.spotify.com/v1/me/player/currently-playing`;
const TOKEN_ENDPOINT = `https://accounts.spotify.com/api/token`;
const getAccessToken = async () => {
const response = await fetch(TOKEN_ENDPOINT, {
method: 'POST',
headers: {
Authorization: `Basic ${basic}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: querystring.stringify({
grant_type: 'refresh_token',
refresh_token,
}),
});
return response.json();
};
export const getNowPlaying = async () => {
const { access_token } = await getAccessToken();
return fetch(NOW_PLAYING_ENDPOINT, {
headers: {
Authorization: `Bearer ${access_token}`,
},
});
};
const Spotify = async (_, res) => {
const response = await getNowPlaying();
if (response.status === 204 || response.status > 400) {
return res.status(200).json({ isPlaying: false });
}
const song = await response.json();
const isPlaying = song.is_playing;
const title = song.item.name;
const artist = song.item.artists.map((_artist) => _artist.name).join(', ');
const album = song.item.album.name;
return res.status(200).json({
album,
artist,
isPlaying,
title,
});
};
export default Spotify

View File

@ -11,7 +11,7 @@ function App () {
useEffect(() => {
const fetchData = async () => {
const result = await axios(
'/api/Spotify',
'https://vercel-spotify-api.vercel.app/api/Spotify',
);
setData(result.data);
};

View File

@ -25,8 +25,6 @@ import PublicKeys from './components/Profile/PublicKeys'
import Button from './components/Profile/Button'
import Contact from './components/Profile/Contact'
import NintendoSW from "./components/Profile/NintendoSW"
import FitbitSleep from "./components/Fitbit/Sleep"
import FitbitHeartrate from "./components/Fitbit/Heartrate"
// next-seo
import { NextSeo } from 'next-seo';
@ -63,8 +61,6 @@ export default function Profile(props) {
<div>
<DiscordPlaying />
<Spotify />
{/* <FitbitSleep />
<FitbitHeartrate /> */}
</div>
</div>