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

UserXML.php 4.22 KB
Newer Older
1
<?php
2
/* GNU FM -- a free network service for sharing your music listening habits
mattl's avatar
mattl committed
3

4
   Copyright (C) 2009 Free Software Foundation, Inc
mattl's avatar
mattl committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18

   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/>.

19
 */
mattl's avatar
mattl committed
20

21
require_once($install_path . '/database.php');
22
require_once($install_path . '/data/User.php');
23
require_once('xml.php');
24

25
class UserXML {
26

elleo's avatar
elleo committed
27
	public static function getInfo($username) {
clint's avatar
clint committed
28

elleo's avatar
elleo committed
29
		$user = new User($username);
30
		if (!$user) {
elleo's avatar
elleo committed
31 32
			return(XML::error('failed', '7', 'Invalid resource specified'));
		}
clint's avatar
clint committed
33

elleo's avatar
elleo committed
34 35 36
		$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
		$user_node = $xml->addChild('user', null);
		$user_node->addChild('name', $user->name);
clint's avatar
clint committed
37
//		$user_node->addChild('email', $user->email); // should this be public
elleo's avatar
elleo committed
38 39 40 41 42 43
		$user_node->addChild('homepage', $user->homepage);
		$user_node->addChild('location', $user->location);
		$user_node->addChild('bio', $user->bio);
		$user_node->addChild('profile_created', strftime('%c', $user->created));
		if (isset($user->modified))
			$user_node->addChild('profile_updated', strftime('%c', $user->modified));
clint's avatar
clint committed
44

elleo's avatar
elleo committed
45
		return($xml);
46
	}
clint's avatar
clint committed
47

elleo's avatar
elleo committed
48
	public static function getTopTracks($username, $time) {
clint's avatar
clint committed
49
		global $adodb;
clint's avatar
clint committed
50

elleo's avatar
elleo committed
51
		$timestamp;
clint's avatar
clint committed
52
		if (!isset($time))
elleo's avatar
elleo committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
			$time = 'overall';
		//TODO: Do better, this is too ugly :\
		if (strcmp($time, 'overall') == 0) {
			$timestamp = 0;
		} else if (strcmp($time, '3month') == 0) {
			$timestamp = strtotime('-3 months');
		} else if (strcmp($time, '6month') == 0) {
			$timestamp = strtotime('-6 months');
		} else if (strcmp($time, '9month') == 0) {
			$timestamp = strtotime('-9 months');
		} else if (strcmp($time, '12month') == 0) {
			$timestamp = strtotime('-12 months');
		} else {
			return(XML::error('error', '13', 'Invalid method signature supplied'));
		}
clint's avatar
clint committed
68

clint's avatar
clint committed
69 70
		$err = 0;
		try {
clint's avatar
clint committed
71 72
			$user = new User($user);
			$res = $user->getTopTracks(20, $timestamp);
clint's avatar
clint committed
73 74 75 76
		}
		catch (exception $e) {
			$err = 1;
		}
clint's avatar
clint committed
77

clint's avatar
clint committed
78
		if ($err || !$res) {
elleo's avatar
elleo committed
79
			return(XML::error('failed', '7', 'Invalid resource specified'));
clint's avatar
clint committed
80
		}
elleo's avatar
elleo committed
81
		$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
clint's avatar
clint committed
82

elleo's avatar
elleo committed
83 84 85 86
		$root = $xml->addChild('toptracks', null);
		$root->addAttribute('user', $username);
		$root->addAttribute('type', $time);
		$i = 1;
clint's avatar
clint committed
87
		foreach($res as &$row) {
elleo's avatar
elleo committed
88 89 90 91 92 93
			$track = $root->addChild('track', null);
			$track->addAttribute('rank', $i);
			$track->addChild('name', repamp($row['name']));

			$track->addChild('playcount', $row['freq']);
			$artist = $track->addChild('artist', repamp($row['artist']));
clint's avatar
clint committed
94
			$artist->addChild('mbid', $row['artist_mbid']);  // artist_mbid isn't being set by getTopTracks yet
elleo's avatar
elleo committed
95 96 97 98
			$i++;
		}

		return($xml);
clint's avatar
clint committed
99

100
	}
clint's avatar
clint committed
101

102
	public static function getRecentTracks($u, $limit) {
clint's avatar
clint committed
103
		global $adodb;
clint's avatar
clint committed
104

elleo's avatar
elleo committed
105 106 107
		if (!isset($limit)) {
			$limit = 10;
		}
clint's avatar
clint committed
108

clint's avatar
clint committed
109 110
		$err = 0;
		try {
111
			$user = new User($u);
clint's avatar
clint committed
112 113
			$res = $user->getScrobbles($limit);
		} catch (exception $e) {
clint's avatar
clint committed
114 115
			$err = 1;
		}
clint's avatar
clint committed
116

clint's avatar
clint committed
117
		if ($err || !$res) {
elleo's avatar
elleo committed
118 119
			return(XML::error('error', '7', 'Invalid resource specified'));
		}
clint's avatar
clint committed
120

elleo's avatar
elleo committed
121 122
		$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
		$root = $xml->addChild('recenttracks', null);
123
		$root->addAttribute('user', $user->name);
clint's avatar
clint committed
124

clint's avatar
clint committed
125
		foreach($res as &$row) {
elleo's avatar
elleo committed
126 127
			$track = $root->addChild('track', null);
			$artist = $track->addChild('artist', repamp($row['artist']));
clint's avatar
clint committed
128
			$artist->addAttribute('mbid', $row['artist_mbid']);
129
			$name = $track->addChild('name', repamp($row['track']));
130
			$track->addChild('mbid', $row['track_mbid']);
131 132
			$album = $track->addChild('album', repamp($row['album']));
			$album->addAttribute('mbid', $row['album_mbid']);
133 134 135 136
			$track->addChild('url', null);
			$date = $track->addChild('date', gmdate("d M Y H:i",$row['time']) . " GMT");
			$date->addAttribute('uts', $row['time']);
			$track->addChild('streamable', null);
elleo's avatar
elleo committed
137
		}
clint's avatar
clint committed
138

elleo's avatar
elleo committed
139
		return($xml);
clint's avatar
clint committed
140
	}
141 142
}
?>