Skip to content

Commit

Permalink
Fixed track.scrobble
Browse files Browse the repository at this point in the history
  • Loading branch information
yayuyokitano authored Nov 23, 2020
1 parent 5d5911a commit 53ea63c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
28 changes: 2 additions & 26 deletions src/classes/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,6 @@ interface ScrobbleObject {
duration?:number;
}

interface ScrobbleArrayed {
artist:string[];
track:string[];
timestamp:number[];
album?:string[];
chosenByUser?:(0|1)[];
trackNumber?:number[];
mbid?:string[];
albumArtist?:string[];
duration?:number[];
}

interface ScrobbleArrayStringified {
artist:string;
track:string;
timestamp:string;
album?:string;
chosenByUser?:string;
trackNumber?:string;
mbid?:string;
albumArtist?:string;
duration?:string;
}

export default class TrackClass extends Base {

public async addTags(artist:string, track:string, tags:string[]|string, sk:string) {
Expand Down Expand Up @@ -102,12 +78,12 @@ export default class TrackClass extends Base {
let params:any = {};

for (let [index, scrobble] of scrobbles.entries()) {
for (let [key, value] of Object.keys(scrobble)) {
for (let [key, value] of Object.entries(scrobble)) {
params[`${key}[${index}]`] = value;
}
}

return await new LFMRequest(this.key, this.secret, {method: "track.scrobble", ...params, sk}).execute(true) as any;
return (await new LFMRequest(this.key, this.secret, {method: "track.scrobble", ...params, sk}).execute()).scrobbles as TrackInterface.scrobble;

}

Expand Down
23 changes: 23 additions & 0 deletions src/interfaces/trackInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ export interface getTopTags {
"@attr":TrackMetadata;
}

interface ScrobbleEntry {
corrected:string;
"#text":string;
}

export interface scrobble {
"@attr": {
accepted:number;
ignored:number;
}
scrobble: {
artist:ScrobbleEntry;
ignoredMessage: {
code:string;
"#text":string;
}
albumArtist:ScrobbleEntry;
timestamp:string;
album:ScrobbleEntry;
track:ScrobbleEntry;
}
}

export interface search {
"opensearch:Query": {
"#text":string;
Expand Down
12 changes: 6 additions & 6 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class LFMRequest {

}

public async execute(isScrobble = false) {
public async execute() {

const isPostRequest = this.isPostRequest();

Expand All @@ -56,7 +56,7 @@ export default class LFMRequest {
throw new SyntaxError("Please enter an api secret key to use post requests with session key.");
}

this.response = await this.post(isScrobble);
this.response = await this.post();

} else {

Expand Down Expand Up @@ -105,14 +105,14 @@ export default class LFMRequest {

}

private async post(isScrobble:boolean) {
private async post() {

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

const api_sig = this.getSignature(isScrobble);
const api_sig = this.getSignature();

const requestParam = {
...this.params,
Expand Down Expand Up @@ -146,7 +146,7 @@ export default class LFMRequest {

}

private getSignature(isScrobble:boolean) {
private getSignature() {

const paramObj:any = {
...this.params,
Expand All @@ -155,7 +155,7 @@ export default class LFMRequest {

const args = Object.keys(paramObj).sort().map((e) => [e, paramObj[e]]) as string[][];

let sig = args.reduce((acc, cur) => acc + (isScrobble && !["api_key", "sk"].includes(cur[0]) ? "" : cur[0]) + cur[1], "");
let sig = args.reduce((acc, cur) => `${acc}${cur[0]}${cur[1]}`, "");

sig = md5(sig + this.secret);

Expand Down

0 comments on commit 53ea63c

Please sign in to comment.