// Group.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 Group. /// public class Group : Base, IEquatable, IHasWeeklyAlbumCharts, IHasWeeklyArtistCharts, IHasWeeklyTrackCharts, IHasURL { /// /// Name of the group. /// public string Name {get; private set;} public Group(string groupName, Session session) :base(session) { Name = groupName; } internal override RequestParameters getParams () { RequestParameters p = new Lastfm.RequestParameters(); p["group"] = Name; return p; } /// /// String representation of the object. /// /// /// A /// public override string ToString() { return Name; } /// /// Returns the latest weekly track chart for this group. /// /// /// A /// public WeeklyTrackChart GetWeeklyTrackChart() { XmlDocument doc = request("group.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 the weekly track chart for this group in the given . /// /// /// 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("group.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 for this group. /// /// /// A /// public WeeklyArtistChart GetWeeklyArtistChart() { XmlDocument doc = request("group.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 the weekly artist chart for this group in the given /// . /// /// /// 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("group.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 for this group. /// /// /// A /// public WeeklyAlbumChart GetWeeklyAlbumChart() { XmlDocument doc = request("group.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 the weekly album chart for this group in the given . /// /// /// 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("group.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 the available timespans for charts available for this group. /// /// /// A /// public WeeklyChartTimeSpan[] GetWeeklyChartTimeSpans() { XmlDocument doc = request("group.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(); } public bool Equals(Group group) { return (group.Name == this.Name); } /// /// Returns the Last.fm page of this object. /// /// /// A /// /// /// A /// public string GetURL(SiteLanguage language) { string domain = getSiteDomain(language); return "http://" + domain + "/group/" + urlSafe(Name); } /// /// The object's Last.fm page url. /// public string URL { get { return GetURL(SiteLanguage.English); } } /// /// The members in this group. /// public GroupMembers Members { get { return new GroupMembers(this, Session); } } } }