Skip to content

Instantly share code, notes, and snippets.

@chrisjdavis
Last active August 23, 2019 22:22
Show Gist options
  • Save chrisjdavis/6bceb36633a1f36bd63f03256ca48de2 to your computer and use it in GitHub Desktop.
Save chrisjdavis/6bceb36633a1f36bd63f03256ca48de2 to your computer and use it in GitHub Desktop.
<?php
class EasyLASTFM
{
const ENDPOINT = 'http://ws.audioscrobbler.com/2.0/';
const URL = 'https://www.last.fm/music/';
const API_KEY = '';
const CACHE = '86400'; // 24 hours in seconds
private function create_dir($path) {
if ( !is_dir( $path ) ) {
mkdir( $path, 0755, true);
}
}
private function generate_url($method, $args) {
$str = '&';
foreach ($args as $key => $value) {
$str .= $key . '=' . $value . '&';
}
$url = self::ENDPOINT . '?method=' . $method . '&extended=1&&api_key=' . self::API_KEY . $str;
return $url;
}
public function get_latest_tracks($args) {
$dir = 'cached-files/';
$cachefile = $dir . date( 'mdY' ) . '.txt';
if( file_exists($cachefile) && time() - $cachetime < filemtime($cachefile) ) {
$tracks = file_get_contents( $cachefile );
$tracks = json_decode( $tracks );
} else {
$tracks = file_get_contents( $this->generate_url( 'user.getrecenttracks', $args ) );
$this->create_dir( $dir );
file_put_contents( $cachefile, $tracks );
$tracks = json_decode( $tracks );
}
return $tracks->recenttracks->track;
}
public function get_latest_loved_tracks($args) {
$dir = 'cached-files/';
$cachefile = $dir . date( 'mdY' ) . '.loved.txt';
if( file_exists($cachefile) && time() - $cachetime < filemtime($cachefile) ) {
$tracks = file_get_contents( $cachefile );
$tracks = json_decode( $tracks );
} else {
$tracks = file_get_contents( $this->generate_url( 'user.getLovedTracks', $args ) );
$this->create_dir( $dir );
file_put_contents( $cachefile, $tracks );
$tracks = json_decode( $tracks );
}
return $tracks->lovedtracks->track;
}
public function get_album_url($album, $artist) {
$album_uri = str_replace( ' ','+', $album );
$artist_uri = str_replace( ' ','+', $artist );
return self::URL . $artist_uri . '/' . $album_uri;
}
}
$lastfm = new EasyLASTFM();
?>
<ul>
<?php
/*
* band name: echo $track->name;
* band url: echo $lastfm->artist->url;
* song name: echo $track->artist->name;
* song url: echo $track->url;
* album name: echo $track->album->{'#text'};
* album url: echo $lastfm->get_album_url( $track->album->{'#text'}, $track->artist->{'#text'} );
* album art small: echo $track->image[0]->{'#text'};
* album art medium: echo $track->image[1]->{'#text'};
* album art large: echo $track->image[2]->{'#text'};
* album art xlarge: echo $track->image[3]->{'#text'};
*/
$tracks = $lastfm->get_latest_tracks( array('user' => 'chrisjdavis', 'format' => 'json', 'limit' => 8) );
foreach( $tracks as $track ) {
echo '<li>';
echo '<p><a href="' . $track->url . '">' . $track->name . '</a></p>';
echo '<p>by <a href="'. $track->artist->url . '">' . $track->artist->name . '</a>';
echo ' from the album <a href="' . $lastfm->get_album_url( $track->album->{'#text'}, $track->artist->name ) . '">' . $track->album->{'#text'} . '</a></p>';
echo '<p><img src="' . $track->image[2]->{'#text'} . '"></p>';
if( $track->date->uts != NULL ) {
echo '<p>' . date( 'l F jS, Y', $track->date->uts ) . '</p>';
} else {
echo 'Now Playing';
}
echo '</li>';
}
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment