Я успешно получил Spotify API для работы с spotify-web-api- js, но я не знаю, как это декодировать. Я хочу иметь возможность отображать название и исполнителя песни, которая в настоящее время воспроизводится на странице HTML.
Прямо сейчас для проверки я пытаюсь использовать <script>document.getElementById("song").innerHTML = data.timestamp</script>
(data
- это материал из API Spotify), чтобы получить метку времени, так как я не знаю, в каком формате go глубже в ответ Spotify. Я получаю Uncaught TypeError: Cannot read property 'timestamp' of undefined
.
Примечание: я новичок в кодировании
Вот javascript, который получает данные от Spotify:
var SpotifyWebApi = require('spotify-web-api-js');
var spotifyApi = new SpotifyWebApi();
var data;
spotifyApi.setAccessToken('token')
spotifyApi.getMyCurrentPlayingTrack('title')
.then(function(data) {
console.log('Now playing', data);
}, function(err) {
console.error(err);
});
Вот часть html веб-страницы который должен отображать название песни (в данном случае метку времени для целей тестирования):
<body>
<div class=center>
<h1 id="song"></h1>
<h2>Luna Li</h2>
<script>document.getElementById("song").innerHTML = data.timestamp</script>
</div>
</body>
Ответ от spotify:
{
"timestamp" : 1592617721303,
"context" : {
"external_urls" : {
"spotify" : "https://open.spotify.com/album/3GU8BzFEAdFSRjc8jZkL3S"
},
"href" : "https://api.spotify.com/v1/albums/3GU8BzFEAdFSRjc8jZkL3S",
"type" : "album",
"uri" : "spotify:album:3GU8BzFEAdFSRjc8jZkL3S"
},
"progress_ms" : 27470,
"item" : {
"album" : {
"album_type" : "compilation",
"artists" : [ {
"external_urls" : {
"spotify" : "https://open.spotify.com/artist/0LyfQWJT6nXafLPZqxe9Of"
},
"href" : "https://api.spotify.com/v1/artists/0LyfQWJT6nXafLPZqxe9Of",
"id" : "0LyfQWJT6nXafLPZqxe9Of",
"name" : "Various Artists",
"type" : "artist",
"uri" : "spotify:artist:0LyfQWJT6nXafLPZqxe9Of"
} ],
"available_markets" : [ "AD", "AE", "AR", "AT", "AU", "BE", "BG", "BH", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "KW", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO", "SA", "SE", "SG", "SK", "SV", "TH", "TN", "TR", "TW", "US", "UY", "VN", "ZA" ],
"external_urls" : {
"spotify" : "https://open.spotify.com/album/3GU8BzFEAdFSRjc8jZkL3S"
},
"href" : "https://api.spotify.com/v1/albums/3GU8BzFEAdFSRjc8jZkL3S",
"id" : "3GU8BzFEAdFSRjc8jZkL3S",
"images" : [ {
"height" : 640,
"url" : "https://i.scdn.co/image/ab67616d0000b2730bdf4dd39843ad48c5b66bc4",
"width" : 640
}, {
"height" : 300,
"url" : "https://i.scdn.co/image/ab67616d00001e020bdf4dd39843ad48c5b66bc4",
"width" : 300
}, {
"height" : 64,
"url" : "https://i.scdn.co/image/ab67616d000048510bdf4dd39843ad48c5b66bc4",
"width" : 64
} ],
"name" : "La La Land (Original Motion Picture Soundtrack)",
"release_date" : "2016-12-09",
"release_date_precision" : "day",
"total_tracks" : 15,
"type" : "album",
"uri" : "spotify:album:3GU8BzFEAdFSRjc8jZkL3S"
},
"artists" : [ {
"external_urls" : {
"spotify" : "https://open.spotify.com/artist/6kXm2YCtdUOpRYNKeKhfue"
},
"href" : "https://api.spotify.com/v1/artists/6kXm2YCtdUOpRYNKeKhfue",
"id" : "6kXm2YCtdUOpRYNKeKhfue",
"name" : "Ryan Gosling",
"type" : "artist",
"uri" : "spotify:artist:6kXm2YCtdUOpRYNKeKhfue"
}, {
"external_urls" : {
"spotify" : "https://open.spotify.com/artist/68Uwx1d3EbjDJhYsLjqvIq"
},
"href" : "https://api.spotify.com/v1/artists/68Uwx1d3EbjDJhYsLjqvIq",
"id" : "68Uwx1d3EbjDJhYsLjqvIq",
"name" : "Emma Stone",
"type" : "artist",
"uri" : "spotify:artist:68Uwx1d3EbjDJhYsLjqvIq"
} ],
"available_markets" : [ "AD", "AE", "AR", "AT", "AU", "BE", "BG", "BH", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "KW", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO", "SA", "SE", "SG", "SK", "SV", "TH", "TN", "TR", "TW", "US", "UY", "VN", "ZA" ],
"disc_number" : 1,
"duration_ms" : 236506,
"explicit" : false,
"external_ids" : {
"isrc" : "USUG11600652"
},
"external_urls" : {
"spotify" : "https://open.spotify.com/track/4r9hiElqKWMPT4Z3vN2exq"
},
"href" : "https://api.spotify.com/v1/tracks/4r9hiElqKWMPT4Z3vN2exq",
"id" : "4r9hiElqKWMPT4Z3vN2exq",
"is_local" : false,
"name" : "A Lovely Night",
"popularity" : 62,
"preview_url" : "https://p.scdn.co/mp3-preview/b315c0bd334edba2f571a4d5e1986ae544d8cf1b?cid=1f307ec00d7b4f3186cb56d56162cab6",
"track_number" : 4,
"type" : "track",
"uri" : "spotify:track:4r9hiElqKWMPT4Z3vN2exq"
},
"currently_playing_type" : "track",
"actions" : {
"disallows" : {
"resuming" : true
}
},
"is_playing" : true
}
Есть ли простой способ получить название песни и исполнитель из этого? Я чувствую, что вы можете сделать это с помощью <script>document.getElementById("song").innerHTML = data.timestamp</script>
, но я не уверен, как перемещаться по ответу Spotify.