-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLastFM.php
93 lines (84 loc) · 3.64 KB
/
LastFM.php
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
<?php
require "auth.php";
define("LASTFM_API_URL", "http://ws.audioscrobbler.com/2.0/?format=json");
define("LASTFM_API_URL_SECURE", "https://ws.audioscrobbler.com/2.0/?format=json");
/*
* Returns an associative array of the values in Last.fm's response
* Documentation: http://www.last.fm/api/show/auth.getMobileSession
*/
function LastFM_getLastFMSession($username, $password){
$APISignature = md5("api_key" . LASTFM_API_KEY .
"method" . "auth.getMobileSession" .
"password" . $password .
"username" . $username .
LASTFM_API_SECRET);
$sessionRequest = new HttpRequest(LASTFM_API_URL_SECURE, HttpRequest::METH_POST);
$sessionRequest->addPostFields(array("username" => $username,
"password" => $password,
"method" => "auth.getMobileSession",
"api_key" => LASTFM_API_KEY,
"api_sig" => $APISignature));
try {
$sessionRequest->send();
if ($sessionRequest->getResponseCode() == 200) {
return json_decode($sessionRequest->getResponseBody(), true);
} else {
return null;
}
} catch (HttpException $e) {
echo $e;
}
}
/*
* Returns an associative array of the values in Last.fm's response
* Documentation: http://www.last.fm/api/show/track.scrobble
*/
function LastFM_scrobbleSong($artist, $song, $album, $timestamp, $sessionKey){
$APISignature = md5("api_key" . LASTFM_API_KEY .
"artist" . $artist .
"method" . "track.scrobble" .
"sk" . $sessionKey .
"timestamp" . $timestamp .
"track" . $song .
LASTFM_API_SECRET);
$scrobbleRequest = new HttpRequest(LASTFM_API_URL_SECURE, HttpRequest::METH_POST);
$scrobbleRequest->addPostFields(array("api_key" => LASTFM_API_KEY,
"artist" => $artist,
"method" => "track.scrobble",
"sk" => $sessionKey,
"timestamp" => $timestamp,
"track" => $song,
"api_sig" => $APISignature));
try {
$scrobbleRequest->send();
if ($scrobbleRequest->getResponseCode() == 200) {
return json_decode($scrobbleRequest->getResponseBody(), true);
} else {
return null;
}
} catch (HttpException $e) {
echo $e;
}
}
/*
* Authorizes itself with Last.fm and scrobbles a song
*/
function LastFM_authenticateAndScrobble($artist, $song, $album, $timestamp){
$userSession = getLastFMSession(WMFO_LASTFM_USERNAME, WMFO_LASTFM_PASSWORD);
if ($userSession == null){
echo "Error: Unexpected response from server when trying to authenticate";
return 1;
} elseif (isset($userSession['error'])){
echo "Error " . $userSession['error'] . ": " . $userSession['message'];
return $userSession['error'];
} else {
$sessionKey = $userSession['session']['key'];
$response = scrobbleSong($artist, $track, $album, $timestamp, $sessionKey);
if (isset($response['error'])){
echo "Error " . $response['error'] . ": " . $response['message'];
return $response['error'];
}
}
return 0;
}
?>