-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mar0xy
committed
Jul 30, 2023
1 parent
11cc209
commit 3571289
Showing
6 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import axios from "axios"; | ||
import { settingsStore } from "../../scripts/settings"; | ||
import { settings } from "../../constants/settings"; | ||
import { MediaStatus } from "../../models/mediaStatus"; | ||
import Store from "electron-store"; | ||
|
||
const ListenBrainzStore = new Store(); | ||
|
||
export class ListenBrainz { | ||
/** | ||
* Call the ListenBrainz API and create playing now payload | ||
* @param title | ||
* @param artists | ||
* @param status | ||
* @param duration | ||
*/ | ||
public static async scrobble(title: string, artists: string, status: string, duration: number): Promise<any> { | ||
try { | ||
if (status == MediaStatus.paused) { | ||
return false; | ||
} 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[]; | ||
const playing_data = { | ||
listen_type: "playing_now", | ||
payload: [ | ||
{ | ||
track_metadata: { | ||
additional_info: { | ||
media_player: "Tidal Hi-Fi", | ||
submission_client: "Tidal Hi-Fi", | ||
music_service: "listen.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) { | ||
// 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], | ||
} | ||
} | ||
] | ||
}; | ||
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]); | ||
} | ||
} | ||
} catch (error) { | ||
console.log(JSON.stringify(error)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters