Skip to content

Commit

Permalink
LastFM - Skipped scrobbles messages & settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Jun 19, 2024
1 parent e75d5bc commit 5cca858
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
20 changes: 20 additions & 0 deletions plugins/LastFM/src/Settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { html } from "@neptune/voby";
// @ts-expect-error Remove this when types are available
import { storage } from "@plugin";

storage.displaySkippedScrobbles ??= true;
export const Settings = () => {
setTimeout(() => {
const displaySkippedScrobbles = document.getElementById("displaySkippedScrobbles");
if (displaySkippedScrobbles instanceof HTMLInputElement && displaySkippedScrobbles.checked !== storage.displaySkippedScrobbles) displaySkippedScrobbles!.checked = storage.displaySkippedScrobbles;
});
const onChange = (key: string) => (e: { target: { checked: boolean } }) => (storage[key] = e.target.checked);
return html`<div class="settings-section">
<h3 class="settings-header">Display Skipped Scrobbles</h3>
<p class="settings-explainer">Show a temporary message whenever a song isnt scrobbled.</p>
<label class="switch">
<input type="checkbox" id="displaySkippedScrobbles" onChange=${onChange("displaySkippedScrobbles")} />
<span class="slider" />
</label>
</div>`;
};
16 changes: 14 additions & 2 deletions plugins/LastFM/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { rejectNotOk, requestStream } from "../../../lib/fetchy";
import { LastFM, ScrobbleOpts } from "./LastFM";

import type { Album, MediaItem, TrackItem } from "neptune-types/tidal";
import { messageError } from "../../../lib/messageLogging";
import { messageError, messageInfo } from "../../../lib/messageLogging";
import { interceptPromise } from "../../../lib/interceptPromise";

import { toBuffer } from "../../SongDownloader/src/lib/toBuffer";
Expand All @@ -16,6 +16,11 @@ import type { ReleaseData } from "./types/ReleaseData";
import { fullTitle } from "../../../lib/fullTitle";
import { Recording } from "./types/Recording";

export { Settings } from "./Settings";

// @ts-expect-error Remove this when types are available
import { storage } from "@plugin";

let totalPlayTime = 0;
let lastPlayStart: number | null = null;

Expand Down Expand Up @@ -51,12 +56,19 @@ const intercepters = [
intercept("playbackControls/MEDIA_PRODUCT_TRANSITION", ([{ playbackContext }]) => {
if (currentTrack !== undefined) {
if (lastPlayStart !== null) totalPlayTime += Date.now() - lastPlayStart;
if (totalPlayTime >= MIN_SCROBBLE_DURATION || totalPlayTime >= +currentTrack.playbackContext.actualDuration * MIN_SCROBBLE_PERCENTAGE * 1000) {
const longerThan4min = totalPlayTime >= MIN_SCROBBLE_DURATION;
const minPlayTime = +currentTrack.playbackContext.actualDuration * MIN_SCROBBLE_PERCENTAGE * 1000;
const moreThan50Percent = totalPlayTime >= minPlayTime;
if (longerThan4min || moreThan50Percent) {
const scrobbleParams = getTrackParams(currentTrack);
console.log("[last.fm] scrobbling", scrobbleParams);
LastFM.scrobble(scrobbleParams)
.catch((err) => messageError(`last.fm - Failed to scrobble! ${err}`))
.then((res) => console.log("[last.fm] scrobbled", res));
} else {
const noScrobbleMessage = `skipped scrobbling ${currentTrack.trackItem.title} - Listened for ${(totalPlayTime / 1000).toFixed(0)}s, need ${(minPlayTime / 1000).toFixed(0)}s`;
console.log(`[last.fm] ${noScrobbleMessage}`);
if (storage.displaySkippedScrobbles) messageInfo(`last.fm - ${noScrobbleMessage}`);
}
}

Expand Down

0 comments on commit 5cca858

Please sign in to comment.