Skip to content

Instantly share code, notes, and snippets.

@DamianZaremba
Created July 17, 2011 22:09
Show Gist options
  • Save DamianZaremba/1088141 to your computer and use it in GitHub Desktop.
Save DamianZaremba/1088141 to your computer and use it in GitHub Desktop.
Crappy module
<?php
/*
Plugin Name: LastFM
Plugin URI:
Description: Displays data from LastFM
Author: damianzaremba
Author URI: http://damianzaremba.co.uk
Version: 1.0
License: GPL3
*/
/*
* Copyright 2011 Damian Zaremba <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function lastfm_handler($atts) {
$api_url = "http://ws.audioscrobbler.com/2.0/";
$api_key = "notthis";
if(!array_key_exists("username", $atts)){
print "lastfm needs a username!";
return;
}else{
$username = $atts['username'];
}
if(!array_key_exists("limit", $atts)){
$limit = 20;
}else{
$limit = $atts['limit'];
}
if(!array_key_exists("period", $atts)){
$period = "overall";
}else{
$period = $atts['period'];
if($period !== "overall" && $period !== "7day" && $period !== "3month" && $period !== "6month" && $period !== "12month"){
print "Invalid period specified to lastfm";
return;
}
}
if(!array_key_exists("type", $atts)){
$type = "recentTracks";
}else{
$type = $atts['type'];
}
$api_call = $api_url . "?api_key=" . urlencode($api_key);
$api_call .= "&user=" . urlencode($username);
$api_call .= "&limit=" . urlencode($limit);
$api_call .= "&period=" . urlencode($period);
if($type == "topArtists"){
$api_call .= "&method=user.getTopArtists";
}else if($type == "recentTracks"){
$api_call .= "&method=user.getRecentTracks";
}else if($type == "topAlbums"){
$api_call .= "&method=user.getTopAlbums";
}else if($type == "topTracks"){
$api_call .= "&method=user.getTopTracks";
}else{
print "Invalid type specified to lastfm";
return;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_call);
curl_setopt($ch, CURLOPT_REFERER, get_bloginfo('siteurl'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
$data = curl_exec($ch);
curl_close($ch);
if(!isset($data) || empty($data)) {
print "<!-- Something bad happended during loading (lastfm) -->";
return False;
}
try {
$xml = @new SimpleXMLElement($data);
} catch (Exception $e) {}
if(!isset($xml)) {
print "<!-- Something bad happended during parsing xml (lastfm) -->";
return False;
}
$status = (string) $xml->attributes()->{"status"};
if($status !== "ok"){
print "<!-- Something bad happended (status not ok) (lastfm) -->";
return False;
}
if($type == "topArtists"){
$data = $xml->{"topartists"}->{"artist"};
}else if($type == "recentTracks"){
$data = $xml->{"recenttracks"}->{"track"};
}else if($type == "topAlbums"){
$data = $xml->{"topalbums"}->{"album"};
}else if($type == "topTracks"){
$data = $xml->{"toptracks"}->{"track"};
}
$output = '<div class="lastfm_content">';
foreach($data as $id => $album){
foreach($album->{"image"} as $id => $image){
$size = (string) $image->attributes()->{"size"};
$image_url = (string) $image->{0};
$album_name = (string) $album->{"name"};
$album_url = (string) $album->{"url"};
if($size === "medium") {
$output .= '<a href="' . $album_url . '" title="' . $album_name . '"><img src="' . $image_url . '" alt="' . $album_name . '" width="64px" height="64px" /></a>';
break;
}
}
}
$output .= '</div>';
return $output;
}
add_shortcode('lastfm', 'lastfm_handler');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment