git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

Commit 3f3b886c authored by Mike Sheldon's avatar Mike Sheldon

Add artist.search webservice method

parent 629a3768
......@@ -91,6 +91,7 @@ $method_map = array(
'artist.gettopfans' => method_artist_getTopFans,
'artist.gettags' => method_artist_getTags,
'artist.getflattr' => method_artist_getFlattr,
'artist.search' => method_artist_search,
'album.addtags' => method_album_addTags,
'album.gettoptags' => method_album_getTopTags,
'album.gettags' => method_album_getTags,
......@@ -635,6 +636,33 @@ function method_artist_getFlattr() {
respond($xml);
}
/**
* artist.server : Search for an artist
*
* ###Description
* Search for an artist.
*
* ###Parameters
* * **artist** : Name of the artist.
* * **streamable** : Only return streamable artists
* * **format** (optional) : Format of response, **xml** or **json**. Default is xml.
* - - -
*
* @package Webservice
* @subpackage Artist
* @api
*/
function method_artist_search() {
if (!isset($_REQUEST['artist'])) {
report_failure(LFM_INVALID_PARAMS);
}
$streamable = get_with_default('streamable', 0);
$xml = ArtistXML::search($_REQUEST['artist'], $streamable);
respond($xml);
}
/**
* album.addtags : Add tags to an album.
*
......
......@@ -243,4 +243,32 @@ class ArtistXML {
}
public static function search($artistQuery, $streamable) {
$res = Server::search($artistQuery, "artist", 40, $streamable);
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
$root = $xml->addChild('results', null);
$root->addAttribute('xmlns:opensearch', 'http://a9.com/-/spec/opensearch/1.1/');
$root->addAttribute('for', $artistQuery);
$matches = $root->addChild('artistmatches', null);
foreach($res as &$row) {
$artist = $matches->addChild('artist', null);
$artist->addChild('name', repamp($row['name']));
$artist->addChild('mbid', $row['mbid']);
$artist->addChild('url', $row['url']);
$artist->addChild('streamable', $row['streamable'] ? "1" : "0");
$image_small = $artist->addChild('image', $row['image_small']);
$image_small->addAttribute('size', 'small');
$image_medium = $artist->addChild('image', $row['image_medium']);
$image_medium->addAttribute('size', 'medium');
$image_large = $artist->addChild('image', $row['image_large']);
$image_large->addAttribute('size', 'large');
}
return $xml;
}
}
......@@ -162,7 +162,7 @@ class Server {
* @param int $cache Caching period in seconds
* @return array An array of artists ((artist, freq, artisturl) ..) or empty array in case of failure
*/
static function getTopArtists($limit = 20, $offset = 0, $streamable = False, $begin = null, $end = null, $userid = null, $cache = 600) {
static function getTopArtists($limit = 21, $offset = 0, $streamable = False, $begin = null, $end = null, $userid = null, $cache = 600) {
global $adodb;
$query = ' SELECT artist, COUNT(artist) as freq FROM Scrobbles s';
......@@ -975,9 +975,10 @@ class Server {
* @param string $search_term
* @param string $search_type Type of search, artist|user|tag
* @param int $limit How many items to return
* @param bool $streamable Only return streamable artists
* @return array Results
*/
static function search($search_term, $search_type, $limit = 40) {
static function search($search_term, $search_type, $limit = 40, $streamable = false) {
global $adodb;
switch ($search_type) {
case 'artist':
......@@ -985,6 +986,10 @@ class Server {
$search_fields[] = 'name';
$data_fields[] = 'name';
$data_fields[] = 'bio_summary';
$data_fields[] = 'streamable';
$data_fields[] = 'image_small';
$data_fields[] = 'image_medium';
$data_fields[] = 'image_large';
break;
case 'user':
$table = 'Users';
......@@ -1021,6 +1026,10 @@ class Server {
$sql .= 'LOWER(' . $search_fields[$i] . ') LIKE LOWER(' . $adodb->qstr('%' . $search_term . '%') . ')';
}
if ($streamable) {
$sql .= " AND streamable = 1 ";
}
$sql .= 'LIMIT ' . $limit;
$res = $adodb->CacheGetAll(600, $sql);
......
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