File size: 5,693 Bytes
765bc42 |
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
/* global forge */
// eslint-disable-next-line no-unused-vars
{
const options = {
apiKey: '6790c00a181128dc7c4ce06cd99d17c8',
apiSecret: 'd68f1dfc6ff43044c96a79ae7dfb5c27',
};
const apiUrl = 'https://ws.audioscrobbler.com/2.0/';
let status = 0;
// const publicApi = {
// getAuth,
// cancelAuth,
// getSession,
// sendNowPlaying,
// scrobble,
// getUserInfo,
// getStatusText,
// updateStatus,
// isAuthorized,
// isAuthRequested,
// };
/**
* Computes string for signing request
*
* See https://www.last.fm/api/authspec#8
*/
const generateSign = (params) => {
const keys = Object.keys(params).filter(
(key) => key !== 'format' || key !== 'callback'
);
// params has to be ordered alphabetically
keys.sort();
const o = keys.reduce((r, key) => r + key + params[key], '');
// append secret
return forge.md5
.create()
.update(forge.util.encodeUtf8(o + options.apiSecret))
.digest()
.toHex();
};
// eslint-disable-next-line no-underscore-dangle
const _isAuthRequested = () => {
const token = localStorage.getObject('lastfmtoken');
return token != null;
};
// eslint-disable-next-line no-unused-vars
class lastfm {
static getSession(callback) {
// load session info from localStorage
let mySession = localStorage.getObject('lastfmsession');
if (mySession != null) {
return callback(mySession);
}
// trade session with token
const token = localStorage.getObject('lastfmtoken');
if (token == null) {
return callback(null);
}
// token exists
const params = {
method: 'auth.getsession',
api_key: options.apiKey,
token,
};
params.api_sig = generateSign(params);
params.format = 'json';
axios
.get(apiUrl, {
params
})
.then((response) => {
const { data } = response;
mySession = data.session;
localStorage.setObject('lastfmsession', mySession);
callback(mySession);
})
.catch((error) => {
if (error.response.status === 403) {
callback(null);
}
});
return null;
}
static getUserInfo(callback) {
this.getSession((session) => {
if (session == null) {
callback(null);
return;
}
const params = {
method: 'user.getinfo',
api_key: options.apiKey,
sk: session.key,
};
params.api_sig = generateSign(params);
params.format = 'json';
axios.post(apiUrl, '', {
params,
}).then((response) => {
const { data } = response;
if (callback != null) {
callback(data);
}
});
});
}
static updateStatus() {
// auth status
// 0: never request for auth
// 1: request but fail to success
// 2: success auth
if (!_isAuthRequested()) {
status = 0;
return;
}
this.getUserInfo((data) => {
if (data === null) {
status = 1;
} else {
status = 2;
}
});
}
static getAuth(callback) {
axios.get(apiUrl, {
params: {
method: 'auth.gettoken',
api_key: options.apiKey,
format: 'json',
},
}).then((response) => {
const { data } = response;
const { token } = data;
localStorage.setObject('lastfmtoken', token);
const grant_url = `https://www.last.fm/api/auth/?api_key=${options.apiKey}&token=${token}`;
window.open(grant_url, '_blank');
status = 1;
if (callback != null) {
callback();
}
});
}
static cancelAuth() {
localStorage.removeItem('lastfmsession');
localStorage.removeItem('lastfmtoken');
this.updateStatus();
}
static sendNowPlaying(track, artist, callback) {
this.getSession((session) => {
const params = {
method: 'track.updatenowplaying',
track,
artist,
api_key: options.apiKey,
sk: session.key,
};
params.api_sig = generateSign(params);
params.format = 'json';
axios.post(apiUrl, '', {
params
}).then((response) => {
const { data } = response;
if (callback != null) {
callback(data);
}
});
});
}
static scrobble(timestamp, track, artist, album, callback) {
this.getSession((session) => {
const params = {
method: 'track.scrobble',
'timestamp[0]': timestamp,
'track[0]': track,
'artist[0]': artist,
api_key: options.apiKey,
sk: session.key,
};
if (album !== '' && album != null) {
params['album[0]'] = album;
}
params.api_sig = generateSign(params);
params.format = 'json';
axios.post(apiUrl, '', {
params,
}).then((response) => {
const { data } = response;
if (callback != null) {
callback(data);
}
});
});
}
static isAuthorized() {
return status === 2;
}
static isAuthRequested() {
return !(status === 0);
}
static getStatusText() {
switch (status) {
case 0:
return '未连接';
case 1:
return '连接中';
case 2:
return '已连接';
default:
return '';
}
}
}
window.lastfm = lastfm;
}
|