Projects: Snarl & Last.FM
Long story: My iPod touch notifies a website called Last.FM when I listen to music. If your not familiar with LastFM here’s a quick run down: Last.FM allows you to “scrobble” the names/artist of songs you listen to, and share this information with anyone an the internet. So if you go here you can see what music I have recently listened to on my iPod/computer.
So being the huge geek I am, I wrote a quick C# application that watches Last.FM for updates to my “Recently Played Tracks”, once it finds a change, it notifies me on my work desktop in a nice little bubble (see picture above).
The bubble is done with a program called Snarl, which aims to replicate GROWL functionality (an OSX notification subsystem) on Windows. (This is very hit or miss, mostly miss.)
So why did I do this? Too lazy to look at the iPod to see what the track names are ;-)
Below is the source code for the quick and dirty C# application, that makes use of external process launching to send messages to snarl, and WebClient to retrieve updates from Last.FM
using System;
using System.Net;
using System.IO;
namespace snarl_lastfm_test
{
class Program
{
/**
* Send a message to snarl
*/
public static void sendMessage(string title, string message) {
//Sending message to snarl
executeCommand("snarl_command.exe /M \""+title+"\" \""+message+"\" /T 10", 800);
}
/**
* Execute a command via cmd.exe
*/
public static int executeCommand(string cmd, int millsecTimeOut) {
System.Diagnostics.ProcessStartInfo processStartInfo =
new System.Diagnostics.ProcessStartInfo("CMD.exe", "/C "+cmd);
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;
System.Diagnostics.Process process =
System.Diagnostics.Process.Start(processStartInfo);
//wait for timeout before asking for return code
process.WaitForExit(millsecTimeOut);
int exitCode = process.ExitCode;
process.Close();
return exitCode;
}
public static void Main(string[] args)
{
Console.WriteLine("Listening for last.fm updates...");
string lastSong = "";
while (true) {
// check url for last played song
// Address of URL
string URL = "http://ws.audioscrobbler.com/1.0/user/npike/recenttracks.txt";
string str = "";
WebClient client = null;
try
{
// Get HTML data
client = new WebClient();
Stream data = client.OpenRead(URL);
StreamReader reader = new StreamReader(data);
str = reader.ReadLine();
data.Close();
}
catch(WebException exp)
{
MessageBox.Show(exp.Message, "Exception");
}
// is last played song different then the last recorded one?
// clean up last played song entry
//1197581437,Puscifer – Rev 22'20 (Dry Martini Mix)
if (str != null) {
int pos = str.IndexOf(",");
str = str.Substring(pos+1);
// song is different, send snarl message and update
if (lastSong != str) {
lastSong = str;
sendMessage("snarl_lastfm_test", "Just Listened To: "+lastSong);
Console.WriteLine(" Song changed: "+lastSong);
}
} else {
Console.WriteLine(" Null message received from Last.FM");
}
System.Console.WriteLine(System.DateTime.Now);
System.Threading.Thread.Sleep(40000);
}
}
}
}
new jew kicks ass.
adam
27 Dec 07 at 10:11 am
Thanks!
I was just looking for a way to make notifications from my application using Snarl and C#, but I couldn’t find any code-example for C# on the Snarl website ( http://www.fullphat.net ). Executing snarl_command.exe is perhaps not the most elegant way to do it, but it works as a workaround util they can release a DLL or code exemple in C# to send a Window Message.
Salle
27 Jan 08 at 11:18 am
Just found a C# example for Snarl (using Windows Messages)! It can be downloaded from: http://www.k23productions.com/e107_files/public/1174085516_471_FT2790_snarlconnector.zip
And you can read the thread about it here:
http://www.k23productions.com/e107_plugins/forum/forum_viewtopic.php?2790.30
Salle
27 Jan 08 at 11:38 am