Skip to content

Commit

Permalink
Added now playing helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
yayuyokitano authored Nov 23, 2020
1 parent 42b5a19 commit c11ea6f
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 8 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"name": "lastfm-typed",
"version": "0.1.2",
"version": "0.2.0",
"description": "Typed API wrapper for Last.FM using promises",
"main": "dist/index.js",
"author": "yayuyokitano",
"license": "MIT",
"dependencies": {
"md5": "^2.3.0",
"node-fetch": "^2.6.1"
},
"devDependencies": {
"node-fetch": "^2.6.1",
"@types/md5": "^2.2.1",
"@types/node": "^14.14.8",
"@types/node-fetch": "^2.5.7"
},
"devDependencies": {
},
"repository": {
"type": "git",
"url": "https://github.com/yayuyokitano/lastfm-typed.git"
Expand Down
126 changes: 123 additions & 3 deletions src/classes/helper.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import LastFM from "..";
import { Image } from "../interfaces/shared";
import * as ArtistInterface from "../interfaces/artistInterface";
import * as AlbumInterface from "../interfaces/albumInterface";
import * as TrackInterface from "../interfaces/trackInterface";

export default class HelperClass {

lastfm:LastFM;
private lastfm:LastFM;

constructor(lastfm:LastFM) {
public constructor(lastfm:LastFM) {
this.lastfm = lastfm;
}

async getCombo(usernameOrSessionKey:string, limit:number) {
public async getCombo(usernameOrSessionKey:string, limit:number) {
let combo = [true, true, true];
let comboData:[string,number][] = [["",0],["",0],["",0]];
let page = 0;
Expand Down Expand Up @@ -99,4 +102,121 @@ export default class HelperClass {
};
}

public async getNowPlaying(usernameOrSessionKey:string, detailTypes:("artist"|"album"|"track")[] = []) {

const currTrack = (await this.lastfm.user.getRecentTracks(usernameOrSessionKey, {limit: 1})).track[0];

const artist = currTrack.artist.name;
const track = currTrack.name;
const image = currTrack.image;
const album = currTrack.album?.name;
const url = currTrack.url;
const nowplaying = currTrack["@attr"]?.nowplaying === "true";

const details:{
artist:{
data?:ArtistInterface.getInfo;
successful:boolean;
}
album:{
data?:AlbumInterface.getInfo;
successful:boolean;
}
track:{
data?:TrackInterface.getInfo;
successful:boolean;
}
} = {
artist: {
successful: false
},
album: {
successful: false
},
track: {
successful: false
}
};

if (detailTypes !== []) {

const res = await this.fetchDetails(usernameOrSessionKey, detailTypes, artist, album, track);
console.log(res);

const exists = res.map(e => e.error === undefined);

let i = 0;

if (detailTypes.includes("artist")) {
details.artist.data = res[i];
details.artist.successful = exists[i];
i++;
}
if (detailTypes.includes("album") && album) {
details.album.data = res[i];
details.album.successful = exists[i];
i++;
}
if (detailTypes.includes("track")) {
details.track.data = res[i];
details.track.successful = exists[i];
i++;
}

return {
recent: {
artist,
album,
track,
image,
url,
nowplaying
},
details
}

}

return {
recent: {
artist,
album,
track,
image,
url,
nowplaying
},
details: {
artist: {
successful: false
},
album: {
successful: false
},
track: {
successful: false
},
}
}

}

private async fetchDetails(usernameOrSessionKey:string, detailTypes:("artist"|"album"|"track")[], artist:string, album:string, track:string) {

let promises:Promise<any>[] = [];

if (detailTypes?.includes("artist")) {
promises.push(this.lastfm.artist.getInfo({artist}, {username: usernameOrSessionKey}).catch(err => {}));
}
if (detailTypes?.includes("album") && album) {
promises.push(this.lastfm.album.getInfo({artist, album}, {username: usernameOrSessionKey}).catch(err => {}));
}
if (detailTypes?.includes("track")) {
promises.push(this.lastfm.track.getInfo({artist, track}, {username: usernameOrSessionKey}).catch(err => {}));
}

return await Promise.all(promises);

}

}
7 changes: 6 additions & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ export default class LFMRequest {
delete this.params.user;
}

if (this.params.hasOwnProperty("username")) {
this.params.sk = this.params.username;
delete this.params.username;
}

const api_sig = this.getSignature();

const requestParam = {
Expand Down Expand Up @@ -164,7 +169,7 @@ export default class LFMRequest {
}

private isPostRequest() {
return this.params.user?.length === 32 || this.params.hasOwnProperty("sk") || this.params.hasOwnProperty("token") || this.params.hasOwnProperty("password");
return this.params.user?.length === 32 || this.params.username?.length === 32 || this.params.hasOwnProperty("sk") || this.params.hasOwnProperty("token") || this.params.hasOwnProperty("password");
}

}

0 comments on commit c11ea6f

Please sign in to comment.