-
Notifications
You must be signed in to change notification settings - Fork 0
/
LastFmDao.ts
112 lines (95 loc) · 3.7 KB
/
LastFmDao.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/// <reference path="./definitions/DefinitelyTyped/underscore/underscore.d.ts"/>
/// <reference path="./definitions/dummy-definitions/lastfm.d.ts"/>
/// <reference path="./definitions/typescript-node-definitions/winston.d.ts"/>
import s = require("./Song");
import _ = require("underscore")
import lastfm = require("lastfm")
import winston = require("winston");
export interface LastFmDao {
postNowPlaying(song:s.Song, lastFmUsername:string, session:string, callback?:(err, status)=>void): void;
scrobble(song:s.Song, lastFmUsername:string, session:string, callback?:(err, status)=>void): void;
}
export class DummyLastFmDao implements LastFmDao {
postNowPlaying(song:s.Song, lastFmUsername:string, session:string, callback?:(err, status)=>void): void {
callback = callback || (() => {});
if (song.Artist && song.Track) {
winston.info("Faking post now playing to user " + lastFmUsername + ", session " + session + " of song:", song);
callback(null, "OK");
}
else {
callback("Invalid song", null);
}
}
scrobble(song:s.Song, lastFmUsername:string, session:string, callback?:(err, status)=>void): void {
callback = callback || (() => {});
if (song.Artist && song.Track) {
winston.info("Faking scrobble to user " + lastFmUsername + ", session " + session + " of song:", song);
callback(null, "OK");
}
else {
callback("Invalid song", null);
}
}
}
export class LastFmDaoImpl implements LastFmDao {
private postNowPlayingLength: number;
constructor(private lastfmNode: lastfm.LastFmNode, postNowPlayingLength: number) {
this.postNowPlayingLength = postNowPlayingLength;
}
postNowPlaying(song:s.Song, lastFmUsername:string, sessionKey:string, callback?:(err, status)=>void): void {
callback = callback || (() => {});
if (!song || !song.Artist || !song.Track || !lastFmUsername || !sessionKey) {
winston.error("postNowPlaying invalid parameters:", { song: song, lastFmUsername:lastFmUsername, sessionKey:sessionKey });
callback("Invalid parameters", null);
return;
}
var updateOptions = {
artist: song.Artist,
track: song.Track,
duration: this.postNowPlayingLength,
handlers: {
success: function (data) {
winston.info("Success posting now playing for " + lastFmUsername, song);
callback(null, "OK");
},
retry: function (result) {
winston.warn("Retrying post now playing for " + lastFmUsername, result);
},
error: function (error) {
winston.error("Error posting now playing for " + lastFmUsername, error.message);
callback(error, null);
}
}
};
var session = this.lastfmNode.session(lastFmUsername, sessionKey);
this.lastfmNode.update('nowplaying', session, updateOptions);
}
scrobble(song:s.Song, lastFmUsername: string, sessionKey:string, callback?:(err, status)=>void) {
callback = callback || (() => {});
if (!song || !song.Artist || !song.Track || !lastFmUsername || !sessionKey) {
winston.error("scrobble invalid parameters:", { song: song, lastFmUsername:lastFmUsername, sessionKey:sessionKey });
callback("Invalid parameters", null);
return;
}
var updateOptions = {
artist: song.Artist,
track: song.Track,
timestamp: Math.round(song.StartTime / 1000),
handlers: {
success: function (data) {
winston.info("Success posting scrobble for " + lastFmUsername, song);
callback(null, "OK");
},
retry: function (result) {
winston.warn("Retrying scrobble for " + lastFmUsername, result);
},
error: function (error) {
winston.error("Error posting scrobble for " + lastFmUsername, error.message);
callback(error, null);
}
}
};
var session = this.lastfmNode.session(lastFmUsername, sessionKey);
this.lastfmNode.update('scrobble', session, updateOptions);
}
}