Dynamic realtime profile ReadMe linked with spotify
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.4 KiB

  1. import { NowRequest, NowResponse } from "@vercel/node";
  2. import { renderToString } from "react-dom/server";
  3. import { decode } from "querystring";
  4. import { Player } from "../components/NowPlaying";
  5. import { nowPlaying } from "../utils/spotify";
  6. export default async function (req: NowRequest, res: NowResponse) {
  7. const {
  8. item = {},
  9. is_playing: isPlaying = false,
  10. progress_ms: progress = 0,
  11. } = await nowPlaying();
  12. const params = decode(req.url.split("?")[1]) as any;
  13. if (params && typeof params.open !== "undefined") {
  14. if (item && item.external_urls) {
  15. res.writeHead(302, {
  16. Location: item.external_urls.spotify,
  17. });
  18. return res.end();
  19. }
  20. return res.status(200).end();
  21. }
  22. res.setHeader("Content-Type", "image/svg+xml");
  23. res.setHeader("Cache-Control", "s-maxage=1, stale-while-revalidate");
  24. const { duration_ms: duration, name: track } = item;
  25. const { images = [] } = item.album || {};
  26. const cover = images[images.length - 1]?.url;
  27. let coverImg = null;
  28. if (cover) {
  29. const buff = await (await fetch(cover)).arrayBuffer();
  30. coverImg = `data:image/jpeg;base64,${Buffer.from(buff).toString("base64")}`;
  31. }
  32. const artist = (item.artists || []).map(({ name }) => name).join(", ");
  33. const text = renderToString(
  34. Player({ cover: coverImg, artist, track, isPlaying, progress, duration })
  35. );
  36. return res.status(200).send(text);
  37. }