Skip to content

Instantly share code, notes, and snippets.

@cxdy
Created December 15, 2017 12:36
Show Gist options
  • Save cxdy/e597f5f233893118a982af9c1fe879b9 to your computer and use it in GitHub Desktop.
Save cxdy/e597f5f233893118a982af9c1fe879b9 to your computer and use it in GitHub Desktop.
last fm current song
<?php
/**
* Package: Scrobbl.in Core (http://github.com/cxdy/Scrobbl.in)
* Version: 2.0
* Authors: Cody Kaczynski (http://github.com/cxdy)
* License: MIT License (http://opensource.org/licenses/MIT)
* You can obtain a Last.FM API key at http://www.last.fm/api/account/create
* Read the documentation (https://github.com/cxdy/Scrobbl.in/blob/master/README.md)
**/
$uname = "YOUR USERNAME"; /* Your Last.FM username */
$key = "YOUR API KEY"; /* Your Last.FM API Key */
$json = file_get_contents("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=" . $uname . "&api_key=" . $key . "&format=json"); /* This is an API call */
/* Checks if you're scrobbling, and displays what you're currently scrobbling. */
if (strpos($json, 'nowplaying') !== false) {
list($currentsonginfo, $crap) = explode('{"nowplaying":"true"}}', $json);
list($crap, $v2) = explode('{"recenttracks":{"track":[{"artist":{"#text":"', $currentsonginfo);
list($artist, $restinfo) = explode('","', $v2);
list($crap, $currenttrack) = explode('"name":"', $restinfo);
}
$playing = $artist . ' - ' . $currenttrack; /* Checks for a song. */
$artistx = htmlspecialchars($artist, ENT_NOQUOTES);
$trackx = htmlspecialchars($currenttrack, ENT_NOQUOTES);
$userx = htmlspecialchars($uname, ENT_NOQUOTES);
str_replace("&amp", "&", $artistx);
str_replace("&amp", "&", $trackx);
if ($playing == ' - ') { /* This only displays if nothing is playing. */
echo '<a title="Powered by Scrobbl.in by cxdy on GitHub" href="http://last.fm/user/' . $userx . '/">';
echo "I'm not listening to anything.";
echo "</a>";
} else { /* Displays the artist and song, and links to them respectively. */
echo '<a title="Powered by Scrobbl.in by cxdy on GitHub" href="http://last.fm/user/' . $userx . '/">';
echo "I'm currently listening to</a> <a href='http://last.fm/artist/" . str_replace(array(" ", "'"), array("+", "%27"), $artistx) . "/_/" . str_replace(array(" ", "'"), array("+", "%27"), $trackx) . "'>" . $trackx . "</a>"; // Linking your song
echo " by <a href='http://last.fm/artist/" . str_replace(array(" ", "'"), array("+", "%27"), $artistx) . "'>" . $artistx . "</a>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment