// User.cs
//
// Copyright (C) 2008 Amr Hassan
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//
using System;
using System.Collections.Generic;
using System.Xml;
namespace Lastfm.Services
{
///
/// A Last.fm user.
///
public class User : Base, IEquatable, IHasWeeklyTrackCharts, IHasWeeklyAlbumCharts,
IHasWeeklyArtistCharts, IHasURL
{
///
/// The user's name.
///
public string Name {get; private set;}
///
/// Returns the user's library.
///
public Library Library
{
get { return new Library(this, Session); }
}
public User(string name, Session session)
:base(session)
{
Name = name;
}
///
/// String representation of the object.
///
///
/// A
///
public override string ToString ()
{
return Name;
}
internal override RequestParameters getParams ()
{
RequestParameters p = new Lastfm.RequestParameters();
p["user"] = Name;
return p;
}
///
/// Check to see if this object equals another.
///
///
/// A
///
///
/// A
///
public bool Equals(User user)
{
return (user.Name == this.Name);
}
///
/// Returns the latest weekly track chart.
///
///
/// A
///
public WeeklyTrackChart GetWeeklyTrackChart()
{
XmlDocument doc = request("user.getWeeklyTrackChart");
XmlNode n = doc.GetElementsByTagName("weeklytrackchart")[0];
DateTime nfrom = Utilities.TimestampToDateTime(Int64.Parse(n.Attributes[1].InnerText), DateTimeKind.Utc);
DateTime nto = Utilities.TimestampToDateTime(Int64.Parse(n.Attributes[2].InnerText), DateTimeKind.Utc);
WeeklyTrackChart chart = new WeeklyTrackChart(new WeeklyChartTimeSpan(nfrom, nto));
foreach(XmlNode node in doc.GetElementsByTagName("track"))
{
int rank = Int32.Parse(node.Attributes[0].InnerText);
int playcount = Int32.Parse(extract(node, "playcount"));
WeeklyTrackChartItem item =
new WeeklyTrackChartItem(new Track(extract(node, "artist"), extract(node, "name"), Session),
rank, playcount, new WeeklyChartTimeSpan(nfrom, nto));
chart.Add(item);
}
return chart;
}
///
/// Returns a weekly chart specified by a certain week.
///
///
/// A
///
///
/// A
///
public WeeklyTrackChart GetWeeklyTrackChart(WeeklyChartTimeSpan span)
{
RequestParameters p = getParams();
p["from"] = Utilities.DateTimeToUTCTimestamp(span.From).ToString();
p["to"] = Utilities.DateTimeToUTCTimestamp(span.To).ToString();
XmlDocument doc = request("user.getWeeklyTrackChart", p);
XmlNode n = doc.GetElementsByTagName("weeklytrackchart")[0];
DateTime nfrom = Utilities.TimestampToDateTime(Int64.Parse(n.Attributes[1].InnerText), DateTimeKind.Utc);
DateTime nto = Utilities.TimestampToDateTime(Int64.Parse(n.Attributes[2].InnerText), DateTimeKind.Utc);
WeeklyTrackChart chart = new WeeklyTrackChart(new WeeklyChartTimeSpan(nfrom, nto));
foreach(XmlNode node in doc.GetElementsByTagName("track"))
{
int rank = Int32.Parse(node.Attributes[0].InnerText);
int playcount = Int32.Parse(extract(node, "playcount"));
WeeklyTrackChartItem item =
new WeeklyTrackChartItem(new Track(extract(node, "artist"), extract(node, "name"), Session),
rank, playcount, new WeeklyChartTimeSpan(nfrom, nto));
chart.Add(item);
}
return chart;
}
///
/// Returns the latest weekly artist chart.
///
///
/// A
///
public WeeklyArtistChart GetWeeklyArtistChart()
{
XmlDocument doc = request("user.getWeeklyArtistChart");
XmlNode n = doc.GetElementsByTagName("weeklyartistchart")[0];
DateTime nfrom = Utilities.TimestampToDateTime(Int64.Parse(n.Attributes[1].InnerText), DateTimeKind.Utc);
DateTime nto = Utilities.TimestampToDateTime(Int64.Parse(n.Attributes[2].InnerText), DateTimeKind.Utc);
WeeklyArtistChart chart = new WeeklyArtistChart(new WeeklyChartTimeSpan(nfrom, nto));
foreach(XmlNode node in doc.GetElementsByTagName("artist"))
{
int rank = Int32.Parse(node.Attributes[0].InnerText);
int playcount = Int32.Parse(extract(node, "playcount"));
WeeklyArtistChartItem item =
new WeeklyArtistChartItem(new Artist(extract(node, "name"), Session),
rank, playcount, new WeeklyChartTimeSpan(nfrom, nto));
chart.Add(item);
}
return chart;
}
///
/// Returns a weekly artist chart of a specified week.
///
///
/// A
///
///
/// A
///
public WeeklyArtistChart GetWeeklyArtistChart(WeeklyChartTimeSpan span)
{
RequestParameters p = getParams();
p["from"] = Utilities.DateTimeToUTCTimestamp(span.From).ToString();
p["to"] = Utilities.DateTimeToUTCTimestamp(span.To).ToString();
XmlDocument doc = request("user.getWeeklyArtistChart", p);
XmlNode n = doc.GetElementsByTagName("weeklyartistchart")[0];
DateTime nfrom = Utilities.TimestampToDateTime(Int64.Parse(n.Attributes[1].InnerText), DateTimeKind.Utc);
DateTime nto = Utilities.TimestampToDateTime(Int64.Parse(n.Attributes[2].InnerText), DateTimeKind.Utc);
WeeklyArtistChart chart = new WeeklyArtistChart(new WeeklyChartTimeSpan(nfrom, nto));
foreach(XmlNode node in doc.GetElementsByTagName("artist"))
{
int rank = Int32.Parse(node.Attributes[0].InnerText);
int playcount = Int32.Parse(extract(node, "playcount"));
WeeklyArtistChartItem item =
new WeeklyArtistChartItem(new Artist(extract(node, "name"), Session),
rank, playcount, new WeeklyChartTimeSpan(nfrom, nto));
chart.Add(item);
}
return chart;
}
///
/// Returns the latest weekly album chart.
///
///
/// A
///
public WeeklyAlbumChart GetWeeklyAlbumChart()
{
XmlDocument doc = request("user.getWeeklyAlbumChart");
XmlNode n = doc.GetElementsByTagName("weeklyalbumchart")[0];
DateTime nfrom = Utilities.TimestampToDateTime(Int64.Parse(n.Attributes[1].InnerText), DateTimeKind.Utc);
DateTime nto = Utilities.TimestampToDateTime(Int64.Parse(n.Attributes[2].InnerText), DateTimeKind.Utc);
WeeklyAlbumChart chart = new WeeklyAlbumChart(new WeeklyChartTimeSpan(nfrom, nto));
foreach(XmlNode node in doc.GetElementsByTagName("album"))
{
int rank = Int32.Parse(node.Attributes[0].InnerText);
int playcount = Int32.Parse(extract(node, "playcount"));
WeeklyAlbumChartItem item =
new WeeklyAlbumChartItem(new Album(extract(node, "artist"), extract(node, "name"), Session),
rank, playcount, new WeeklyChartTimeSpan(nfrom, nto));
chart.Add(item);
}
return chart;
}
///
/// Returns a weekly album chart of a specified week.
///
///
/// A
///
///
/// A
///
public WeeklyAlbumChart GetWeeklyAlbumChart(WeeklyChartTimeSpan span)
{
RequestParameters p = getParams();
p["from"] = Utilities.DateTimeToUTCTimestamp(span.From).ToString();
p["to"] = Utilities.DateTimeToUTCTimestamp(span.To).ToString();
XmlDocument doc = request("user.getWeeklyAlbumChart", p);
XmlNode n = doc.GetElementsByTagName("weeklyalbumchart")[0];
DateTime nfrom = Utilities.TimestampToDateTime(Int64.Parse(n.Attributes[1].InnerText), DateTimeKind.Utc);
DateTime nto = Utilities.TimestampToDateTime(Int64.Parse(n.Attributes[2].InnerText), DateTimeKind.Utc);
WeeklyAlbumChart chart = new WeeklyAlbumChart(new WeeklyChartTimeSpan(nfrom, nto));
foreach(XmlNode node in doc.GetElementsByTagName("album"))
{
int rank = Int32.Parse(node.Attributes[0].InnerText);
int playcount = Int32.Parse(extract(node, "playcount"));
WeeklyAlbumChartItem item =
new WeeklyAlbumChartItem(new Album(extract(node, "artist"), extract(node, "name"), Session),
rank, playcount, new WeeklyChartTimeSpan(nfrom, nto));
chart.Add(item);
}
return chart;
}
///
/// Returns all the vailable weeks (as an array of )
///
///
/// A
///
public WeeklyChartTimeSpan[] GetWeeklyChartTimeSpans()
{
XmlDocument doc = request("user.getWeeklyChartList");
List list = new List();
foreach(XmlNode node in doc.GetElementsByTagName("chart"))
{
long lfrom = long.Parse(node.Attributes[0].InnerText);
long lto = long.Parse(node.Attributes[1].InnerText);
DateTime from = Utilities.TimestampToDateTime(lfrom, DateTimeKind.Utc);
DateTime to = Utilities.TimestampToDateTime(lto, DateTimeKind.Utc);
list.Add(new WeeklyChartTimeSpan(from, to));
}
return list.ToArray();
}
///
/// Returns the top tracks listened to by this user in a specified period.
///
///
/// A
///
///
/// A
///
public TopTrack[] GetTopTracks(Period period)
{
RequestParameters p = getParams();
p["period"] = getPeriod(period);
XmlDocument doc = request("user.getTopTracks", p);
List list = new List();
foreach(XmlNode node in doc.GetElementsByTagName("track"))
{
Track track = new Track(extract(node, "name", 1), extract(node, "name"), Session);
int count = Int32.Parse(extract(node, "playcount"));
list.Add(new TopTrack(track, count));
}
return list.ToArray();
}
///
/// Returns the overall most listned-to tracks by this user.
///
///
/// A
///
public TopTrack[] GetTopTracks()
{
return GetTopTracks(Period.Overall);
}
///
/// Returns the top tags used by this user.
///
///
/// A
///
public TopTag[] GetTopTags()
{
XmlDocument doc = request("user.getTopTags");
List list = new List();
foreach(XmlNode node in doc.GetElementsByTagName("tag"))
list.Add(new TopTag(new Tag(extract(node, "name"), Session), Int32.Parse(extract(node, "count"))));
return list.ToArray();
}
///
/// Returns the top tags used by this user.
///
///
/// A
///
///
/// A
///
public TopTag[] GetTopTags(int limit)
{
return sublist(GetTopTags(), limit);
}
///
/// Returns the top artists listened-to by this user in a specified period.
///
///
/// A
///
///
/// A
///
public TopArtist[] GetTopArtists(Period period)
{
RequestParameters p = getParams();
p["period"] = getPeriod(period);
XmlDocument doc = request("user.getTopArtists", p);
List list = new List();
foreach(XmlNode node in doc.GetElementsByTagName("artist"))
{
Artist artist = new Artist(node, base.Session);
int playcount = Int32.Parse(extract(node, "playcount"));
list.Add(new TopArtist(artist, playcount));
}
return list.ToArray();
}
///
/// Returns teh overall most listened-to artists by this user.
///
///
/// A
///
public TopArtist[] GetTopArtists()
{
return GetTopArtists(Period.Overall);
}
///
/// Returns the most listened-to albums by this user in a specified period.
///
///
/// A
///
///
/// A
///
public TopAlbum[] GetTopAlbums(Period period)
{
RequestParameters p = getParams();
p["period"] = getPeriod(period);
XmlDocument doc = request("user.getTopAlbums", p);
List list = new List();
foreach(XmlNode node in doc.GetElementsByTagName("album"))
{
Album album = new Album(extract(node, "name", 1), extract(node, "name"), Session);
int playcount = Int32.Parse(extract(node, "playcount"));
list.Add(new TopAlbum(album, playcount));
}
return list.ToArray();
}
///
/// Returne the overall most listened-to albums by this user.
///
///
/// A
///
public TopAlbum[] GetTopAlbums()
{
return GetTopAlbums(Period.Overall);
}
///
/// Returns the most recent played tracks for this user.
///
///
/// A
///
///
/// A
///
public Track[] GetRecentTracks(int limit)
{
RequestParameters p = getParams();
p["limit"] = limit.ToString();
XmlDocument doc = request("user.getRecentTracks", p);
List