Click here to Skip to main content
15,893,487 members
Articles / Programming Languages / C#

LyricsFetcher - The Easiest Way to Find Lyrics for your Songs

Rate me:
Please Sign up or sign in to vote.
4.93/5 (82 votes)
29 Oct 2009GPL325 min read 201.6K   2.4K   184  
An article describing the development of a non-trivial C#/.NET application to fetch lyrics for songs.
/*
 * A Song represents a single track within a media library.
 *
 * Author: Phillip Piper
 * Date: 8/01/2008 4:28 PM
 *
 * CHANGE LOG:
 * 2009-03-19 JPP  Added FullPath property
 * 2009-02-15 JPP  Removed Kind as a visible property
 * 2008-01-07 JPP  Initial Version
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace LyricsFetcher
{
    /// <summary>
    /// This enum represents the status of lyrics for a song.
    /// </summary>
    public enum LyricsStatus {
        Success,
        Failed,
        GenreIgnored,
        DataMissing,
        Untried
    }

    /// <summary>
    /// A Song represents a single track within a media library.
    /// </summary>
    abstract public class Song
    {
        #region Constructors

        public Song()
        {
        }

        public Song(string title, string artist, string album, string genre)
        {
            this.Title = title;
            this.Artist = artist;
            this.Album = album;
            this.Genre = genre;
        }

        #endregion

        #region Public properties

        public string Album { get; set; }
        public string Artist { get; set; }
        public string Genre { get; set; }
        public string Lyrics { get; set; }
        public string Title { get; set; }

        /// <summary>
        /// Return the full path to the underlying media file
        /// </summary>
        abstract public string FullPath { get; }

        /// <summary>
        /// Return an enum indicating whether the lyrics of this song have been fetched
        /// </summary>
        public LyricsStatus LyricsStatus {
            get {
                if (!String.IsNullOrEmpty(this.Lyrics)) {
                    if (this.Lyrics.StartsWith("Failed") || this.Lyrics.StartsWith("[[LyricsFetcher failed"))
                        return LyricsStatus.Failed;
                    else
                        return LyricsStatus.Success;
                }
                if (Song.IsGenreIgnored(this.Genre))
                    return LyricsStatus.GenreIgnored;

                if (String.IsNullOrEmpty(this.Title) || String.IsNullOrEmpty(this.Artist))
                    return LyricsStatus.DataMissing;

                if (this.Title.StartsWith("Track ") || this.Title.StartsWith("Faixa "))
                    return LyricsStatus.DataMissing;

                return LyricsStatus.Untried;
            }
        }

        /// <summary>
        /// Return a string indicating the status of the lyrics of this song
        /// </summary>
        public string LyricsStatusString {
            get {
                switch (this.LyricsStatus) {
                    case LyricsStatus.Success:          return "Success";
                    case LyricsStatus.Failed:           return "Failed";
                    case LyricsStatus.GenreIgnored:     return "Genre ignored";
                    case LyricsStatus.DataMissing:      return "Missing Data";
                    case LyricsStatus.Untried:          return "Untried";
                }
                return "Unknown";
            }
        }

        #endregion

        #region Converters

        public override string ToString() {
            return string.Format("[Song Title={0} Artist={1} Album={2}]", this.Title, this.Artist, this.Album);
        }

        #endregion

        #region Commands

        abstract public void Commit();

        /// <summary>
        /// Songs should not load lyrics from their source until this method is called.
        /// Specifically, lyrics should not be loaded in the constructor.
        /// </summary>
        abstract public void GetLyrics();

        #endregion

        #region Static DB type methods

        /// <summary>
        /// Each string in this list represents a genre for which lyrics should
        /// never be fetched.
        /// </summary>
        static public List<string> GenresToIgnore = new List<String>();

        static public bool IsGenreIgnored(string genre)
        {
            return Song.GenresToIgnore.Contains(genre);
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Team Leader
Australia Australia
Phillip has been playing with computers since the Apple II was the hottest home computer available. He learned the fine art of C programming and Guru meditation on the Amiga.

C# and Python are his languages of choice. Smalltalk is his mentor for simplicity and beauty. C++ is to programming what drills are to visits to the dentist.

He worked for longer than he cares to remember as Lead Programmer and System Architect of the Objective document management system. (www.objective.com)

He has lived for 10 years in northern Mozambique, teaching in villages.

He has developed high volume trading software, low volume FX trading software, and is currently working for Atlassian on HipChat.

Comments and Discussions