Skip to content

Commit

Permalink
Moved playlist creation into its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilCrosby committed Dec 14, 2008
1 parent bbaa463 commit c9ebe8d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 83 deletions.
96 changes: 96 additions & 0 deletions classes/LastGenius.php
@@ -0,0 +1,96 @@
<?php

class LastGenius {

function getPlaylist($track, $artist, $numTracksRequired = 10) {
$url = LAST_FM_API_URL.'?method=track.getinfo&artist='.urlencode($artist).'&track='.urlencode($track).'&api_key='.LAST_FM_API_KEY;

$curl = new CurlCall();
$data = $curl->getFromXmlSource($url);
$item = $data->track;

$isStreamable = $item->streamable;
$isFullTrack = 0;
foreach($item->streamable->attributes() as $key => $value) {
if ( 'fulltrack' == $key ) {
$isFullTrack = $value;
}
}

if ($isStreamable && $isFullTrack) {
$playlist = array($data->track);
}

for ( $i=0; $i < $numTracksRequired - 1; $i++ ) {

$chooseFrom = array();

$url = LAST_FM_API_URL.'?method=track.getsimilar&artist='.urlencode($artist).'&track='.urlencode($track).'&api_key='.LAST_FM_API_KEY;
//error_log($url);

$data = $curl->getFromXmlSource($url);
//error_log(print_r($data, true));

$bestMatch = $data->similartracks->track[0]->match;

foreach ( $data->similartracks->track as $item ) {
$isStreamable = $item->streamable;
$isFullTrack = 0;
foreach($item->streamable->attributes() as $key => $value) {
if ( 'fulltrack' == $key ) {
$isFullTrack = $value;
}
}

if (!$isStreamable || !$isFullTrack) {
continue;
}

if ($this->isSongAlreadyInPlaylist($item, $playlist)) {
continue;
}

if ( $item->match < 0.85 * $bestMatch ) {
break;
}

array_push($chooseFrom, $item);
}

if ( 0 == sizeof($chooseFrom) ) {
foreach ( $data->similartracks->track as $item ) {
if (!$this->isSongAlreadyInPlaylist($item, $playlist)) {
//error_log("pushed from the topish - {$item->artist->name} - {$item->name} - {$item->match}");
array_push($playlist, $item);
break;
}
}
} else {
array_push($playlist, $chooseFrom[array_rand($chooseFrom)]);
}

$artist = $playlist[sizeof($playlist) -1]->artist->name;
$track = $playlist[sizeof($playlist) -1]->name;

}

return $playlist;

}

private function isSongAlreadyInPlaylist($song, $playlist) {
$songUrl = (string) $song->url;

foreach ( $playlist as $item ) {
//error_log("{$song->url} {$item->url}");
if ($songUrl == (string) $item->url) {
return true;
}
}

return false;
}

}

?>
86 changes: 3 additions & 83 deletions index.php
@@ -1,6 +1,7 @@
<?php

require_once('config/config.php');
require_once('classes/LastGenius.php');
require_once('classes/CurlCall.php');
require_once('classes/PhpCache.php');

Expand All @@ -21,76 +22,8 @@

$start = time();

$url = LAST_FM_API_URL.'?method=track.getinfo&artist='.urlencode($artist).'&track='.urlencode($track).'&api_key='.LAST_FM_API_KEY;

$curl = new CurlCall();
$data = $curl->getFromXmlSource($url);
$item = $data->track;

$isStreamable = $item->streamable;
$isFullTrack = 0;
foreach($item->streamable->attributes() as $key => $value) {
if ( 'fulltrack' == $key ) {
$isFullTrack = $value;
}
}

if ($isStreamable && $isFullTrack) {
$playlist = array($data->track);
}

for ( $i=0; $i < $numTracksRequired - 1; $i++ ) {

$chooseFrom = array();

$url = LAST_FM_API_URL.'?method=track.getsimilar&artist='.urlencode($artist).'&track='.urlencode($track).'&api_key='.LAST_FM_API_KEY;
//error_log($url);

$data = $curl->getFromXmlSource($url);
//error_log(print_r($data, true));

$bestMatch = $data->similartracks->track[0]->match;

foreach ( $data->similartracks->track as $item ) {
$isStreamable = $item->streamable;
$isFullTrack = 0;
foreach($item->streamable->attributes() as $key => $value) {
if ( 'fulltrack' == $key ) {
$isFullTrack = $value;
}
}

if (!$isStreamable || !$isFullTrack) {
continue;
}

if (isSongAlreadyInPlaylist($item, $playlist)) {
continue;
}

if ( $item->match < 0.85 * $bestMatch ) {
break;
}

array_push($chooseFrom, $item);
}

if ( 0 == sizeof($chooseFrom) ) {
foreach ( $data->similartracks->track as $item ) {
if (!isSongAlreadyInPlaylist($item, $playlist)) {
//error_log("pushed from the topish - {$item->artist->name} - {$item->name} - {$item->match}");
array_push($playlist, $item);
break;
}
}
} else {
array_push($playlist, $chooseFrom[array_rand($chooseFrom)]);
}

$artist = $playlist[sizeof($playlist) -1]->artist->name;
$track = $playlist[sizeof($playlist) -1]->name;

}
$lastGenius = new LastGenius();
$playlist = $lastGenius->getPlaylist($track, $artist, $numTracksRequired);

echo '<ol>';
foreach ($playlist as $item) {
Expand All @@ -101,17 +34,4 @@
$taken = time() - $start;
echo "<p>Took $taken seconds</p>";

function isSongAlreadyInPlaylist($song, $playlist) {
$songUrl = (string) $song->url;

foreach ( $playlist as $item ) {
//error_log("{$song->url} {$item->url}");
if ($songUrl == (string) $item->url) {
return true;
}
}

return false;
}

?>

0 comments on commit c9ebe8d

Please sign in to comment.