refactor(listenbrainz): Simplify scrobble api calls and logging

This commit is contained in:
FoxxMD
2024-07-24 10:31:37 -04:00
parent b41d7f539f
commit 9c2bf05a24
2 changed files with 32 additions and 19 deletions
+30 -5
View File
@@ -1,6 +1,6 @@
import { stringSameness } from '@foxxmd/string-sameness';
import dayjs from "dayjs";
import request, { Request } from 'superagent';
import request, { Request, Response } from 'superagent';
import { PlayObject } from "../../../core/Atomic.js";
import { slice } from "../../../core/StringUtils.js";
import { combinePartsToString } from "../../utils.js";
@@ -127,7 +127,7 @@ export class ListenbrainzApiClient extends AbstractApiClient {
}
callApi = async <T>(req: Request, retries = 0): Promise<T> => {
callApi = async <T = Response>(req: Request, retries = 0): Promise<T> => {
const {
maxRequestRetries = 2,
retryMultiplier = DEFAULT_RETRY_MULTIPLIER
@@ -248,17 +248,27 @@ export class ListenbrainzApiClient extends AbstractApiClient {
}
}
submitListen = async (play: PlayObject) => {
submitListen = async (play: PlayObject, log: boolean = false) => {
try {
const listenPayload: SubmitPayload = {listen_type: 'single', payload: [ListenbrainzApiClient.playToListenPayload(play)]};
await this.callApi(request.post(`${this.url}1/submit-listens`).type('json').send(listenPayload));
if(log) {
this.logger.debug(`Submit Payload: ${JSON.stringify(listenPayload)}`);
}
// response consists of {"status": "ok"}
// so no useful information
// https://listenbrainz.readthedocs.io/en/latest/users/api-usage.html#submitting-listens
// TODO may we should make a call to recent-listens to get the parsed scrobble?
const resp = await this.callApi(request.post(`${this.url}1/submit-listens`).type('json').send(listenPayload));
if(log) {
this.logger.debug(`Submit Response: ${resp.text}`)
}
return listenPayload;
} catch (e) {
throw e;
}
}
static playToListenPayload = (play: PlayObject): ListenPayload => {
static playToListenPayload(play: PlayObject): ListenPayload {
const {
data: {
playDate,
@@ -581,6 +591,21 @@ export class ListenbrainzApiClient extends AbstractApiClient {
}
}
static submitToPlayObj(submitObj: SubmitPayload, playObj: PlayObject): PlayObject {
if (submitObj.payload.length > 0) {
const respPlay = {
...playObj,
};
respPlay.data = {
...playObj.data,
album: submitObj.payload[0].track_metadata?.release_name ?? playObj.data.album,
track: submitObj.payload[0].track_metadata?.track_name ?? playObj.data.album,
};
return respPlay;
}
return playObj;
}
static formatPlayObj(obj: any, options: FormatPlayObjectOptions): PlayObject {
return ListenbrainzApiClient.listenResponseToPlay(obj);
}
@@ -91,30 +91,18 @@ export default class ListenbrainzScrobbler extends AbstractScrobbleClient {
} = {}
} = playObj;
let rawPayload = {listen_type: 'single', payload: [this.playToClientPayload(playObj)]};
try {
const resp = await this.api.submitListen(playObj);
rawPayload = resp;
await this.api.submitListen(playObj, true);
if (newFromSource) {
this.logger.info(`Scrobbled (New) => (${source}) ${buildTrackString(playObj)}`);
} else {
this.logger.info(`Scrobbled (Backlog) => (${source}) ${buildTrackString(playObj)}`);
}
// last fm has rate limits but i can't find a specific example of what that limit is. going to default to 1 scrobble/sec to be safe
//await sleep(1000);
return playObj;
} catch (e) {
await this.notifier.notify({title: `Client - ${capitalize(this.type)} - ${this.name} - Scrobble Error`, message: `Failed to scrobble => ${buildTrackString(playObj)} | Error: ${e.message}`, priority: 'error'});
this.logger.error(`Failed to scrobble => ${e.message}`, {payload: rawPayload});
if(e instanceof UpstreamError) {
throw e;
} else {
throw new UpstreamError(`Error occurred while making Listenbrainz API request: ${e.message}`, {cause: e, showStopper: true});
}
} finally {
this.logger.debug(`Raw Payload:`, {rawPayload});
throw new UpstreamError(`Error occurred while making Listenbrainz API scrobble request: ${e.message}`, {cause: e, showStopper: !(e instanceof UpstreamError)});
}
}
}