SoundCloud: Best developer key – EVER!

Some weeks ago, i made a post about VEOH.com and the developer they hid in the source code of their very own site.

This time i was investigating SoundCloud. It’s something like YouTube for music and they got this pretty neat comment feature, where you can pin your comment to a certain time in the track. Here is some example for you – listen to it while reading the rest of this post ;)


They also propagate the development of 3rd party apps and their for offer a rich and easy to use API. But once more you are forced to register yourself as an official developer to get access to a client id, which effectively is their name for developer key. So while reading their API documentation, I saw a URL like this:

…/tracks/43110396.json?client_id=YOUR_CLIENT_ID

And thought… “where to get this #*!$##! key from…” never the less i copied the URL in my browser and… i worked! Stunned by this, my first idea was, they only use the key for statistics, but nay. If you change the client_id parameter to something else, you only get:

{"errors":[{"error_message":"401 - Unauthorized"}]}

So development with YOUR_CLIENT_ID went on and until now i never faced any problems using this special developer key :) – Thanks SoundCloud!

Observe your clipboard – like a BOSS!

Observing the users clipboard in C#… yeha, no big deal i thought in the first place. .NET comes with this nice Clipboard class, that has a bunch of static methods for reading and writing all kinds of clipboard data. But that’s it. No events to hook, no way to get notified when the content of the clipboard changes. dafuq?

Time to ask google, but the result is pretty unpleasant. Actual there are two ways to observe the users clipboard and both of them rely on win32 API calls and need a window handle…

Solution #1
This one works for all version of Windows but ist the more ugly one. The observers are organized as a liked list. But the catch is, every observer needs to manage the reference of it’s follower on it’s own. So after adding yourself as a listener / observer / viewer you get the handle of the next observer. After receiving a WM_DRAWCLIPBOARD message through the WndProc method, you must forward the message using SendMessage. When a application unregisters itself, you receive a WM_DRAWCLIPBOARD message and must probably re-organize your reference to the next observer. Long story short, no matter if your code is correct, if someone else has written bad code, the whole list gets messed up and your application is going down.

private const int WM_DRAWCLIPBOARD = 0x308;
private const int WM_CHANGECBCHAIN = 0x030D;

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);

Solution #2
It took some a long time, till Microsoft realized that this was horrible API design. On Windows Vista they came up with an alternative. This one is straight forward. Using Add/RemoveClipboardFormatListener you register your window (yeha, you always need one…) for receiving WM_CLIPBOARDUPDATE messages. That’s it.

private const int WM_CLIPBOARDUPDATE = 0x031D;

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool AddClipboardFormatListener(IntPtr hWndListener);

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool RemoveClipboardFormatListener(IntPtr hWndListener);

Solution #3
Something like this… *smiliethatpukesoverakeyboard*

var observerThread = new Thread(() =>
{
    string lastText;
    while (!pleaseKillMe)
    {
        if (!lastText.Equals(Clipboard.GetText()))
        {
            /* notify */
            lastText = Clipboard.GetText();
        }
    }
});
observerThread.SetApartmentState(ApartmentState.STA);
observerThread.Start();

While using solution #3 in the first place, i could not bear how dead ugly it was. So i took solution 1+2, added a dummy window (what is nearly as ugly as solution #3…), an automatic version switch to use the more sable & sexy API on newer windows machines and packed it all together into a small reusable component. Now observing the clipboard is as easy as i thought in the first place :)

var clipboardObserver = new ClipboardObserver();
clipboardObserver.ClipboardTextChanged += newText => Console.WriteLine(newText);

Want to save an hour of your time and simply use the component instead or reinventing it? Or extend it to listen on other clipboard data such as Audio / Image / DataObject?

Download VS10 Solution

About that developer key…

Currently i’m working on VEOH.com integration for our software TubeBox (was mine, now sold, whatsoever). TubeBox is search / download / convert tool for video sites like YouTube, Vimeo, Metacafe, etc.

And every time you start implementing a new provider you cross your fingers and search for “[Provider-Name-Here] API”. Most providers have beautiful Web 3.0 sites and offer a public API to create mesh ups and get more reputation.

And so does VEOH.com. They offer a REST API full of awesome methods to search videos an query all kind of helpful information. Nice.

But wait… API Key needed? Developer Registration? Become Business Partner? No way my bros from oversea! This is not an option. A Web 3.0 API should be public! Really.

So lets get back to basics and parse plain HTML with the power of regular expressions… Boring.

So i opened a video from their site and started studying the source code, when suddenly out of nowhere *le wild line 533 appeared:

__api.apiKey = “E97FCECD-875D-D5EB-035C-8EF241F184E2”;

Could it be? They use their API themselves and are also registered as developers and business partner of… themselves? Let’s check it out…

Yep. They really are! So let’s close our favourite RegularExpression Designer and have a look at the API again :) – Thanks VEOH!