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.

50 lines
1.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import { renderToString } from "react-dom/server";
  2. import { NowRequest, NowResponse } from "@vercel/node";
  3. import { decode } from "querystring";
  4. import { nowPlaying } from "../utils/spotify";
  5. import { Player } from "../components/NowPlaying";
  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. res.setHeader("Cache-Control", "no-cache");
  25. const {
  26. duration_ms: duration,
  27. name: track,
  28. } = item;
  29. const { images = [] } = item.album || {};
  30. const cover = images[images.length - 1]?.url;
  31. let coverImg = null;
  32. if (cover) {
  33. const buff = await(await fetch(cover)).arrayBuffer();
  34. coverImg = `data:image/jpeg;base64,${Buffer.from(buff).toString(
  35. "base64"
  36. )}`;
  37. }
  38. const artist = (item.artists || []).map(({ name }) => name).join(", ");
  39. const text = renderToString(
  40. Player({ cover: coverImg, artist, track, isPlaying, progress, duration })
  41. );
  42. return res.status(200).send(text);
  43. }