Gazelle/classes/lastfm.class.php

179 lines
6.4 KiB
PHP
Raw Normal View History

2013-01-11 08:00:38 +00:00
<?
2013-01-02 08:00:26 +00:00
define('LASTFM_API_URL', 'http://ws.audioscrobbler.com/2.0/?method=');
2013-01-11 08:00:38 +00:00
class LastFM {
2013-04-18 08:00:54 +00:00
public static function get_artist_events($ArtistID, $Artist, $Limit = 15) {
2013-08-28 23:08:41 +00:00
$ArtistEvents = G::$Cache->get_value("artist_events_$ArtistID");
2013-04-18 08:00:54 +00:00
if (empty($ArtistEvents)) {
$ArtistEvents = self::lastfm_request("artist.getEvents", array("artist" => $Artist, "limit" => $Limit));
2013-08-28 23:08:41 +00:00
G::$Cache->cache_value("artist_events_$ArtistID", $ArtistEvents, 432000);
2013-04-18 08:00:54 +00:00
}
return $ArtistEvents;
}
2013-01-11 08:00:38 +00:00
2013-04-18 08:00:54 +00:00
public static function get_user_info($Username) {
2013-08-28 23:08:41 +00:00
$Response = G::$Cache->get_value("lastfm_user_info_$Username");
2013-04-18 08:00:54 +00:00
if (empty($Response)) {
$Response = self::lastfm_request("user.getInfo", array("user" => $Username));
2013-08-28 23:08:41 +00:00
G::$Cache->cache_value("lastfm_user_info_$Username", $Response, 86400);
2013-04-18 08:00:54 +00:00
}
return $Response;
}
2013-01-02 08:00:26 +00:00
2013-04-18 08:00:54 +00:00
public static function compare_user_with($Username1, $Limit = 15) {
2013-08-28 23:08:41 +00:00
$QueryID = G::$DB->get_query_id();
G::$DB->query("
2013-06-06 08:01:03 +00:00
SELECT username
FROM lastfm_users
2013-08-28 23:08:41 +00:00
WHERE ID = '" . G::$LoggedUser['ID'] . "'");
if (G::$DB->has_results()) {
list($Username2) = G::$DB->next_record();
2013-04-18 08:00:54 +00:00
//Make sure the usernames are in the correct order to avoid dupe cache keys.
if (strcasecmp($Username1, $Username2)) {
$Temp = $Username1;
$Username1 = $Username2;
$Username2 = $Temp;
}
2013-08-28 23:08:41 +00:00
$Response = G::$Cache->get_value("lastfm_compare_$Username1" . "_$Username2");
2013-04-18 08:00:54 +00:00
if (empty($Response)) {
$Response = self::lastfm_request("tasteometer.compare", array("type1" => "user", "type2" => "user", "value1" => $Username1, "value2" => $Username2, "limit" => $Limit));
$Response = json_encode($Response);
2013-08-28 23:08:41 +00:00
G::$Cache->cache_value("lastfm_compare_$Username1" . "_$Username2", $Response, 86400);
2013-04-18 08:00:54 +00:00
}
2013-08-28 23:08:41 +00:00
} else {
$Response = null;
2013-04-18 08:00:54 +00:00
}
2013-08-28 23:08:41 +00:00
G::$DB->set_query_id($QueryID);
return $Response;
2013-04-18 08:00:54 +00:00
}
2013-01-02 08:00:26 +00:00
2013-04-18 08:00:54 +00:00
public static function get_last_played_track($Username) {
2013-08-28 23:08:41 +00:00
$Response = G::$Cache->get_value("lastfm_last_played_track_$Username");
2013-04-18 08:00:54 +00:00
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);
2013-08-28 23:08:41 +00:00
G::$Cache->cache_value("lastfm_last_played_track_$Username", $Response, 7200);
2013-04-18 08:00:54 +00:00
}
return $Response;
}
2013-01-02 08:00:26 +00:00
2013-04-18 08:00:54 +00:00
public static function get_top_artists($Username, $Limit = 15) {
2013-08-28 23:08:41 +00:00
$Response = G::$Cache->get_value("lastfm_top_artists_$Username");
2013-04-18 08:00:54 +00:00
if (empty($Response)) {
sleep(1);
$Response = self::lastfm_request("user.getTopArtists", array("user" => $Username, "limit" => $Limit));
$Response = json_encode($Response);
2013-08-28 23:08:41 +00:00
G::$Cache->cache_value("lastfm_top_artists_$Username", $Response, 86400);
2013-04-18 08:00:54 +00:00
}
return $Response;
}
2013-01-02 08:00:26 +00:00
2013-04-18 08:00:54 +00:00
public static function get_top_albums($Username, $Limit = 15) {
2013-08-28 23:08:41 +00:00
$Response = G::$Cache->get_value("lastfm_top_albums_$Username");
2013-04-18 08:00:54 +00:00
if (empty($Response)) {
sleep(2);
$Response = self::lastfm_request("user.getTopAlbums", array("user" => $Username, "limit" => $Limit));
$Response = json_encode($Response);
2013-08-28 23:08:41 +00:00
G::$Cache->cache_value("lastfm_top_albums_$Username", $Response, 86400);
2013-04-18 08:00:54 +00:00
}
return $Response;
}
2013-01-02 08:00:26 +00:00
2013-04-18 08:00:54 +00:00
public static function get_top_tracks($Username, $Limit = 15) {
2013-08-28 23:08:41 +00:00
$Response = G::$Cache->get_value("lastfm_top_tracks_$Username");
2013-04-18 08:00:54 +00:00
if (empty($Response)) {
sleep(3);
$Response = self::lastfm_request("user.getTopTracks", array("user" => $Username, "limit" => $Limit));
$Response = json_encode($Response);
2013-08-28 23:08:41 +00:00
G::$Cache->cache_value("lastfm_top_tracks_$Username", $Response, 86400);
2013-04-18 08:00:54 +00:00
}
return $Response;
}
2013-01-02 08:00:26 +00:00
2013-09-09 08:00:52 +00:00
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) {
2013-09-10 08:00:47 +00:00
$Response = G::$Cache->get_value("lastfm_top_artists_$Limit");
2013-09-09 08:00:52 +00:00
if (empty($Response)) {
$Response = self::lastfm_request("chart.getTopArtists", array("limit" => $Limit));
$Response = json_encode($Response);
2013-09-10 08:00:47 +00:00
G::$Cache->cache_value("lastfm_top_artists_$Limit", $Response, 86400);
2013-09-09 08:00:52 +00:00
}
return $Response;
}
public static function get_hyped_artists($Limit = 100) {
2013-09-10 08:00:47 +00:00
$Response = G::$Cache->get_value("lastfm_hyped_artists_$Limit");
2013-09-09 08:00:52 +00:00
if (empty($Response)) {
$Response = self::lastfm_request("chart.getHypedArtists", array("limit" => $Limit));
$Response = json_encode($Response);
2013-09-10 08:00:47 +00:00
G::$Cache->cache_value("lastfm_hyped_artists_$Limit", $Response, 86400);
2013-09-09 08:00:52 +00:00
}
return $Response;
2013-09-08 08:00:57 +00:00
}
2013-09-10 08:00:47 +00:00
2013-09-11 08:00:55 +00:00
public static function clear_cache($Username, $UserID) {
$Response = G::$Cache->get_value("lastfm_clear_cache_$UserID");
2013-04-18 08:00:54 +00:00
if (empty($Response)) {
// Prevent clearing the cache on the same uid page for the next 10 minutes.
2013-09-11 08:00:55 +00:00
G::$Cache->cache_value("lastfm_clear_cache_$UserID", 1, 600);
2013-08-28 23:08:41 +00:00
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("
2013-06-06 08:01:03 +00:00
SELECT username
FROM lastfm_users
2013-09-11 08:00:55 +00:00
WHERE ID = " . G::$LoggedUser['ID']);
2013-08-28 23:08:41 +00:00
if (G::$DB->has_results()) {
list($Username2) = G::$DB->next_record();
2013-04-18 08:00:54 +00:00
//Make sure the usernames are in the correct order to avoid dupe cache keys.
if (strcasecmp($Username, $Username2)) {
$Temp = $Username;
$Username = $Username2;
$Username2 = $Temp;
}
2013-09-11 08:00:55 +00:00
G::$Cache->delete_value("lastfm_compare_{$Username}_$Username2");
2013-04-18 08:00:54 +00:00
}
2013-08-28 23:08:41 +00:00
G::$DB->set_query_id($QueryID);
2013-04-18 08:00:54 +00:00
}
}
2013-01-02 08:00:26 +00:00
2013-04-18 08:00:54 +00:00
private static function lastfm_request($Method, $Args) {
if (!defined('LASTFM_API_KEY')) {
return false;
}
$Url = LASTFM_API_URL . $Method;
if (is_array($Args)) {
foreach ($Args as $Key => $Value) {
2013-07-10 00:08:53 +00:00
$Url .= "&$Key=" . urlencode($Value);
2013-04-18 08:00:54 +00:00
}
$Url .= "&format=json&api_key=" . LASTFM_API_KEY;
2013-01-02 08:00:26 +00:00
2013-04-18 08:00:54 +00:00
$Curl = curl_init();
curl_setopt($Curl, CURLOPT_HEADER, 0);
curl_setopt($Curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($Curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($Curl, CURLOPT_URL, $Url);
$Return = curl_exec($Curl);
curl_close($Curl);
return json_decode($Return, true);
}
}
2013-01-02 08:00:26 +00:00
}