为什么这样做? Spotify API调用node.js

时间:2019-01-23 04:01:22

标签: node.js spotify

我是Node的新手,正在通过node.js进行API调用,对此感到有些困惑。我已经通过节点轻松完成了其他API调用,因为很容易弄清楚如何定位各个字段等。但是我从未与Spotify API链接,并且对data.tracks.items.artists.name的用法感到困惑我是艺术家的名字吗?

我知道这是一个无知的问题,但我真的想了解它的工作原理,而不仅仅是使它起作用。

function song() {
var nodeArgs = process.argv;
var SongName = "";
for (var i = 3; i < nodeArgs.length; i++) {
    if (i > 3 && i < nodeArgs.length) {
        SongName = SongName + "+" + nodeArgs[i];
    }
    else {
        SongName += nodeArgs[i];
    }
}

var Spotify = require('node-spotify-api');
var spotify = new Spotify({
    id: "id",
    secret: "secret"
});

spotify.search({ type: 'track', query: SongName, limit: 1 }, function (err, data) {
    if (err) {
        SongName = "";
        console.log("Artist: " + songData.artists[0].name);
        console.log("Song Title: " + songData.name);
        console.log("Preview Track: " + songData.preview_url);
        console.log("Album: " + songData.album.name);
        song();
    }

    for (var i = 0; i < data.tracks.items.length; i++) {
        var songData = data.tracks.items[i];
        console.log("Artist: " + songData.artists[0].name);
        console.log("Song Title: " + songData.name);
        console.log("Preview Track: " + songData.preview_url);
        console.log("Album: " + songData.album.name);
    }
});
}

1 个答案:

答案 0 :(得分:1)

简短答案- 跟踪api端点使用Object Model进行响应,其中name也包含艺术家对象-它是艺术家对象的数组,其中艺术家对象包含键KEY VALUE | TYPE | VALUE DESCRIPTION --- artists | an array of simplified | The artists who performed the track. | artist objects | information about the artist.

ref:https://developer.spotify.com/documentation/web-api/reference/tracks/get-track/

从他们的API文档中

  

获取https://api.spotify.com/v1/tracks/ {id}

响应对象包含

artist object (simplified)

KEY VALUE       | TYPE                   | VALUE DESCRIPTION
---
external_urls   | an external URL object | Known external URLs for this artist.
href            | string                 | A link to the Web API endpoint providing full details of the artist.
id              | string                 | The Spotify ID for the artist.
name            | string                 | The name of the artist
type            | string                 | The object type: "artist"
uri             | string                 | The Spotify URI for the artist.

艺术家对象

from django.core.exceptions import ValidationError

class YourModel(models.Model):
    ...
    def clean(self):
       if self.something is 'wrong':
           raise ValidationError("Something is wrong")

    def save(self, *args, **kwargs):
        self.full_clean()
        return super(YourModel, self).save(*args, **kwargs)