mirror of
https://github.com/Mastermindzh/tidal-hifi.git
synced 2025-08-05 12:26:41 +02:00
various code improvements and some boyscout rule fixes :)
This commit is contained in:
@@ -1,12 +1,35 @@
|
||||
import axios from "axios";
|
||||
import { settingsStore } from "../../scripts/settings";
|
||||
import { ipcRenderer } from "electron";
|
||||
import Store from "electron-store";
|
||||
import { settings } from "../../constants/settings";
|
||||
import { MediaStatus } from "../../models/mediaStatus";
|
||||
import Store from "electron-store";
|
||||
import { settingsStore } from "../../scripts/settings";
|
||||
import { Logger } from "../logger";
|
||||
import { StoreData } from "./models/storeData";
|
||||
|
||||
const ListenBrainzStore = new Store({name: "listenbrainz"});
|
||||
const ListenBrainzStore = new Store({ name: "listenbrainz" });
|
||||
|
||||
export const ListenBrainzConstants = {
|
||||
oldData: "oldData",
|
||||
};
|
||||
|
||||
export class ListenBrainz {
|
||||
/**
|
||||
* Create the object to store old information in the Store :)
|
||||
* @param title
|
||||
* @param artists
|
||||
* @param duration
|
||||
* @returns data passed along in an object + a "listenedAt" key with the current time
|
||||
*/
|
||||
private static constructStoreData(title: string, artists: string, duration: number): StoreData {
|
||||
return {
|
||||
listenedAt: Math.floor(new Date().getTime() / 1000),
|
||||
title,
|
||||
artists,
|
||||
duration,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the ListenBrainz API and create playing now payload and scrobble old song
|
||||
* @param title
|
||||
@@ -14,72 +37,98 @@ export class ListenBrainz {
|
||||
* @param status
|
||||
* @param duration
|
||||
*/
|
||||
public static async scrobble(title: string, artists: string, status: string, duration: number): Promise<any> {
|
||||
public static async scrobble(
|
||||
title: string,
|
||||
artists: string,
|
||||
status: string,
|
||||
duration: number
|
||||
): Promise<void> {
|
||||
try {
|
||||
if (status == MediaStatus.paused) {
|
||||
return false;
|
||||
if (status === MediaStatus.paused) {
|
||||
return;
|
||||
} else {
|
||||
// Fetches the OldData required for scrobbling and proceeds to construct a playing_now data payload for the Playing Now area
|
||||
const OldData = ListenBrainzStore.get("OldData") as string[];
|
||||
// Fetches the oldData required for scrobbling and proceeds to construct a playing_now data payload for the Playing Now area
|
||||
const oldData = ListenBrainzStore.get(ListenBrainzConstants.oldData) as StoreData;
|
||||
const playing_data = {
|
||||
listen_type: "playing_now",
|
||||
payload: [
|
||||
{
|
||||
track_metadata: {
|
||||
additional_info: {
|
||||
media_player: "Tidal Hi-Fi",
|
||||
submission_client: "Tidal Hi-Fi",
|
||||
music_service: "tidal.com",
|
||||
duration: duration,
|
||||
},
|
||||
artist_name: artists,
|
||||
track_name: title,
|
||||
}
|
||||
}
|
||||
]
|
||||
listen_type: "playing_now",
|
||||
payload: [
|
||||
{
|
||||
track_metadata: {
|
||||
additional_info: {
|
||||
media_player: "Tidal Hi-Fi",
|
||||
submission_client: "Tidal Hi-Fi",
|
||||
music_service: "tidal.com",
|
||||
duration: duration,
|
||||
},
|
||||
artist_name: artists,
|
||||
track_name: title,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
await axios.post(`${settingsStore.get(settings.ListenBrainz.api)}/1/submit-listens`, playing_data, {
|
||||
headers:{
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Token ${settingsStore.get(settings.ListenBrainz.token)}`
|
||||
}
|
||||
});
|
||||
if (!OldData) {
|
||||
ListenBrainzStore.set("OldData", [Math.floor(new Date().getTime() / 1000), title, artists, duration]);
|
||||
} else if (OldData[1] != title) {
|
||||
await axios.post(
|
||||
`${settingsStore.get<string, string>(settings.ListenBrainz.api)}/1/submit-listens`,
|
||||
playing_data,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Token ${settingsStore.get<string, string>(
|
||||
settings.ListenBrainz.token
|
||||
)}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
if (!oldData) {
|
||||
ListenBrainzStore.set(
|
||||
ListenBrainzConstants.oldData,
|
||||
this.constructStoreData(title, artists, duration)
|
||||
);
|
||||
} else {
|
||||
if (oldData.title !== title) {
|
||||
// This constructs the data required to scrobble the data after the song finishes
|
||||
const scrobble_data = {
|
||||
listen_type: "single",
|
||||
payload: [
|
||||
{
|
||||
listened_at: OldData[0],
|
||||
track_metadata: {
|
||||
additional_info: {
|
||||
media_player: "Tidal Hi-Fi",
|
||||
submission_client: "Tidal Hi-Fi",
|
||||
music_service: "listen.tidal.com",
|
||||
duration: OldData[3],
|
||||
},
|
||||
artist_name: OldData[2],
|
||||
track_name: OldData[1],
|
||||
}
|
||||
}
|
||||
]
|
||||
listen_type: "single",
|
||||
payload: [
|
||||
{
|
||||
listened_at: oldData.listenedAt,
|
||||
track_metadata: {
|
||||
additional_info: {
|
||||
media_player: "Tidal Hi-Fi",
|
||||
submission_client: "Tidal Hi-Fi",
|
||||
music_service: "listen.tidal.com",
|
||||
duration: oldData.duration,
|
||||
},
|
||||
artist_name: oldData.artists,
|
||||
track_name: oldData.title,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
await axios.post(`${settingsStore.get(settings.ListenBrainz.api)}/1/submit-listens`, scrobble_data, {
|
||||
headers:{
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Token ${settingsStore.get(settings.ListenBrainz.token)}`
|
||||
}
|
||||
});
|
||||
ListenBrainzStore.set("OldData", [Math.floor(new Date().getTime() / 1000), title, artists, duration]);
|
||||
await axios.post(
|
||||
`${settingsStore.get<string, string>(settings.ListenBrainz.api)}/1/submit-listens`,
|
||||
scrobble_data,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Token ${settingsStore.get<string, string>(
|
||||
settings.ListenBrainz.token
|
||||
)}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
ListenBrainzStore.set(
|
||||
ListenBrainzConstants.oldData,
|
||||
this.constructStoreData(title, artists, duration)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(JSON.stringify(error));
|
||||
const logger = new Logger(ipcRenderer);
|
||||
logger.log(JSON.stringify(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { ListenBrainzStore };
|
||||
export { ListenBrainzStore };
|
||||
|
9
src/features/listenbrainz/models/storeData.ts
Normal file
9
src/features/listenbrainz/models/storeData.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Data saved for ListenBrainz
|
||||
*/
|
||||
export interface StoreData {
|
||||
listenedAt: number;
|
||||
title: string;
|
||||
artists: string;
|
||||
duration: number;
|
||||
}
|
@@ -26,8 +26,7 @@ export class Logger {
|
||||
public log(content: string, object: object = {}) {
|
||||
if (this.ipcRenderer) {
|
||||
this.ipcRenderer.send(globalEvents.log, { content, object });
|
||||
} else {
|
||||
console.log(`${content} \n ${JSON.stringify(object, null, 2)}`);
|
||||
}
|
||||
console.log(`${content} \n ${JSON.stringify(object, null, 2)}`);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user