forked from TOMMMMMMMMMC/GreatPosterWall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlastfm.class.php
202 lines (187 loc) · 8.42 KB
/
lastfm.class.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
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
<?
define('LASTFM_API_URL', 'http://ws.audioscrobbler.com/2.0/?method=');
class LastFM {
public static function get_lastfm_username($UserID) {
$Username = G::$Cache->get_value("lastfm_username_$UserID");
if ($Username === false) {
$QueryID = G::$DB->get_query_id();
G::$DB->query("
SELECT Username
FROM lastfm_users
WHERE ID = $UserID");
list($Username) = G::$DB->next_record();
G::$DB->set_query_id($QueryID);
G::$Cache->cache_value("lastfm_username_$UserID", $Username, 0);
}
return $Username;
}
public static function get_artist_events($ArtistID, $Artist, $Limit = 15) {
$ArtistEvents = G::$Cache->get_value("artist_events_$ArtistID");
if (empty($ArtistEvents)) {
$ArtistEvents = self::lastfm_request("artist.getEvents", array("artist" => $Artist, "limit" => $Limit));
G::$Cache->cache_value("artist_events_$ArtistID", $ArtistEvents, 432000);
}
return $ArtistEvents;
}
public static function get_user_info($Username) {
$Response = G::$Cache->get_value("lastfm_user_info_$Username");
if (empty($Response)) {
$Response = self::lastfm_request("user.getInfo", array("user" => $Username));
G::$Cache->cache_value("lastfm_user_info_$Username", $Response, 86400);
}
return $Response;
}
public static function compare_user_with($Username1, $Limit = 15) {
$QueryID = G::$DB->get_query_id();
G::$DB->query("
SELECT username
FROM lastfm_users
WHERE ID = '" . G::$LoggedUser['ID'] . "'");
if (G::$DB->has_results()) {
list($Username2) = G::$DB->next_record();
//Make sure the usernames are in the correct order to avoid dupe cache keys.
if (strcasecmp($Username1, $Username2)) {
$Temp = $Username1;
$Username1 = $Username2;
$Username2 = $Temp;
}
$Response = G::$Cache->get_value("lastfm_compare_$Username1" . "_$Username2");
if (empty($Response)) {
$Response = self::lastfm_request("tasteometer.compare", array("type1" => "user", "type2" => "user", "value1" => $Username1, "value2" => $Username2, "limit" => $Limit));
$Response = json_encode($Response);
G::$Cache->cache_value("lastfm_compare_$Username1" . "_$Username2", $Response, 86400);
}
} else {
$Response = null;
}
G::$DB->set_query_id($QueryID);
return $Response;
}
public static function get_last_played_track($Username) {
$Response = G::$Cache->get_value("lastfm_last_played_track_$Username");
if (empty($Response)) {
$Response = self::lastfm_request("user.getRecentTracks", array("user" => $Username, "limit" => 1));
// Take the single last played track out of the response.
$Response = $Response['recenttracks']['track'];
$Response = json_encode($Response);
G::$Cache->cache_value("lastfm_last_played_track_$Username", $Response, 7200);
}
return $Response;
}
public static function get_top_artists($Username, $Limit = 15) {
$Response = G::$Cache->get_value("lastfm_top_artists_$Username");
if (empty($Response)) {
sleep(1);
$Response = self::lastfm_request("user.getTopArtists", array("user" => $Username, "limit" => $Limit));
$Response = json_encode($Response);
G::$Cache->cache_value("lastfm_top_artists_$Username", $Response, 86400);
}
return $Response;
}
public static function get_top_albums($Username, $Limit = 15) {
$Response = G::$Cache->get_value("lastfm_top_albums_$Username");
if (empty($Response)) {
sleep(2);
$Response = self::lastfm_request("user.getTopAlbums", array("user" => $Username, "limit" => $Limit));
$Response = json_encode($Response);
G::$Cache->cache_value("lastfm_top_albums_$Username", $Response, 86400);
}
return $Response;
}
public static function get_top_tracks($Username, $Limit = 15) {
$Response = G::$Cache->get_value("lastfm_top_tracks_$Username");
if (empty($Response)) {
sleep(3);
$Response = self::lastfm_request("user.getTopTracks", array("user" => $Username, "limit" => $Limit));
$Response = json_encode($Response);
G::$Cache->cache_value("lastfm_top_tracks_$Username", $Response, 86400);
}
return $Response;
}
public static function get_user_artist_chart($Username, $From = '', $To = '') {
$Response = G::$Cache->get_value("lastfm_artist_chart_$Username");
if (empty($Response)) {
$Response = self::lastfm_request("user.getWeeklyArtistChart", array("user" => $Username));
$Response = json_encode($Response);
G::$Cache->cache_value("lastfm_artist_chart_$Username", $Response, 86400);
}
return $Response;
}
public static function get_weekly_artists($Limit = 100) {
$Response = G::$Cache->get_value("lastfm_top_artists_$Limit");
if (empty($Response)) {
$Response = self::lastfm_request("chart.getTopArtists", array("limit" => $Limit));
$Response = json_encode($Response);
G::$Cache->cache_value("lastfm_top_artists_$Limit", $Response, 86400);
}
return $Response;
}
public static function get_hyped_artists($Limit = 100) {
$Response = G::$Cache->get_value("lastfm_hyped_artists_$Limit");
if (empty($Response)) {
$Response = self::lastfm_request("chart.getHypedArtists", array("limit" => $Limit));
$Response = json_encode($Response);
G::$Cache->cache_value("lastfm_hyped_artists_$Limit", $Response, 86400);
}
return $Response;
}
public static function clear_cache($Username, $UserID) {
$Response = G::$Cache->get_value("lastfm_clear_cache_$UserID");
if (empty($Response)) {
// Prevent clearing the cache on the same uid page for the next 10 minutes.
G::$Cache->cache_value("lastfm_clear_cache_$UserID", 1, 600);
G::$Cache->delete_value("lastfm_user_info_$Username");
G::$Cache->delete_value("lastfm_last_played_track_$Username");
G::$Cache->delete_value("lastfm_top_artists_$Username");
G::$Cache->delete_value("lastfm_top_albums_$Username");
G::$Cache->delete_value("lastfm_top_tracks_$Username");
$QueryID = G::$DB->get_query_id();
G::$DB->query("
SELECT username
FROM lastfm_users
WHERE ID = " . G::$LoggedUser['ID']);
if (G::$DB->has_results()) {
list($Username2) = G::$DB->next_record();
//Make sure the usernames are in the correct order to avoid dupe cache keys.
if (strcasecmp($Username, $Username2)) {
$Temp = $Username;
$Username = $Username2;
$Username2 = $Temp;
}
G::$Cache->delete_value("lastfm_compare_{$Username}_$Username2");
}
G::$DB->set_query_id($QueryID);
}
}
private static function lastfm_request($Method, $Args) {
if (!defined('LASTFM_API_KEY')) {
return false;
}
$RecentFailsKey = 'lastfm_api_fails';
$RecentFails = (int)G::$Cache->get_value($RecentFailsKey);
if ($RecentFails > 5) {
// Take a break if last.fm's API is down/nonfunctional
return false;
}
$Url = LASTFM_API_URL . $Method;
if (is_array($Args)) {
foreach ($Args as $Key => $Value) {
$Url .= "&$Key=" . urlencode($Value);
}
$Url .= "&format=json&api_key=" . LASTFM_API_KEY;
$Curl = curl_init();
curl_setopt($Curl, CURLOPT_HEADER, 0);
curl_setopt($Curl, CURLOPT_TIMEOUT, 3);
curl_setopt($Curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($Curl, CURLOPT_URL, $Url);
$Return = curl_exec($Curl);
$Errno = curl_errno($Curl);
curl_close($Curl);
if ($Errno) {
G::$Cache->cache_value($RecentFailsKey, $RecentFails + 1, 1800);
return false;
}
return json_decode($Return, true);
}
}
}