SoundCloud for Developers

Discover, connect and build

Backstage Blog March 5th, 2012 Announcements Email Unlock - Development Unlocked By leemartin

Email Unlock

Email Unlock is a free hosted SoundCloud Labs app that allows you to give fans exclusive downloads and get their email addresses in return. Here's a little insight on how it was developed.

 

Backend

Email Unlock is a Rails 3.1 app hosted on Heroku's Cedar stack. It uses the OmniAuth SoundCloud strategy to power user authentication and the lightweight Sendgrid SMTP API to send emails. It relies on Heroku's PostgreSQL database for basic storage needs, and Dalli to interface with memcache for caching. SoundCloud's API powers all streaming and downloading.

 

Frontend

The main templating language used throughout is Haml for it's clean and highly versatile syntax. All styling is powered by a combination of Sass and Compass to maximize cross-browser support. The Draper gem is used to clean up complex Rails views. The Email Unlock widget itself was constructed very simply as a custom player with jQuery and SoundManager2.

 

Stories

Here's a few expanded stories on how the SoundCloud platform was used.

Connecting with SoundCloud using Omniauth

As stated above, OmniAuth 1.0 was used to authenticate SoundCloud users. All you need to do is setup the gem and rack middleware, and create a simple Sessions controller to deal with logging in & logging out. These users are stored in a light-weight database, and are associated with the campaigns they create. This gives them authenticated access to export the emails their campaigns obtain.

Getting Tracks from SoundCloud and Creating Campaigns

Once the user is logged in properly, the SoundCloud Ruby Gem is used to pull in tracks:

soundcloud.get("/me/tracks.json")

Then, upon campaign creation, some of the track data is cached as part of the campaign. Finally, I use a bit of rmagick to create a large blurred background image using the track's artwork, and lazily store that as a Base64 encoded text in my database.

img = Magick::Image.read(artwork_url).first
img.resize!(1500, 1500, Magick::GaussianFilter, 5.0)
self.background = Base64.encode64(img.to_blob)

Smoke and mirrors? You bet.

Servicing the SoundCloud Download

How does one get a download of a non-downloadable track? Easy: OAuth. Storing your non-expiring access token gives Email Unlock access to your tracks as needed. In this case, a user has confirmed their email address and wishes to download your campaign track. For an instant, that users becomes you and is handed the download in a secure exchange:

user.soundcloud.get("/tracks/#{track_id}/download", {}, :follow_redirects => false) rescue $!.response.headers["location"]

 

Thanks

Got any technical questions I didn't cover here? Drop me a line at lee@soundcloud.com or contact me on Twitter @leemartin.