Question

I'd like to display the artwork image of a soundcloud track I have the Track-URL from.

I know that when I extract the track key/id from the URL, I can request the artwork URL via their API and then inject the retrieved URL into a image tag.

What I'd like to know is if its possible to use some kind of URL schema to make soundcloud forward browsers to the correct artwork URL like its possible with facebook profile images.

For example:

Mark Zuckerbergs current profile picture has the URL http://profile.ak.fbcdn.net/hprofile-ak-prn2/t5/202896_4_1782288297_q.jpg

Thats some cryptic stuff because its hosted on a CDN. Soundcloud artwork URLs look pretty much cryptic as well.

Now, when I know marks facebook id/key ("zuck"), I can simply access his profile image like so:

http://graph.facebook.com/zuck/picture

That URL is automatically forwarded to the profile picture URL by the facebook API.

Using this URL schema you abstract away not only the reason for a additional API request, but they also safe processing time on their side.

Is there some functionality like this for soundcloud track artworks?

Was it helpful?

Solution 2

No. The only documented way is: API Reference for "/tracks"

OTHER TIPS

I wrote an express app that redirects to largest available image on their CDN, given artwork_url.

FixSoundCloudArtworkUrl.js

It uses their naming scheme and enumerates sizes one by one until some image returns status 200.

Source:

'use strict';

var express = require('express'),
    app = express();

require('./config/development')(app, express);
require('./config/production')(app, express);

var redis = require('redis'),
    request = require('request'),
    Promise = require('bluebird');

Promise.promisifyAll(redis.RedisClient.prototype);

var redisSettings = app.set('redis'),
    redisClient = redis.createClient(redisSettings.port, redisSettings.host, redisSettings.options);

app.configure(function () {
  app.use(express.bodyParser());
  app.use(app.router);
});

function sendError(res, status, error) {
  if (!(error instanceof Error)) {
    error = new Error(JSON.stringify(error));
  }

  return res
    .status(status || 500)
    .end(error && error.message || 'Internal Server Error');
}

function generateCacheHeaders() {
  var maxAge = 3600 * 24 * 365;

  return {
    'Cache-Control': 'public,max-age=' + maxAge,
    'Expires': new Date(Date.now() + (maxAge * 1000)).toUTCString()
  };
}

function getCacheKey(url) {
  return 'soundcloud-thumbnail-proxy:' + url;
}

app.get('/*', function (req, res) {
  var originalUrl = req.params[0],
      cacheKey = getCacheKey(originalUrl),
      urls;

  // https://developers.soundcloud.com/docs/api/reference#artwork_url
  // This is a ridiculous naming scheme, by the way.

  urls = [
    originalUrl,
    originalUrl.replace('-large', '-t500x500'),
    originalUrl.replace('-large', '-crop'), // 400x400
    originalUrl.replace('-large', '-t300x300'),
    originalUrl.replace('-large', '-large') // 100x100
  ];

  return redisClient.getAsync(cacheKey).then(function (cachedUrl) {
    if (cachedUrl) {
      return cachedUrl;
    }

    return Promise.reduce(urls, function (resolvedUrl, url) {
      if (resolvedUrl) {
        return resolvedUrl;
      }

      return new Promise(function (resolve) {
        request.head(url, function (err, response) {
          if (!err && response.statusCode === 200) {
            resolve(url);
          } else {
            resolve(null);
          }
        });
      });
    }, null);
  }).then(function (url) {
    if (!url) {
      throw new Error('File not found');
    }

    var headers = generateCacheHeaders();
    for (var key in headers) {
      if (headers.hasOwnProperty(key)) {
        res.setHeader(key, headers[key]);
      }
    }

    res.redirect(url);
    redisClient.set(cacheKey, url);
    redisClient.expire(cacheKey, 60 * 60 * 24 * 30);

  }).catch(function (err) {
    sendError(res, 404, err);
  });

});

app.get('/crossdomain.xml', function (req, res) {
  req.setEncoding('utf8');
  res.writeHead(200, { 'Content-Type': 'text/xml' });
  res.end('<?xml version="1.0" ?><cross-domain-policy><allow-access-from domain="*" /></cross-domain-policy>');
});

redisClient.on('ready', function () {
  app.listen(app.set('port'));
});

redisClient.on('error', function () {
  throw new Error('Could not connect to Redis');
});

module.exports = app;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top