// Album.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
{
///
/// A Last.fm album.
///
public class Album : Base, IEquatable, IHasImage, IHasURL
{
///
/// The album title.
///
public string Title {get; private set;}
///
/// The album title/name.
///
public string Name {get { return Title; } }
///
/// The album's artist.
///
public Artist Artist {get; private set; }
///
/// The album's wiki on Last.fm.
///
public AlbumWiki Wiki {get { return new AlbumWiki(this, Session); } }
///
/// Createa an album object.
///
///
/// A
///
///
/// A
///
///
/// A
///
public Album(string artistName, string title, Session session)
:base(session)
{
Artist = new Artist(artistName, Session);
Title = title;
}
///
/// Create an album.
///
///
/// A
///
///
/// A
///
///
/// A
///
public Album(Artist artist, string title, Session session)
:base(session)
{
Artist = artist;
Title = title;
}
///
/// String representation of the object.
///
///
/// A
///
public override string ToString ()
{
return Artist.Name + " - " + Title;
}
internal override RequestParameters getParams ()
{
RequestParameters p = new Lastfm.RequestParameters();
p["artist"] = Artist.Name;
p["album"] = Title;
return p;
}
///
/// Returns the album's MusicBrainz ID if available.
///
///
/// A
///
public string GetMBID()
{
XmlDocument doc = request("album.getInfo");
return extract(doc, "mbid");
}
///
/// Returns the album ID on Last.fm.
///
///
/// A
///
public string GetID()
{
XmlDocument doc = request("album.getInfo");
return extract(doc, "id");
}
///
/// Returns the album's release date.
///
///
/// A
///
public DateTime GetReleaseDate()
{
XmlDocument doc = request("album.getInfo");
return DateTime.Parse(extract(doc, "releasedate"));
}
///
/// Returns the url to the album cover if available.
///
///
/// A
///
///
/// A
///
public string GetImageURL(AlbumImageSize size)
{
XmlDocument doc = request("album.getInfo");
return extractAll(doc, "image", 4)[(int)size];
}
///
/// Returns the url to the album cover if available.
///
///
/// A
///
public string GetImageURL()
{
return GetImageURL(AlbumImageSize.ExtraLarge);
}
///
/// Returns the number of listeners on Last.fm.
///
///
/// A
///
public int GetListenerCount()
{
XmlDocument doc = request("album.getInfo");
return Int32.Parse(extract(doc, "listeners"));
}
///
/// Returns the play count on Last.fm.
///
///
/// A
///
public int GetPlaycount()
{
XmlDocument doc = request("album.getInfo");
return Int32.Parse(extract(doc, "playcount"));
}
///
/// Returns an array of the tracks on this album.
///
///
/// A
///
public Track[] GetTracks()
{
string url = "lastfm://playlist/album/" + this.GetID();
return (new XSPF(url, Session)).GetTracks();
}
///
/// Returns the top tags for this album on Last.fm.
///
///
/// A
///
public TopTag[] GetTopTags()
{
XmlDocument doc = request("album.getInfo");
XmlNode node = doc.GetElementsByTagName("toptags")[0];
List list = new List();
foreach(XmlNode n in ((XmlElement)node).GetElementsByTagName("tag"))
{
Tag tag = new Tag(extract(n, "name"), Session);
int count = Int32.Parse(extract(n, "count"));
list.Add(new TopTag(tag, count));
}
return list.ToArray();
}
///
/// Returns the top tags for this album on Last.fm.
///
///
/// A
///
///
/// A
///
public TopTag[] GetTopTags(int limit)
{
return sublist(GetTopTags(), limit);
}
///
/// Check to see if this object equals another.
///
///
/// A
///
///
/// A
///
public bool Equals(Album album)
{
if(album.Title == this.Title && album.Artist.Name == this.Artist.Name)
return true;
else
return false;
}
///
/// Search for an album on Last.fm.
///
///
/// A
///
///
/// A
///
///
/// A
///
public static AlbumSearch Search(string albumName, Session session)
{
return new AlbumSearch(albumName, session);
}
///
/// Add tags to this album.
///
///
/// 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("album.addTags", p);
}
}
///
/// Add tags to this album.
///
///
/// A
///
public void AddTags(params string[] tags)
{
foreach(string tag in tags)
AddTags(new Tag(tag, Session));
}
///
/// Add tags to this album.
///
///
/// A
///
public void AddTags(TagCollection tags)
{
foreach(Tag tag in tags)
AddTags(tag);
}
///
/// Returns the tags set by the authenticated user to this album.
///
///
/// A
///
public Tag[] GetTags()
{
//This method requires authentication
requireAuthentication();
XmlDocument doc = request("album.getTags");
TagCollection collection = new TagCollection(Session);
foreach(string name in this.extractAll(doc, "name"))
collection.Add(name);
return collection.ToArray();
}
///
/// Remove from your tags on this album.
///
///
/// 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("album.removeTag", p);
}
}
///
/// Remove from your tags on this album.
///
///
/// A
///
public void RemoveTags(params string[] tags)
{
//This method requires authentication
requireAuthentication();
foreach(string tag in tags)
RemoveTags(new Tag(tag, Session));
}
///
/// Remove from the authenticated user's tags on this album.
///
///
/// A
///
public void RemoveTags(TagCollection tags)
{
foreach(Tag tag in tags)
RemoveTags(tag);
}
///
/// Set the authenticated user's tags to only those tags.
///
///
/// A
///
public void SetTags(string[] tags)
{
List list = new List();
foreach(string name in tags)
list.Add(new Tag(name, Session));
SetTags(list.ToArray());
}
///
/// Set the authenticated user's tags to only those tags.
///
///
/// 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());
}
///
/// Set the authenticated user's tags to only those tags.
///
///
/// A
///
public void SetTags(TagCollection tags)
{
SetTags(tags.ToArray());
}
///
/// Clears all the tags that the authenticated user has set to this album.
///
public void ClearTags()
{
foreach(Tag tag in GetTags())
RemoveTags(tag);
}
///
/// Returns an album by it's MusicBrainz id.
///
///
/// A
///
///
/// A
///
///
/// A
///
public static Album GetByMBID(string mbid, Session session)
{
RequestParameters p = new Lastfm.RequestParameters();
p["mbid"] = mbid;
XmlDocument doc = (new Request("album.getInfo", session, p)).execute();
string title = doc.GetElementsByTagName("name")[0].InnerText;
string artist = doc.GetElementsByTagName("artist")[0].InnerText;
return new Album(artist, title, session);
}
///
/// Returns the Last.fm page of this object.
///
///
/// A
///
///
/// A
///
public string GetURL(SiteLanguage language)
{
string domain = getSiteDomain(language);
return "http://" + domain + "/music/" + urlSafe(Artist.Name) + "/" + urlSafe(Name);
}
///
/// The Last.fm page of this object.
///
public string URL
{ get { return GetURL(SiteLanguage.English); } }
}
}