We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit f3be2aff authored by Mike Sheldon's avatar Mike Sheldon

Add recommended artist radio stream

parent 6f003fea
......@@ -346,6 +346,48 @@ class User {
}
/**
* Get artists recommended for this yser
*
* @param int $limit The number of artists to return (defaults to 10)
* @param bool $randomised Pick artists at random
* @return An array of artist details
*/
function getRecommended($limit=10, $random=false) {
global $adodb;
$loved = $this->getLovedTracks(50);
$artists = array();
for($i = 0; $i < $limit; $i++) {
if($random) {
$n = rand(0, count($loved));
} else {
$n = $i;
}
$artists[] = $loved[$n]['artist'];
}
$recommendedArtists = array();
foreach($artists as $artist_name) {
$artist = new Artist($artist_name);
$similar = $artist->getSimilar(5);
foreach($similar as $sa) {
$recommendedArtists[] = $sa;
}
}
if($random) {
$randomArtists = array();
for($i = 0; $i < $limit; $i++) {
$randomArtists[] = $recommendedArtists[rand(0, count($recommendedArtists) - 1)];
}
return $randomArtists;
} else {
return array_slice($recommendedArtists, 0, $limit);
}
}
/**
* Determines whether a user has permission to manage an artist
*
......
......@@ -76,18 +76,17 @@ function make_playlist($session, $old_format=false) {
} elseif(ereg('l(ast|ibre)fm://artist/(.*)/similarartists', $url, $regs)) {
$artist = new Artist($regs[2]);
$similarArtists = $artist->getSimilar(20);
$artistsClause = 'lower(artist_name) = ' . $adodb->qstr(mb_strtolower($artist->name, 'UTF-8'));
for($i = 0; $i < 8; $i++) {
$r = rand(0, count($similarArtists) - 1);
$artistsClause .= ' OR lower(artist_name) = ' . $adodb->qstr(mb_strtolower($similarArtists[$r]['artist'], 'UTF-8'));
}
$res = $adodb->Execute('SELECT name, artist_name, album_name, duration, streamurl FROM Track WHERE streamable=1 AND ' . $artistsClause);
$res = get_artist_selection($similarArtists, $artist);
} elseif(ereg('l(ast|ibre)fm://artist/(.*)', $url, $regs)) {
$artist = $regs[2];
$res = $adodb->Execute('SELECT name, artist_name, album_name, duration, streamurl FROM Track WHERE streamable=1 AND lower(artist_name) = ' . $adodb->qstr(mb_strtolower($artist, 'UTF-8')));
} elseif(ereg('l(ast|ibre)fm://user/(.*)/(loved|library)', $url, $regs)) {
$requser = new User($regs[2]);
$res = $adodb->Execute('SELECT Track.name, Track.artist_name, Track.album_name, Track.duration, Track.streamurl FROM Track INNER JOIN Loved_Tracks ON Track.artist_name=Loved_Tracks.artist AND Track.name=Loved_Tracks.track WHERE Loved_Tracks.userid=' . $requser->uniqueid . ' AND Track.streamable=1');
} elseif(ereg('l(ast|ibre)fm://user/(.*)/recommended', $url, $regs)) {
$requser = new User($regs[2]);
$recommendedArtists = $requser->getRecommended(8, true);
$res = get_artist_selection($recommendedArtists);
} elseif(ereg('l(ast|ibre)fm://community/loved', $url, $regs)) {
$res = $adodb->Execute('SELECT Track.name, Track.artist_name, Track.album_name, Track.duration, Track.streamurl FROM Track INNER JOIN Loved_Tracks ON Track.artist_name=Loved_Tracks.artist AND Track.name=Loved_Tracks.track WHERE Track.streamable=1');
} else {
......@@ -156,4 +155,21 @@ function make_playlist($session, $old_format=false) {
}
}
function get_artist_selection($artists, $artist=false) {
global $adodb;
if($artist) {
$artistsClause = 'lower(artist_name) = ' . $adodb->qstr(mb_strtolower($artist->name, 'UTF-8'));
}
for($i = 0; $i < 8; $i++) {
$r = rand(0, count($artists) - 1);
if($i != 0 || $artist) {
$artistsClause .= ' OR ';
}
$artistsClause .= 'lower(artist_name) = ' . $adodb->qstr(mb_strtolower($artists[$r]['artist'], 'UTF-8'));
}
return $adodb->Execute('SELECT name, artist_name, album_name, duration, streamurl FROM Track WHERE streamable=1 AND ' . $artistsClause);
}
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment