如何使用API​​获取SoundCloud的缩略图?

时间:2012-02-01 12:51:12

标签: soundcloud

例如:

http://soundcloud.com/mhiqu3/sets/heretik/

如何获取缩略图网址:http://i1.sndcdn.com/artworks-000004897289-5r6ws9-large.jpg?1767646

使用api(或不使用)?

5 个答案:

答案 0 :(得分:23)

我知道这已经很老了,但是我偶然发现了这个问题寻找其他东西,我想给我两分钱。

这些都是可能的尺寸(来源API Reference):

        * t500x500:     500×500
        * crop:         400×400
        * t300x300:     300×300
        * large:        100×100 (default)
        * t67x67:       67×67    (only on artworks)
        * badge:        47×47
        * small:        32×32
        * tiny:         20×20    (on artworks)
        * tiny:         18×18    (on avatars)
        * mini:         16×16
        * original:     (originally uploaded image)

答案 1 :(得分:9)

另外,如果将url字符串中的“-large”替换为“-t300x300”或“-t500x500”,您将可以访问更大的格式。

答案 2 :(得分:7)

您可以使用/tracks端点,并从返回的JSON中读取artwork_url属性。更多详情:http://developers.soundcloud.com/docs/api/tracks

答案 3 :(得分:4)

你想要做的是使用Soundclouds API,如下所示:

SC.get(PATH, function (track, err){
  IMG_URL = track.artwork;
});

你可以在这里看到一个例子: http://runnable.com/UuiMCfATvMMkAAAS/get-a-tracks-artwork-on-soundcloud-in-javascript#

可以通过不同的方式获得轨道的PATH 1.如果您知道图像ID,您可以像这样抓住它 var PATH = "/tracks" + id;
2.如果你知道赛道的名称和用户
var PATH = "/users/dj-faze/tracks/phase-shift-360-019-degrees"
3.您还可以使用其他soundcloud API

获取id

注意:确保您尝试获取的曲目图像是公开的。如果它是私有的,您将必须使用soundcloud授权API登录。

答案 4 :(得分:3)

我尝试使用this answer但遗憾的是,SoundCloud似乎无法保证t500x500original或其他尺寸不存在。

因此,我使用artwork_url编写了一个快速应用程序,该应用程序重定向到其CDN上的最大可用图像。

<强> https://gist.github.com/gaearon/717ed6d6c7771f95d50d

它使用它们的命名方案并逐个枚举大小,直到某些图像返回状态200。

来源:

'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;