Skip to content

Commit

Permalink
[lastfm] improve activity reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
ioj4 authored and yellowsink committed Sep 26, 2024
1 parent 18cccad commit 7ab8846
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
6 changes: 0 additions & 6 deletions plugins/lastfm/cfg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,3 @@ export const LFM_API_KEY = "311958e99f6ee58518756f83720db787";

//export const ACTIVITY_TYPES = ["PLAYING", "STREAMING", "LISTENING", "COMPETING"] as const;
export const ACTIVITY_TYPE_LISTENING = 2;

// https://github.com/amsyarasyiq/letup/blob/41bad09/plugins/Last.fm/src/constants.ts#L9-L12
export const IGNORED_COVERS = [
"2a96cbd8b46e442fc41c2b86b821562f",
"c6f59c1e5e7240a4c0d427abd71f3dbb",
];
22 changes: 13 additions & 9 deletions plugins/lastfm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
DEFAULT_INTERVAL,
DEFAULT_NAME,
DISCORD_APP_ID,
IGNORED_COVERS,
LFM_API_KEY,
} from "./cfg";
import { getAsset } from "./assets";
Expand Down Expand Up @@ -41,7 +40,7 @@ interface Track {
nowPlaying: boolean;
}

const setPresence = async (name = "", activity?: Track) =>
const setPresence = async (name = "", activity?: Track, start?: number) =>
dispatcher.dispatch({
type: "LOCAL_ACTIVITY_UPDATE",
activity: activity
Expand All @@ -53,7 +52,7 @@ const setPresence = async (name = "", activity?: Track) =>
state: activity.artist,
application_id: DISCORD_APP_ID,
timestamps: store.stamp
? { start: Date.now() }
? { start }
: undefined,
assets: {
large_image:
Expand Down Expand Up @@ -81,13 +80,11 @@ const getScrobbleLastfm = async () => {
const lastTrack = (await res.json())?.recenttracks?.track?.[0];
if (!lastTrack) return;

const aart = lastTrack.image[3]["#text"];

return {
name: lastTrack.name,
artist: lastTrack.artist.name,
album: lastTrack.album["#text"],
albumArt: IGNORED_COVERS.includes(aart) ? undefined : aart,
albumArt: lastTrack.image[3]["#text"],
url: lastTrack.url,
//date: lastTrack.date?.["#text"] ?? "now",
nowPlaying: !!lastTrack["@attr"]?.nowplaying,
Expand Down Expand Up @@ -170,6 +167,7 @@ const getScrobbleListenbrainz = async () => {
};

let lastUrl: string;
let startTimestamp: number;

const updateStatus = async () => {
if (!store.user) return setPresence();
Expand All @@ -188,9 +186,15 @@ const updateStatus = async () => {
store.service === "lbz" ? getScrobbleListenbrainz : getScrobbleLastfm;

const lastTrack = await getFn();
if (!lastTrack?.nowPlaying) return setPresence();
if (!lastTrack?.nowPlaying) {
startTimestamp = null;
return setPresence();
}

if (lastTrack.url !== lastUrl || !startTimestamp) {
startTimestamp = Date.now();
};

if (lastTrack.url === lastUrl) return;
lastUrl = lastTrack.url;

let appName = store.appName || DEFAULT_NAME;
Expand All @@ -200,7 +204,7 @@ const updateStatus = async () => {
eval(`(c)=>{with(c){try{return ${code}}catch(e){return e}}}`)(lastTrack),
);

await setPresence(appName, lastTrack);
await setPresence(appName, lastTrack, startTimestamp);
};

let interval;
Expand Down

0 comments on commit 7ab8846

Please sign in to comment.