// 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 list = new List(); foreach(XmlNode node in doc.GetElementsByTagName("track")) { // skip the track that is now playing. if (node.Attributes.Count > 0) continue; list.Add(new Track(extract(node, "artist"), extract(node, "name"), Session)); } return list.ToArray(); } /// /// Returns the most recent played tracks for this user. /// /// /// A /// public Track[] GetRecentTracks() { // default value is 10. return GetRecentTracks(10); } /// /// Returns the track that the user's currently listening to. /// /// /// A if the user is listening to a track, or null if they're not. /// public Track GetNowPlaying() { // Would return null if no track is now playing. RequestParameters p = getParams(); p["limit"] = "1"; XmlDocument doc = request("user.getRecentTracks", p); XmlNode node = doc.GetElementsByTagName("track")[0]; if (node.Attributes.Count > 0) return new Track(extract(node, "artist"), extract(node, "name"), Session); else return null; } /// /// Returns true if the user's listening right now. /// /// /// A /// public bool IsNowListening() { return (GetNowPlaying() != null); } /// /// Returns this user's playlists. /// /// /// A /// public Playlist[] GetPlaylists() { XmlDocument doc = request("user.getPlaylists"); List list = new List(); foreach(string id in extractAll(doc, "id")) list.Add(new Playlist(Name, Int32.Parse(id), Session)); return list.ToArray(); } /// /// Returns the past events attended by this user. /// public PastEvents PastEvents { get { return new PastEvents(this, Session); } } /// /// Returns the neighbours of this user. /// /// /// A /// public User[] GetNeighbours() { XmlDocument doc = request("user.getNeighbours"); List list = new List(); foreach(string name in extractAll(doc, "name")) list.Add(new User(name, Session)); return list.ToArray(); } /// /// Returns the neighbours of this user. /// /// /// A /// /// /// A /// public User[] GetNeighbours(int limit) { return sublist(GetNeighbours(), limit); } /// /// Returns the most recent 50 loved tracks by this user. /// /// /// A /// public Track[] GetLovedTracks() { XmlDocument doc = request("user.getLovedTracks"); List list = new List(); foreach(XmlNode node in doc.GetElementsByTagName("track")) list.Add(new Track(extract(node, "name", 1), extract(node, "name"), Session)); return list.ToArray(); } /// /// Returns the user's friends. /// /// /// A /// public User[] GetFriends() { XmlDocument doc = request("user.getFriends"); List list = new List(); foreach(string name in extractAll(doc, "name")) list.Add(new User(name, Session)); return list.ToArray(); } /// /// Returns the user's friends. /// /// /// A /// /// /// A /// public User[] GetFriends(int limit) { return sublist(GetFriends(), limit); } /// /// Returns the upcoming events for this user. /// /// /// A /// public Event[] GetUpcomingEvents() { XmlDocument doc = request("user.getEvents"); List list = new List(); foreach(string id in extractAll(doc, "id")) list.Add(new Event(Int32.Parse(id), Session)); return list.ToArray(); } /// /// Compare this user with another. /// /// /// A /// /// /// A /// public Tasteometer Compare(User anotherUser) { return new Tasteometer(this, anotherUser, Session); } /// /// Compare this user with a list of artists. /// /// /// A /// /// /// A /// public Tasteometer Compare(IEnumerable artists) { return new Tasteometer(this, artists, Session); } /// /// Compare this user with a myspace profile. /// /// /// A /// /// /// A /// public Tasteometer Compare(string myspaceURL) { return new Tasteometer(this, myspaceURL, Session); } /// /// Returns the Last.fm page of this object. /// /// /// A /// /// /// A /// public string GetURL(SiteLanguage language) { string domain = getSiteDomain(language); return "http://" + domain + "/user/" + urlSafe(Name); } /// /// The object's Last.fm page url. /// public string URL { get { return GetURL(SiteLanguage.English); } } } }