Skip to content

Instantly share code, notes, and snippets.

@ab5w
Last active April 13, 2016 22:31
Show Gist options
  • Save ab5w/5086fde8f878bf058256abec4e4ee9d5 to your computer and use it in GitHub Desktop.
Save ab5w/5086fde8f878bf058256abec4e4ee9d5 to your computer and use it in GitHub Desktop.
<?php
class lastfm {
private $userfile = 'lastfm.json';
private $api_key = '';
public function __construct() {
if (!file_exists($this->userfile)) { touch($this->userfile); chmod($this->userfile, 0755); }
}
private function getuser($nick) {
$users = json_decode(file_get_contents($this->userfile),true);
$user = $users[$nick];
return $user;
}
public function checkuser($nick) {
$users = json_decode(file_get_contents($this->userfile),true);
if (empty($users[$nick])) {
return false;
} else {
return true;
}
}
public function adduser($nick,$user) {
$users = json_decode(file_get_contents($this->userfile),true);
$users[$nick] = $user;
file_put_contents($this->userfile, json_encode($users));
}
public function nowplaying($nick) {
$user = $this->getuser($nick);
$output = file_get_contents("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=". $user . "&api_key=" . $this->api_key . "&format=json");
$output = json_decode($output,true);
$lastplayed_array = $output['recenttracks']['track'][0];
$artist = $lastplayed_array['artist']['#text'];
$track = $lastplayed_array['name'];
$track = $artist . " - " . $track;
if (isset($lastplayed_array['@attr']['nowplaying'])) {
if ($this->spot_check($track)) {
$output = $nick . " is currently listening to: " . $track . ", " . $this->spot_trackid($track);
} else {
$output = $nick . " is currently listening to: " . $track;
}
} else {
if ($this->spot_check($track)) {
$output = $nick . " last listened to: " . $track . ", " . $this->spot_trackid($track);
} else {
$output = $nick . " last listened to: " . $track;
}
}
return $output;
}
private function spot_check($track) {
//Set the base URL for searching.
$base = "https://api.spotify.com/v1/search";
$track = str_replace('-', '', $track);
$track = urlencode($track);
//Talk to the spotify API to return some json yumminess.
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $base . "?q=" . $track . "&type=track");
$output = curl_exec($ch);
curl_close($ch);
//Decode the json into an array.
$output = json_decode($output,true);
//If there are no results for the track.
if ($output['tracks']['total'] == '0') {
return false;
} else {
return true;
}
}
public function spot_trackid($track) {
$base = "https://api.spotify.com/v1/search";
$track = str_replace('-', '', $track);
$track = urlencode($track);
//Talk to the spotify API to return some json yumminess.
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $base . "?q=" . $track . "&type=track");
$output = curl_exec($ch);
curl_close($ch);
//Decode the json into an array.
$output = json_decode($output,true);
$output = $output['tracks']['items'][0];
$trackid = $output['uri'];
$track = explode(':', $trackid);
$track = "https://open.spotify.com/track/" . $track[2];
return $track;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment