diff --git a/package.json b/package.json index f709e1b..f3bed1c 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "react-hot-toast": "^2.4.0", "react-icons": "^4.4.0", "sass": "^1.55.0", + "spotify-now-playing": "^0.1.3", "tailwindcss": "^3.1.8", "typescript": "4.8.4" } diff --git a/pages/api/spotify.ts b/pages/api/spotify.ts index d5f7960..69510ea 100644 --- a/pages/api/spotify.ts +++ b/pages/api/spotify.ts @@ -1,28 +1,22 @@ -import { NextApiRequest, NextApiResponse } from "next"; +import { NextApiRequest, NextApiResponse } from 'next'; +import { SpotifyService } from 'spotify-now-playing' export default async function (req: NextApiRequest, res: NextApiResponse) { - const { LASTFM_USERNAME, LASTFM_API } = process.env; + const { CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN } = process.env; + const spotify = new SpotifyService(CLIENT_ID!, CLIENT_SECRET!, REFRESH_TOKEN!) + const song = await spotify.getCurrentSong() - const { recenttracks: response } = await fetch( - `https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${LASTFM_USERNAME}&api_key=${LASTFM_API}&format=json&limit=1` - ).then((res) => res.json()); - - const { track } = response; - const { artist, name, url, image } = track[0]; - - let nowplaying = Boolean(track[0]["@attr"]?.nowplaying); - - if (nowplaying) { - return res.status(200).json({ - nowplaying, + if(!song.isPlaying) { + return res.status(200).json({ + nowplaying: false, + }); + } + res.status(200).json({ + nowplaying: true, song: { - artist: artist["#text"], - title: name, - url: url, - image: image[2]["#text"], - }, + artist: song.artist.name, + title: song.title, + url: song.externalUrl, + } }); - } - - return res.status(200).json({ nowplaying }); -} +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 623ae8b..869b320 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1016,6 +1016,11 @@ side-channel@^1.0.4: resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c" integrity sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw== +spotify-now-playing@^0.1.3: + version "0.1.3" + resolved "https://registry.yarnpkg.com/spotify-now-playing/-/spotify-now-playing-0.1.3.tgz#1ffad53bf6b1fb0c36165f21a40fdfc99dec2c0a" + integrity sha512-AB0Gp3eqUOO0WQHcapK7jxVQfED7uJQK9fIy5ALX51uBjCHSMLGtNKmr3Y70GoB6rEsIirr7zB7w1RliAp7TxQ== + string.prototype.trimend@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/string.prototype.trimend/-/string.prototype.trimend-1.0.5.tgz#914a65baaab25fbdd4ee291ca7dde57e869cb8d0"