Skip to content

Instantly share code, notes, and snippets.

@duncanbeevers
Forked from pheuter/sc-dl.js
Created March 22, 2012 12:16
Show Gist options
  • Save duncanbeevers/2157987 to your computer and use it in GitHub Desktop.
Save duncanbeevers/2157987 to your computer and use it in GitHub Desktop.
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);
@haroldSanchezb
Copy link

hi @markpiro i added HTML5 download Attribute,

download:$sound.find(".soundTitle__title").first().text()+'.mp3'

inside of $downloadButton

Greetings!

@marcandrebenoit
Copy link

@haroldSanchezb can you tell me where your code goes exactly ?

post your updated version if possible or just the updated $downloadButton version that you have :)

Thanks!

@haroldSanchezb
Copy link

hello @marcandrebenoit
apology for the delay, with the option that i said, would look like:

(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);

@duncanbeevers
Copy link
Author

@haroldSanchezb @marcandrebenoit Updated to the latest version posted by @haroldSanchezb. These changes are great.

@shrwnsan
Copy link

shrwnsan commented Apr 8, 2014

This still working for people? I tried recently and I get a "Failed - No file" via Chrome. Thanks in advance.

@haroldSanchezb @duncanbeevers

@haroldSanchezb
Copy link

hi @stechico still working, but remember, only works in single songs no in continuous songs, or playlists on stream.

@GustavoHahn
Copy link

Hello, Anyone know how I can use your code and put it in a contextmenu of chrome?

@shrwnsan
Copy link

Thanks @haroldSanchezb, I'll keep that in mind an retest. Cheers!

@haroldSanchezb
Copy link

@GustavoHahn is possible, simply set up your context menu that captures the link by pressing right click, and send that url to a script that makes the function of generating the link to download.

Greetings!

Copy link

ghost commented Jul 3, 2014

It seems that this works, although the filenames are just a string of letters and numbers with 128 at the end showing their bit rate. Any reason why I am running into this issue?

@thibmaek
Copy link

Doesn't work in Safari with newest SoundCloud

@Korayem
Copy link

Korayem commented Oct 11, 2014

This doesn't work on chrome. I guess soundcloud are changing their protection mechanisms.

@kjvarga
Copy link

kjvarga commented Oct 19, 2014

'require' is a missing reference. Perhaps SC is not using requireJS anymore?

@aleemb
Copy link

aleemb commented Nov 9, 2014

Yup, broken now. Is there a quick fix?

@haroldSanchezb
Copy link

Ok, i will check tonight

@kjvarga
Copy link

kjvarga commented Dec 1, 2014

Anything to report?

@hoang-himself
Copy link

Time to give this an update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment