Skip to content

Instantly share code, notes, and snippets.

@pol8139
Last active May 27, 2023 15:28
Show Gist options
  • Select an option

  • Save pol8139/4bfbe9f63479b284cfb9cdd222bf8f0a to your computer and use it in GitHub Desktop.

Select an option

Save pol8139/4bfbe9f63479b284cfb9cdd222bf8f0a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="noindex">
<title>ぽる研</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/main.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="//twemoji.maxcdn.com/2/twemoji.min.js?11.3"></script>
</head>
<body>
<style>
#album-info {
white-space: pre;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<div id="app">
<input type="text" v-model="albumInput" placeholder="Enter Spotify Album ID">
<button @click="getAlbumInfo">Submit</button>
<p id="album-info">{{ albumInfo }}</p>
</div>
<script>
const client_id = '';
const client_secret = '';
const getAccessToken = async () => {
const response = await fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + btoa(client_id + ':' + client_secret)
},
body: 'grant_type=client_credentials'
});
const data = await response.json();
return data.access_token;
}
new Vue({
el: '#app',
data: {
albumInput: '',
albumInfo: ''
},
methods: {
getAlbumId(input) {
const urlPattern = /^https:\/\/open\.spotify\.com\/album\/([\w\d]+)(\?.*)?$/;
const match = input.match(urlPattern);
if (match) {
return match[1];
} else {
return input;
}
},
async getAlbumInfo() {
const albumId = this.getAlbumId(this.albumInput);
const accessToken = await getAccessToken();
fetch(`https://api.spotify.com/v1/albums/${albumId}`, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
})
.then(response => response.json())
.then(data => {
this.albumInfo = `${data.artists[0].name}\t${data.name}\t${data.release_date}`;
});
}
}
});
</script>
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> -->
<!-- <script src="js/bootstrap.min.js"></script> -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment