Spotify search api

Developer
Size
2,644 Kb
Views
24,288

How do I make an spotify search api?

What is a spotify search api? How do you make a spotify search api? This script and codes were developed by Jason Chan on 13 September 2022, Tuesday.

Spotify search api Previews

Spotify search api - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>spotify search api</title> <link rel='stylesheet prefetch' href='https://developer.spotify.com/web-api/static/css/cached.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class="container"> <h1>Search for an Artist</h1> <p>Type an artist name and click on "Search". Then, click on any album from the results to play 30 seconds of its first track.</p> <form id="search-form"> <input type="text" id="query" value="" class="form-control" placeholder="Type an Artist Name"/> <input type="submit" id="search" class="btn btn-primary" value="Search" /> </form> <div id="results"></div>
</div>
<script id="results-template" type="text/x-handlebars-template"> {{#each albums.items}} <div style="background-image:url({{images.0.url}})" data-album-id="{{id}}" class="cover"></div> {{/each}}
</script> <script src='https://code.jquery.com/jquery-2.2.4.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Spotify search api - Script Codes CSS Codes

body { padding: 20px;
}
#search-form, .form-control { margin-bottom: 20px;
}
.cover { width: 300px; height: 300px; display: inline-block; background-size: cover;
}
.cover:hover { cursor: pointer;
}
.cover.playing { border: 5px solid #e45343;
}

Spotify search api - Script Codes JS Codes

// find template and compile it
var templateSource = document.getElementById('results-template').innerHTML, template = Handlebars.compile(templateSource), resultsPlaceholder = document.getElementById('results'), playingCssClass = 'playing', audioObject = null;
var fetchTracks = function (albumId, callback) { $.ajax({ url: 'https://api.spotify.com/v1/albums/' + albumId, success: function (response) { callback(response); } });
};
var searchAlbums = function (query) { $.ajax({ url: 'https://api.spotify.com/v1/search', data: { q: query, type: 'album' }, success: function (response) { resultsPlaceholder.innerHTML = template(response); } });
};
results.addEventListener('click', function (e) { var target = e.target; if (target !== null && target.classList.contains('cover')) { if (target.classList.contains(playingCssClass)) { audioObject.pause(); } else { if (audioObject) { audioObject.pause(); } fetchTracks(target.getAttribute('data-album-id'), function (data) { audioObject = new Audio(data.tracks.items[0].preview_url); audioObject.play(); target.classList.add(playingCssClass); audioObject.addEventListener('ended', function () { target.classList.remove(playingCssClass); }); audioObject.addEventListener('pause', function () { target.classList.remove(playingCssClass); }); }); } }
});
document.getElementById('search-form').addEventListener('submit', function (e) { e.preventDefault(); searchAlbums(document.getElementById('query').value);
}, false);
Spotify search api - Script Codes
Spotify search api - Script Codes
Home Page Home
Developer Jason Chan
Username jasonchan
Uploaded September 13, 2022
Rating 3
Size 2,644 Kb
Views 24,288
Do you need developer help for Spotify search api?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Jason Chan (jasonchan) Script Codes
Create amazing web content with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!