stechico
3/22/2012 - 12:16 PM

Bookmarklet that generates download link for a Soundcloud upload

Bookmarklet that generates download link for a Soundcloud upload

(function(window) {
    var i, $sound, $buttonGroup;

    var $sounds = $(".sound");
    var clientId = require("config").get("client_id");
    var oauthToken = require("lib/connect").getAuthToken();
    var conversionHelper = require("lib/helpers/conversion-helper");
    var $downloadButton, size;
    var params, downloadUrl, onSuccess;

    for (i = $sounds.length - 1; i >= 0; i--) {
        $sound = $($sounds[i]);

        var soundcloudUrl = "https://soundcloud.com" + ($sound.find(".soundTitle__title").attr("href") || window.location.pathname);

        params = {
            url: soundcloudUrl,
            client_id: clientId
        };

        onSuccess = (function($sound) {
            return function(data) {
                var params = {
                  client_id: clientId
                };
                downloadUrl = require("lib/url").stringify({ query: params }, data.stream_url + ".mp3");

                $buttonGroup = $($sound.find(".sound__soundActions .sc-button-group")[0]);
                size = ($buttonGroup.find(".sc-button:first")[0].className).match(/sc-button-((?:small)|(?:medium))/)[1];

                $downloadButton = $('<a class="sc-button sc-button-download sc-button-icon sc-button-responsive">Download</a>').attr({
                  title: "Download this sound (" + conversionHelper.bytesToMB(data.original_content_size) + ")",
                  href: downloadUrl,
                  download:$sound.find(".soundTitle__title").first().text()+'.mp3'
                }).addClass("sc-button-" + size);

                if (0 == $sound.find(".sc-button-download").length) {
                    $buttonGroup.append($downloadButton);
                }
            };
        })($sound);

        $.getJSON("http://api.soundcloud.com/resolve.json", params).success(onSuccess);
    }
})(window);
(function(window){var i,$sound,$buttonGroup;var $sounds=$(".sound");var clientId=require("config").get("client_id");var oauthToken=require("lib/connect").getAuthToken();var conversionHelper=require("lib/helpers/conversion-helper");var $downloadButton,size;var params,downloadUrl,onSuccess;for(i=$sounds.length-1;i>=0;i--){$sound=$($sounds[i]);var soundcloudUrl="https://soundcloud.com"+($sound.find(".soundTitle__title").attr("href")||window.location.pathname);params={url:soundcloudUrl,client_id:clientId};onSuccess=function($sound){return function(data){var params={client_id:clientId};downloadUrl=require("lib/url").stringify({query:params},data.stream_url+".mp3");$buttonGroup=$($sound.find(".sound__soundActions .sc-button-group")[0]);size=$buttonGroup.find(".sc-button:first")[0].className.match(/sc-button-((?:small)|(?:medium))/)[1];$downloadButton=$('<a class="sc-button sc-button-download sc-button-icon sc-button-responsive">Download</a>').attr({title:"Download this sound ("+conversionHelper.bytesToMB(data.original_content_size)+")",href:downloadUrl,download:$sound.find(".soundTitle__title").first().text()+".mp3"}).addClass("sc-button-"+size);if(0==$sound.find(".sc-button-download").length){$buttonGroup.append($downloadButton)}}}($sound);$.getJSON("http://api.soundcloud.com/resolve.json",params).success(onSuccess)}})(window);