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

ArtistXML.php 3.08 KB
Newer Older
1
<?php
2
/* GNU FM -- a free network service for sharing your music listening habits
3

4
   Copyright (C) 2009 Free Software Foundation, Inc
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

require_once($install_path . '/data/Artist.php');
require_once('xml.php');

class ArtistXML {

	/**
	 * Provides artist information in XML format
	 *
	 * @param string $api_key A 32 character API key (currently not checked)
	 * @param string $artistName The name of the artist to retrieve info for
	 * @param string $mbid A music brainz ID (optional), if supplied this will be preferred to the artist name
	 * @param string $lang A 2 character ISO 639 alpha-2 code indicating the language to return the information in
	 * @return A SimpleXMLElement containing the artist's information
	 */
elleo's avatar
elleo committed
35
	public static function getInfo($artistName, $api_key=false, $mbid=false, $lang='en') {
36
		// We assume $api_key is valid and set at this point
clint's avatar
clint committed
37

38
		if (!isset($artistName) && !isset($mbid)) {
elleo's avatar
elleo committed
39
			echo XML::error('failed', '7', 'Invalid resource specified');
40 41 42 43 44
			return;
		}

		$artist = new Artist($artistName, $mbid);

45
		if (!$artist) {
elleo's avatar
elleo committed
46
			return(XML::error('failed', '7', 'Invalid resource specified'));
47 48
		}

elleo's avatar
elleo committed
49
		$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
50

elleo's avatar
elleo committed
51 52 53 54 55
		$artistXml = $xml->addChild('artist', null);
		$artistXml->addChild('name', $artist->name);
		$artistXml->addChild('mbid', $artist->mbid);
		$artistXml->addChild('url', $artist->getURL());
		$artistXml->addChild('streamable', $artist->streamable);
56

elleo's avatar
elleo committed
57 58 59 60
		$bio = $artistXml->addChild('bio', null);
		$bio->addChild('published', $artist->bio_published);
		$bio->addChild('summary', $artist->bio_summary);
		$bio->addChild('content', $artist->bio_content);
61 62 63 64 65 66 67 68

		return($xml);
	}

	public static function getTopTracks($artistName) {

		$artist = new Artist($artistName);

69
		if (!$artist) {
elleo's avatar
elleo committed
70
			return(XML::error('failed', '7', 'Invalid resource specified'));
71 72
		}

elleo's avatar
elleo committed
73 74 75
		$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
		$root = $xml->addChild('toptracks', null);
		$root->addAttribute('artist', $artist->name);
76 77 78 79 80

		$tracks = $artist->getTopTracks(50);

		// Loop over every result and add as children to "toptracks".
		for($i = 1; $i < count($tracks); $i++) {
elleo's avatar
elleo committed
81 82 83 84 85 86
			$track = $root->addChild('track', null);
			$track->addAttribute('rank', $i);
			$track->addChild('name', $tracks[$i]->name);
			$track->addChild('mbid', $tracks[$i]->mbid);
			$track->addChild('playcount', $tracks[$i]->getPlayCount());
			$track->addChild('listeners', $tracks[$i]->getListenerCount());
87 88
		}

clint's avatar
clint committed
89
		return($xml);
90 91 92 93 94
	}

}

?>