Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Venipa/ytmdesktop2
Browse files Browse the repository at this point in the history
  • Loading branch information
Venipa committed Mar 11, 2024
2 parents 4e92dd9 + b7f7d60 commit 4bf9874
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ytmdesktop2",
"version": "0.10.11",
"version": "0.10.12",
"private": false,
"author": "Venipa <admin@venipa.net>",
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions src/app/lib/lastfm/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ export class LastFMClient {
this.sessionName = s.name;
return this.session = s.key;
}
async updateNowPlaying(...tracks: { artist: string, track: string, album?: string, duration?: number }[]) {
if (!this.session) throw new Error("Invalid session");
const updatePromise = this.callMethod("track.updateNowPlaying", "post", {
query: {
sk: this.session,
...tracks[0]
}
})
}
async scrobble(...tracks: { artist: string, track: string, timestamp: number, album?: string, duration?: number }[]) {
if (!this.session) throw new Error("Invalid session");
return await this.callMethod("track.scrobble", "post", {
Expand Down
13 changes: 13 additions & 0 deletions src/app/plugins/lastfmProvider.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ export default class LastFMProvider extends BaseProvider implements AfterInit, O
return this.getState()
}

async handleTrackStart(track: TrackData) {{
if (!this.client.isConnected()) return;
await this.client.updateNowPlaying({
artist: track.video.author,
track: track.video.title,
duration: track.meta.duration
}).then(stringifyJson)
.then(d => this.logger.debug(d))
.catch(err => {
this.logger.error(err);
})
}}

async handleTrackChange(track: TrackData) {
if (!this.client.isConnected()) return;
await this.client.scrobble({
Expand Down
3 changes: 2 additions & 1 deletion src/app/plugins/trackProvider.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default class TrackProvider extends BaseProvider implements AfterInit {
if (trackId) this.__onActiveTrack(trackId);
}
/**
*
*
* @param lazy 500ms timeout before throwing
* @returns [like, dislike] tuple
*/
Expand Down Expand Up @@ -152,6 +152,7 @@ export default class TrackProvider extends BaseProvider implements AfterInit {
const lastfm = this.getProvider("lastfm");
const lastfmState = lastfm.getState();
if (lastfm && lastfmState.connected && !lastfmState.processing) {
await lastfm.handleTrackStart(track)
if (this.trackChangeTimeout) clearTimeout(this.trackChangeTimeout);
this.trackChangeTimeout = setTimeout(() => {
lastfm.handleTrackChange(track)
Expand Down

0 comments on commit 4bf9874

Please sign in to comment.