optimize music widget

This commit is contained in:
zoop 2026-09-13 08:33:01 -04:00
parent 1bd2d9edb1
commit f88dc71a7b
No known key found for this signature in database
GPG Key ID: 15C0D336C9E08F3F
4 changed files with 6 additions and 50 deletions

View File

@ -84,11 +84,6 @@ import { Icon } from "astro-icon/components";
<ul class="stat-list" id="lfm-artists"></ul> <ul class="stat-list" id="lfm-artists"></ul>
</div> </div>
<div class="stat-block" id="lfm-recent-block" hidden>
<h2 class="label">recently played</h2>
<ul class="stat-list stat-list--plain" id="lfm-recent"></ul>
</div>
<p class="wall-foot"> <p class="wall-foot">
scrobbling <a scrobbling <a
href="https://www.last.fm/user/zoop-zach" href="https://www.last.fm/user/zoop-zach"
@ -338,13 +333,6 @@ import { Icon } from "astro-icon/components";
letter-spacing: 0.02em; letter-spacing: 0.02em;
color: var(--faint); color: var(--faint);
} }
.stat-list--plain li::before {
display: none;
}
.stat-list--plain .stat-list__v {
color: var(--faint);
}
.np { .np {
display: flex; display: flex;
gap: 0.8rem; gap: 0.8rem;

View File

@ -53,7 +53,6 @@ export const GET: APIRoute = async () => {
nowPlaying: null as unknown, nowPlaying: null as unknown,
top: [] as unknown[], top: [] as unknown[],
topArtists: [] as unknown[], topArtists: [] as unknown[],
history: [] as unknown[],
weekPlays: 0, weekPlays: 0,
allTimePlays: 0, allTimePlays: 0,
estMinutes: 0, estMinutes: 0,
@ -70,16 +69,15 @@ export const GET: APIRoute = async () => {
try { try {
const [recent, top, artists, week, info] = await Promise.all([ const [recent, top, artists, week, info] = await Promise.all([
call("method=user.getrecenttracks&limit=8&extended=1"), call("method=user.getrecenttracks&limit=1&extended=1"),
call("method=user.gettoptracks&period=7day&limit=8"), call("method=user.gettoptracks&period=7day&limit=5"),
call("method=user.gettopartists&period=7day&limit=6"), call("method=user.gettopartists&period=7day&limit=5"),
call(`method=user.getrecenttracks&limit=1&from=${from}`), call(`method=user.getrecenttracks&limit=1&from=${from}`),
call("method=user.getinfo"), call("method=user.getinfo"),
]); ]);
const rtRaw = (recent as any)?.recenttracks?.track; const rtRaw = (recent as any)?.recenttracks?.track;
const rtArr: any[] = Array.isArray(rtRaw) ? rtRaw : rtRaw ? [rtRaw] : []; const rt = Array.isArray(rtRaw) ? rtRaw[0] : rtRaw;
const rt = rtArr[0];
const nowPlaying = rt const nowPlaying = rt
? { ? {
title: rt.name ?? "", title: rt.name ?? "",
@ -92,16 +90,6 @@ export const GET: APIRoute = async () => {
} }
: null; : null;
const history = rtArr
.filter((t: any) => t["@attr"]?.nowplaying !== "true" && t.date?.uts)
.slice(0, 6)
.map((t: any) => ({
title: t.name ?? "",
artist: t.artist?.name ?? t.artist?.["#text"] ?? "",
playedAt: n(t.date.uts) * 1000,
}))
.filter((t: any) => t.title);
const topTracks = ((top as any)?.toptracks?.track ?? []) const topTracks = ((top as any)?.toptracks?.track ?? [])
.map((t: any) => ({ .map((t: any) => ({
title: t.name ?? "", title: t.name ?? "",
@ -137,7 +125,6 @@ export const GET: APIRoute = async () => {
nowPlaying, nowPlaying,
top: topTracks, top: topTracks,
topArtists, topArtists,
history,
weekPlays, weekPlays,
allTimePlays, allTimePlays,
estMinutes, estMinutes,

View File

@ -11,7 +11,7 @@ import { site, elsewhere } from "../data/site";
<Base> <Base>
<script is:inline> <script is:inline>
window.__mm = {}; window.__mm = {};
for (const k of ["lastfm", "reading", "stats"]) { for (const k of ["lastfm", "reading", "stats"]) {
window.__mm[k] = fetch("/api/" + k + ".json", { headers: { accept: "application/json" } }) window.__mm[k] = fetch("/api/" + k + ".json", { headers: { accept: "application/json" } })
.then(function (r) { .then(function (r) {

View File

@ -10,11 +10,6 @@ interface ArtistRow {
plays: number; plays: number;
url: string; url: string;
} }
interface HistRow {
title: string;
artist: string;
playedAt: number;
}
interface NP { interface NP {
title: string; title: string;
artist: string; artist: string;
@ -29,7 +24,6 @@ interface Data {
nowPlaying: NP | null; nowPlaying: NP | null;
top: Track[]; top: Track[];
topArtists: ArtistRow[]; topArtists: ArtistRow[];
history: HistRow[];
weekPlays: number; weekPlays: number;
allTimePlays: number; allTimePlays: number;
estMinutes: number; estMinutes: number;
@ -142,12 +136,6 @@ function renderStats(d: Data): void {
w: Math.round((a.plays / artMax) * 100), w: Math.round((a.plays / artMax) * 100),
})), })),
); );
fillList(
"lfm-recent",
"lfm-recent-block",
d.history.map((h) => ({ k: h.title, sub: h.artist, v: ago(h.playedAt) })),
);
} }
function mockData(): Data { function mockData(): Data {
@ -180,13 +168,7 @@ function mockData(): Data {
{ name: "Lil Uzi Vert", plays: 18, url: "" }, { name: "Lil Uzi Vert", plays: 18, url: "" },
{ name: "Kehlani", plays: 12, url: "" }, { name: "Kehlani", plays: 12, url: "" },
{ name: "The Weeknd", plays: 7, url: "" }, { name: "The Weeknd", plays: 7, url: "" },
], { name: "Kanye West", plays: 5, url: "" },
history: [
{ title: "20 Min", artist: "Lil Uzi Vert", playedAt: Date.now() - 6 * 60_000 },
{ title: "After Party", artist: "Don Toliver", playedAt: Date.now() - 12 * 60_000 },
{ title: "Gemstones", artist: "Don Toliver", playedAt: Date.now() - 40 * 60_000 },
{ title: "NIGHTS LIKE THIS", artist: "Kehlani", playedAt: Date.now() - 3 * 3_600_000 },
{ title: "HEARTLESS", artist: "The Weeknd", playedAt: Date.now() - 26 * 3_600_000 },
], ],
weekPlays: 128, weekPlays: 128,
allTimePlays: 4210, allTimePlays: 4210,
@ -216,7 +198,6 @@ async function load(): Promise<MusicState> {
const top = Array.isArray(data.top) ? data.top : []; const top = Array.isArray(data.top) ? data.top : [];
data.top = top; data.top = top;
data.topArtists = Array.isArray(data.topArtists) ? data.topArtists : []; data.topArtists = Array.isArray(data.topArtists) ? data.topArtists : [];
data.history = Array.isArray(data.history) ? data.history : [];
data.lib = data.lib ?? { artists: 0, albums: 0, tracks: 0 }; data.lib = data.lib ?? { artists: 0, albums: 0, tracks: 0 };
if (np) renderNowPlaying(np); if (np) renderNowPlaying(np);
renderStats(data); renderStats(data);