// Tag.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.Xml; using System.Collections.Generic; namespace Lastfm.Services { public class Tag : Base, System.IEquatable, IHasWeeklyArtistCharts, IHasURL { /// /// The tag name. /// public string Name {get; private set;} public Tag(string name, Session session) :base(session) { Name = name; } internal override RequestParameters getParams () { RequestParameters p = new Lastfm.RequestParameters(); p["tag"] = this.Name; return p; } /// /// String representation of the object. /// /// /// A /// public override string ToString () { return this.Name; } /// /// Returns similar tags. /// /// /// A /// public Tag[] GetSimilar() { XmlDocument doc = request("tag.getSimilar"); List list = new List(); foreach(string name in extractAll(doc, "name")) list.Add(new Tag(name, Session)); return list.ToArray(); } /// /// Returns the top albums tagged with this tag. /// /// /// A /// public TopAlbum[] GetTopAlbums() { XmlDocument doc = request("tag.getTopAlbums"); List list = new List(); foreach(XmlNode n in doc.GetElementsByTagName("album")) { Album album = new Album(extract(n, "name", 1), extract(n, "name"), Session); int count = Int32.Parse(extract(n, "tagcount")); list.Add(new TopAlbum(album, count)); } return list.ToArray(); } public TopArtist[] GetTopArtists(int page = 1, int limit = 50) { RequestParameters parameters = this.getParams(); parameters["page"] = page.ToString(); if (limit > 0) { parameters["limit"] = limit.ToString(); } XmlDocument document = base.request("tag.getTopArtists", parameters); List list = new List(); foreach (XmlNode node in document.GetElementsByTagName("artist")) { Artist artist = new Artist(node, base.Session); list.Add(new TopArtist(artist, 0)); } return list.ToArray(); } public TopTrack[] GetTopTracks(int page = 1, int limit = 50) { RequestParameters parameters = this.getParams(); parameters["page"] = page.ToString(); if (limit > 0) { parameters["limit"] = limit.ToString(); } XmlDocument document = base.request("tag.getTopTracks", parameters); int playcount = 0; List list = new List(); foreach (XmlNode node in document.GetElementsByTagName("track")) { var mbid = extract(node, "mbid"); Track track = new Track(base.extract(node, "name", 1), base.extract(node, "name"), mbid, base.Session); playcount++; list.Add(new TopTrack(track, playcount)); } return list.ToArray(); } /// /// Check to see if this object equals another. /// /// /// A /// /// /// A /// public bool Equals(Tag tag) { if (tag.Name == this.Name) return true; else return false; } /// /// Search for tags by name. /// /// /// A /// /// /// A /// /// /// A /// public static TagSearch Search(string name, Session session) { return new TagSearch(name, session); } /// /// Returns the available weekly chart time spans (weeks) for this tag. /// /// /// A /// public WeeklyChartTimeSpan[] GetWeeklyChartTimeSpans() { XmlDocument doc = request("tag.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 latest weekly artist chart. /// /// /// A /// public WeeklyArtistChart GetWeeklyArtistChart() { XmlDocument doc = request("tag.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 = int.Parse(node.Attributes[0].InnerText); int playcount = int.Parse(base.extract(node, "playcount")); WeeklyArtistChartItem item = new WeeklyArtistChartItem(new Artist(node, base.Session), rank, playcount, new WeeklyChartTimeSpan(nfrom, nto)); chart.Add(item); } return chart; } /// /// Returns the weekly artist chart for a specified time span (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("tag.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(node, base.Session), rank, playcount, new WeeklyChartTimeSpan(nfrom, nto)); chart.Add(item); } return chart; } /// /// Returns the Last.fm page of this object. /// /// /// A /// /// /// A /// public string GetURL(SiteLanguage language) { string domain = getSiteDomain(language); return "http://" + domain + "/tag/" + urlSafe(Name); } /// /// The object's Last.fm page url. /// public string URL { get { return GetURL(SiteLanguage.English); } } } }