// Artist.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.Globalization; using System.Xml; namespace Lastfm.Services { /// /// An artist on Last.fm /// public class Artist : Base, ITaggable, IEquatable, IShareable, IHasImage, IHasURL { /// /// The name of the artist. /// public string Name {get; private set;} /// /// The current wiki version. /// public ArtistBio Bio { get { return new ArtistBio(this, Session); } } private string[] _imageUrl; private int? _listeners; private int? _playCount; public Artist(string name, Session session) : base(session) { this._listeners = null; this._playCount = null; this.Name = name; } public Artist(XmlNode node, Session session) : base(session) { int num; int num2; this._listeners = null; this._playCount = null; this.Name = base.extract(node, "name"); this._imageUrl = base.extractAll(node, "image", 5); if (this.HasElement(node, "listeners") && int.TryParse(base.extract(node, "listeners"), out num)) { this._listeners = new int?(num); } if (this.HasElement(node, "playcount") && int.TryParse(base.extract(node, "playcount"), out num2)) { this._playCount = new int?(num2); } } private static string GetBooleanValue(bool autoCorrect) { if (autoCorrect) { return "1"; } return "0"; } private bool HasElement(XmlNode node, string tagName) { XmlNodeList elementsByTagName = ((XmlElement)node).GetElementsByTagName(tagName); return ((elementsByTagName != null) && (elementsByTagName.Count > 0)); } /// /// String representation of the object. /// /// /// A /// public override string ToString () { return this.Name; } internal override RequestParameters getParams () { RequestParameters p = new Lastfm.RequestParameters(); p["artist"] = this.Name; return p; } public static Artist GetByName(string artistName, Session session, bool autoCorrect = false) { RequestParameters parameters = new RequestParameters(); parameters["artist"] = artistName; parameters["autocorrect"] = GetBooleanValue(autoCorrect); try { XmlNodeList elementsByTagName = new Request("artist.getInfo", session, parameters).execute().GetElementsByTagName("artist"); if (elementsByTagName.Count == 0) { return null; } return new Artist(elementsByTagName[0], session); } catch (ServiceException) { return null; } } public TopSimilarArtist[] GetSimilar(int limit) { RequestParameters parameters = this.getParams(); if (limit > -1) { parameters["limit"] = limit.ToString(); } XmlDocument document = base.request("artist.getSimilar", parameters); List list = new List(); foreach (XmlNode node in document.GetElementsByTagName("artist")) { Artist artist = new Artist(node, base.Session); NumberFormatInfo provider = new NumberFormatInfo { NumberDecimalSeparator = "." }; int match = (int)(double.Parse(base.extract(node, "match"), provider) * 100.0); list.Add(new TopSimilarArtist(artist, match)); } return list.ToArray(); } /// /// Returns the similar artists to this artist ordered by similarity from the /// most similar to the least similar. /// /// /// A /// public TopSimilarArtist[] GetSimilar() { return GetSimilar(-1); } /// /// Returns the total number of listeners on Last.fm. /// /// /// A /// public int GetListenerCount() { if (!this._listeners.HasValue) { XmlDocument document = base.request("artist.getInfo"); this._listeners = new int?(Convert.ToInt32(base.extract(document, "listeners"))); } return this._listeners.Value; } /// /// Returns the total number of playcounts on Last.fm. /// /// /// A /// public int GetPlaycount() { if (!this._playCount.HasValue) { XmlDocument document = base.request("artist.getInfo"); this._playCount = new int?(Convert.ToInt32(base.extract(document, "playcount"))); } return this._playCount.Value; } public string GetImageURL() { return this.GetImageURL(ImageSize.Large); } public string GetImageURL(ImageSize size) { if (this._imageUrl == null) { XmlDocument document = base.request("artist.getInfo"); this._imageUrl = base.extractAll(document, "image"); } return this._imageUrl[(int)size]; } public TopTrack[] GetTopTracks(int page = 1, int limit = -1) { RequestParameters parameters = this.getParams(); parameters["page"] = page.ToString(); if (limit > -1) { parameters["limit"] = limit.ToString(); } XmlDocument document = base.request("artist.getTopTracks", parameters); List list = new List(); foreach (XmlNode node in document.GetElementsByTagName("track")) { Track track = new Track(base.extract(node, "name", 1), base.extract(node, "name"), base.Session); int playcount = int.Parse(base.extract(node, "playcount")); list.Add(new TopTrack(track, playcount)); } return list.ToArray(); } /// /// Returns a list of upcoming events for this artist. /// /// /// A /// public Event[] GetEvents() { XmlDocument doc = request("artist.getEvents"); List list = new List(); foreach(string id in extractAll(doc, "id")) list.Add(new Event(Int32.Parse(id), Session)); return list.ToArray(); } /// /// Returns the most popular albums by this artist. /// /// /// A /// public TopAlbum[] GetTopAlbums() { XmlDocument doc = request("artist.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 weight = int.Parse(extract(n, "playcount")); list.Add(new TopAlbum(album, weight)); } return list.ToArray(); } /// /// Returns the the top fans for this artist. /// /// /// A /// public TopFan[] GetTopFans() { XmlDocument doc = request("artist.getTopFans"); List list = new List(); foreach(XmlNode node in doc.GetElementsByTagName("user")) { User user = new User(extract(node, "name"), Session); int weight = int.Parse(extract(node, "weight")); list.Add(new TopFan(user, weight)); } return list.ToArray(); } /// /// Check to see if this object equals another. /// /// /// A /// /// /// A /// public bool Equals(Artist artist) { return (artist.Name == this.Name); } /// /// Share this artist with others. /// /// /// A /// /// /// A /// public void Share(Recipients recipients, string message) { if (recipients.Count > 1) { foreach(string recipient in recipients) { Recipients r = new Recipients(); r.Add(recipient); Share(r, message); } return; } requireAuthentication(); RequestParameters p = getParams(); p["recipient"] = recipients[0]; p["message"] = message; request("artist.Share", p); } /// /// Share this artist with others. /// /// /// A /// public void Share(Recipients recipients) { if (recipients.Count > 1) { foreach(string recipient in recipients) { Recipients r = new Recipients(); r.Add(recipient); Share(r); } return; } requireAuthentication(); RequestParameters p = getParams(); p["recipient"] = recipients[0]; request("artist.Share", p); } /// /// Search for artists on Last.fm. /// /// /// A /// /// /// A /// /// /// A /// public static ArtistSearch Search(string artistName, Session session) { return new ArtistSearch(artistName, session); } /// /// Add one or more tags to this artist. /// /// /// A /// public void AddTags(params Tag[] tags) { //This method requires authentication requireAuthentication(); foreach(Tag tag in tags) { RequestParameters p = getParams(); p["tags"] = tag.Name; request("artist.addTags", p); } } /// /// Add one or more tags to this artist. /// /// /// A /// public void AddTags(params string[] tags) { foreach(string tag in tags) AddTags(new Tag(tag, Session)); } /// /// Add one or more tags to this artist. /// /// /// A /// public void AddTags(TagCollection tags) { foreach(Tag tag in tags) AddTags(tag); } /// /// Returns the tags set by the authenticated user to this artist. /// /// /// A /// public Tag[] GetTags() { //This method requires authentication requireAuthentication(); XmlDocument doc = request("artist.getTags"); TagCollection collection = new TagCollection(Session); foreach(string name in this.extractAll(doc, "name")) collection.Add(name); return collection.ToArray(); } /// /// Returns the top tags of this artist on Last.fm. /// /// /// A /// public TopTag[] GetTopTags() { XmlDocument doc = request("artist.getTopTags"); List list = new List(); foreach(XmlNode n in doc.GetElementsByTagName("tag")) list.Add(new TopTag(new Tag(extract(n, "name"), Session), Int32.Parse(extract(n, "count")))); return list.ToArray(); } /// /// Returns the top tags of this artist on Last.fm. /// /// /// A /// /// /// A /// public TopTag[] GetTopTags(int limit) { return this.sublist(GetTopTags(), limit); } /// /// Removes from the tags that the authenticated user has set to this artist. /// /// /// A /// public void RemoveTags(params Tag[] tags) { //This method requires authentication requireAuthentication(); foreach(Tag tag in tags) { RequestParameters p = getParams(); p["tag"] = tag.Name; request("artist.removeTag", p); } } /// /// Removes from the tags that the authenticated user has set to this artist. /// /// /// A /// public void RemoveTags(params string[] tags) { //This method requires authentication requireAuthentication(); foreach(string tag in tags) RemoveTags(new Tag(tag, Session)); } /// /// Removes from the tags that the authenticated user has set to this artist. /// /// /// A /// public void RemoveTags(TagCollection tags) { foreach(Tag tag in tags) RemoveTags(tag); } /// /// Sets the tags applied by the authenticated user to this artist to /// only those tags. Removing and adding tags as neccessary. /// /// /// A /// public void SetTags(string[] tags) { List list = new List(); foreach(string name in tags) list.Add(new Tag(name, Session)); SetTags(list.ToArray()); } /// /// Sets the tags applied by the authenticated user to this artist to /// only those tags. Removing and adding tags as neccessary. /// /// /// A /// public void SetTags(Tag[] tags) { List newSet = new List(tags); List current = new List(GetTags()); List toAdd = new List(); List toRemove = new List(); foreach(Tag tag in newSet) if(!current.Contains(tag)) toAdd.Add(tag); foreach(Tag tag in current) if(!newSet.Contains(tag)) toRemove.Add(tag); if (toAdd.Count > 0) AddTags(toAdd.ToArray()); if (toRemove.Count > 0) RemoveTags(toRemove.ToArray()); } /// /// Sets the tags applied by the authenticated user to this artist to /// only those tags. Removing and adding tags as neccessary. /// /// /// A /// public void SetTags(TagCollection tags) { SetTags(tags.ToArray()); } /// /// Clears the tags that the authenticated user has set to this artist. /// public void ClearTags() { foreach(Tag tag in GetTags()) RemoveTags(tag); } /// /// Returns an artist by their MusicBrainz artist id. /// /// /// A /// /// /// A /// /// /// A /// public static Artist GetByMBID(string mbid, Session session) { RequestParameters p = new Lastfm.RequestParameters(); p["mbid"] = mbid; XmlDocument doc = (new Request("artist.getInfo", session, p)).execute(); string name = doc.GetElementsByTagName("name")[0].InnerText; return new Artist(name, session); } /// /// Returns the artist's MusicBrainz id. /// /// /// A /// public string GetMBID() { XmlDocument doc = request("artist.getInfo"); return extract(doc, "mbid"); } /// /// Returns the Last.fm page of this object. /// /// /// A /// /// /// A /// public string GetURL(SiteLanguage language) { string domain = getSiteDomain(language); return "http://" + domain + "/music/" + urlSafe(Name); } /// /// The object's Last.fm page url. /// public string URL { get { return GetURL(SiteLanguage.English); } } /// /// Returns true if the artist's music is available for streaming. /// /// /// A /// public bool IsStreamable() { XmlDocument doc = request("artist.getInfo"); if (extract(doc, "streamable") == "1") return true; else return false; } } }