Source

Ikulo / Server / Ikulo.Server.Core / GenpuidFingerprinter.cs

Full commit
// GenpuidFingerprinter.cs
//
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//                    Version 2, December 2004
//
// Copyright (C) 2008 Johannes Athmer
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
//  0. You just DO WHAT THE FUCK YOU WANT TO.
//
//  This program is free software. It comes without any warranty, to
//  the extent permitted by applicable law. You can redistribute it
//  and/or modify it under the terms of the Do What The Fuck You Want
//  To Public License, Version 2, as published by Sam Hocevar. See
//  http://sam.zoy.org/wtfpl/COPYING for more details.

namespace Ikulo.Server.Core
{
	using System;
	using System.Globalization;
	using System.IO;
	using System.Xml;
	
	/// <summary>
	/// Creates audio fingerprints using the <see cref="GenpuidWrapper"/>.
	/// </summary>
	public class GenpuidFingerprinter : IFingerprinter
	{
		/// <summary>
		/// Our <see cref="GenpuidWrapper"/>.
		/// </summary>
		private GenpuidWrapper genpuid_wrapper;
		
		/// <summary>
		/// Creates a new instance of the <see cref="GenpuidFingerprinter"/>.
		/// </summary>
		/// <param name="path">
		/// A <see cref="System.String"/>  which points to the executable of Genpuid.
		/// </param>
		/// <param name="musicDnsKey">
		/// MusicDNS key to use with Genpuid.
		/// </param>
		public GenpuidFingerprinter(string path,
		                            string musicDnsKey)
			: this(new FileInfo(path),
			       musicDnsKey)
		{
		}
		
		/// <summary>
		/// Creates a new instance of the <see cref="GenpuidFingerprinter"/>.
		/// </summary>
		/// <param name="genpuidExecutable">
		/// A <see cref="FileInfo"/> which points to the executable of Genpuid.
		/// </param>
		/// <param name="musicDnsKey">
		/// MusicDNS key to use with Genpuid.
		/// </param>
		public GenpuidFingerprinter(FileInfo genpuidExecutable,
		                            string musicDnsKey)
		{
            if (null == genpuidExecutable)
            {
                throw new ArgumentNullException("genpuidExecutable");
            }
            
			this.genpuid_wrapper = new GenpuidWrapper(genpuidExecutable,
			                                          musicDnsKey);
		}
		
		/// <summary>
		/// Creates an audio fingerprint of the file at
		/// the given <paramref name="path"/>.
		/// </summary>
		/// <param name="path">
		/// Path to the file to fingerprint.
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/> with the fingerprint.
		/// </returns>
		public virtual string CreatePrint(string path)
		{
			return this.CreatePrint(new FileInfo(path));
		}
		
		/// <summary>
		/// Creates an audio fingerprint of the given <paramref name="file"/>. 
		/// </summary>
		/// <param name="file">
		/// A <see cref="FileInfo"/> with audio data to fingerprint.
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/> with the fingerprint.
		/// </returns>
		public virtual string CreatePrint(FileInfo file)
		{
            if (null == file)
            {
                throw new ArgumentNullException("file");
            }
            
			XmlDocument xmlDocument = this.genpuid_wrapper.Analyze(file);
			XmlNodeList genpuid = xmlDocument.GetElementsByTagName("genpuid");			
			if (!genpuid.Count.Equals(1))
			{
				throw new FingerprintException("genpuid root element not found");
			}
			
			if (!genpuid[0].ChildNodes.Count.Equals(1))
			{
				throw new FingerprintException("genpuid does not have any tracks");
			}
			
			XmlNode xmlNode = genpuid[0].ChildNodes[0];
			
			if (xmlNode.Attributes.GetNamedItem("status") != null)
			{
				string status = xmlNode.Attributes.GetNamedItem("status").Value;
				throw new FingerprintException(string.Format(CultureInfo.CurrentCulture, "Status was returned: {0}", status));
			}
			
			string puid = xmlNode.Attributes.GetNamedItem("puid").Value;
			return puid;
		}
		
#region IFingerprinter member		
		
		/// <summary>
		/// Creates an audio fingerprint of the file at
		/// the given <paramref name="path"/>.
		/// </summary>
		/// <param name="path">
		/// Path to the file to fingerprint.
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/> with the fingerprint.
		/// </returns>
		string IFingerprinter.CreatePrint(string path)
		{
			return this.CreatePrint(path);
		}
		
		/// <summary>
		/// Creates an audio fingerprint of the given <paramref name="file"/>. 
		/// </summary>
		/// <param name="file">
		/// A <see cref="FileInfo"/> with audio data to fingerprint.
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/> with the fingerprint.
		/// </returns>
		string IFingerprinter.CreatePrint(FileInfo file)
		{
			return this.CreatePrint(file);
		}
		
#endregion
	}
}