Skip to content

Instantly share code, notes, and snippets.

@kddlb
Created March 9, 2014 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kddlb/9443488 to your computer and use it in GitHub Desktop.
Save kddlb/9443488 to your computer and use it in GitHub Desktop.
<?php
//Uses WildPHP: https://github.com/super3/IRC-Bot/
// Namespace
namespace Command;
/**
* Gets info for a Spotify URI.
* arguments[0] == Spotify URI.
*
* @package IRCBot
* @subpackage Command
* @author Kevin López <kelopez.cl@gmail.com>
*/
class Spotify extends \Library\IRC\Command\Base {
/**
* The command's help text.
*
* @var string
*/
protected $help = '!spotify [uri]';
/**
* The number of arguments the command needs.
*
* You have to define this in the command.
*
* @var integer
*/
protected $numberOfArguments = 1;
/**
* Joins the specified channel.
*
* IRC-Syntax: JOIN [#channel]
*/
public function command() {
$sarg = preg_replace("/\r|\n/", "", implode("", $this->arguments));
if (strpos($sarg, 'track') !== false) {
$input = file_get_contents("http://ws.spotify.com/lookup/1/.json?uri=" . $sarg);
if (!($input == "")) {
$spotify = json_decode($input);
$name = $spotify->track->name;
$artists = $spotify->track->artists;
$album = $spotify->track->album->name;
$albumyear = $spotify->track->album->released;
if (isset($spotify->track->explicit)) {
$explicit = $spotify->track->explicit;
}
$length = gmdate("i:s", $spotify->track->length);
$number = $spotify->track->{"track-number"};
$ends = array('th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th');
if (($number % 100) >= 11 && ($number % 100) <= 13)
$abbreviation = $number . 'th';
else
$abbreviation = $number . $ends[$number % 10];
$fltMedio = "";
for ($i = 0; $i <= (count($artists) - 1); $i++) {
if (!($i == count($artists) - 1)) {
$fltMedio .= $artists[$i]->name . ", ";
} elseif (count($artists) == 1) {
$fltMedio .= $artists[$i]->name;
} else {
$fltMedio .= "and " . $artists[$i]->name;
}
}
$yuri = urlencode($name . " " . $artists[0]->name);
$exp ="";
if ($explicit == true) {
$exp = "EXPLICIT/NSFW " ;
}
$lol = "$exp$name ($length) by $fltMedio; $abbreviation track on $album ($albumyear) | "
. "Search on YouTube: http://www.youtube.com/results?search_query=$yuri";
$this->say($lol); } else $this->say("Error, track not found! (or Spotify banned me...)");
} else {
$this->say("Error! It should be a Spotify *track* URI.");
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment