241 lines
8.2 KiB
HTML
241 lines
8.2 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<title>Spotify Duplicate Finder</title>
|
|
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
|
|
<style type="text/css">
|
|
#login, #loggedin {
|
|
display: none;
|
|
}
|
|
|
|
.text-overflow {
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
width: 500px;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="container">
|
|
<div id="login">
|
|
<h1>This is an example of the Authorization Code flow</h1>
|
|
<a href="/login" class="btn btn-primary">Log in with Spotify</a>
|
|
</div>
|
|
<div id="loggedin">
|
|
<div id="user-profile">
|
|
</div>
|
|
<div id="oauth">
|
|
</div>
|
|
<button class="btn btn-default" id="obtain-new-token">Obtain new token using the refresh token</button>
|
|
|
|
<h1>Duplicates finder</h1>
|
|
<div id="dups">
|
|
</div>
|
|
<div>
|
|
<button class="btn btn-default" id="get-playlists">Get playlists</button>
|
|
<div id="playlists">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script id="user-profile-template" type="text/x-handlebars-template">
|
|
<h1>Logged in as {{display_name}}</h1>
|
|
<div class="media">
|
|
<div class="pull-left">
|
|
<img class="media-object" width="150" src="{{images.0.url}}"/>
|
|
</div>
|
|
<div class="media-body">
|
|
<dl class="dl-horizontal">
|
|
<dt>Display name</dt>
|
|
<dd class="clearfix">{{display_name}}</dd>
|
|
<dt>Id</dt>
|
|
<dd>{{id}}</dd>
|
|
<dt>Spotify URI</dt>
|
|
<dd><a href="{{external_urls.spotify}}">{{external_urls.spotify}}</a></dd>
|
|
<dt>Link</dt>
|
|
<dd><a href="{{href}}">{{href}}</a></dd>
|
|
<dt>Profile Image</dt>
|
|
<dd class="clearfix"><a href="{{images.0.url}}">{{images.0.url}}</a></dd>
|
|
<dt>Country</dt>
|
|
<dd>{{country}}</dd>
|
|
</dl>
|
|
</div>
|
|
</div>
|
|
</script>
|
|
|
|
<script id="oauth-template" type="text/x-handlebars-template">
|
|
<h2>oAuth info</h2>
|
|
<dl class="dl-horizontal">
|
|
<dt>Access token</dt>
|
|
<dd class="text-overflow">{{access_token}}</dd>
|
|
<dt>Refresh token</dt>
|
|
<dd class="text-overflow">{{refresh_token}}></dd>
|
|
</dl>
|
|
</script>
|
|
|
|
<script id="dups-template" type="text/x-handlebars-template">
|
|
<h2>Duplicates in {{pl_name}}</h2>
|
|
{{message}}
|
|
{{#list dups}}{{dup_trackname}} - {{dup_artist}}{{/list}}
|
|
</script>
|
|
|
|
<script id="playlists-template" type="text/x-handlebars-template">
|
|
<h2>Playlists</h2>
|
|
{{#list playlists}}<a class="pl_item" href="/pl/{{pl_uid}}/{{pl_id}}">{{pl_name}}</a>{{/list}}
|
|
</script>
|
|
|
|
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0-alpha.1/handlebars.min.js"></script>
|
|
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
|
|
<script>
|
|
(function () {
|
|
|
|
/**
|
|
* Obtains parameters from the hash of the URL
|
|
* @return Object
|
|
*/
|
|
function getHashParams() {
|
|
var hashParams = {};
|
|
var e, r = /([^&;=]+)=?([^&;]*)/g,
|
|
q = window.location.hash.substring(1);
|
|
while (e = r.exec(q)) {
|
|
hashParams[e[1]] = decodeURIComponent(e[2]);
|
|
}
|
|
return hashParams;
|
|
}
|
|
|
|
Handlebars.registerHelper('list', function (items, options) {
|
|
var out = "<ul>";
|
|
|
|
for (var i = 0, l = items.length; i < l; i++) {
|
|
out = out + "<li>" + options.fn(items[i]) + "</li>";
|
|
}
|
|
|
|
return out + "</ul>";
|
|
});
|
|
|
|
|
|
var userProfileSource = document.getElementById('user-profile-template').innerHTML,
|
|
userProfileTemplate = Handlebars.compile(userProfileSource),
|
|
userProfilePlaceholder = document.getElementById('user-profile');
|
|
|
|
var oauthSource = document.getElementById('oauth-template').innerHTML,
|
|
oauthTemplate = Handlebars.compile(oauthSource),
|
|
oauthPlaceholder = document.getElementById('oauth');
|
|
|
|
var playlistsSource = document.getElementById('playlists-template').innerHTML,
|
|
playlistsTemplate = Handlebars.compile(playlistsSource),
|
|
playlistsPlaceholder = document.getElementById('playlists');
|
|
|
|
var dupsSource = document.getElementById('dups-template').innerHTML,
|
|
dupsTemplate = Handlebars.compile(dupsSource),
|
|
dupsPlaceholder = document.getElementById('dups');
|
|
|
|
var params = getHashParams();
|
|
|
|
var access_token = params.access_token,
|
|
refresh_token = params.refresh_token,
|
|
error = params.error;
|
|
|
|
if (error) {
|
|
alert('There was an error during the authentication');
|
|
} else {
|
|
if (access_token) {
|
|
// render oauth info
|
|
oauthPlaceholder.innerHTML = oauthTemplate({
|
|
access_token: access_token,
|
|
refresh_token: refresh_token
|
|
});
|
|
|
|
$.ajax({
|
|
url: 'https://api.spotify.com/v1/me',
|
|
headers: {
|
|
'Authorization': 'Bearer ' + access_token
|
|
},
|
|
success: function (response) {
|
|
userProfilePlaceholder.innerHTML = userProfileTemplate(response);
|
|
|
|
$('#login').hide();
|
|
$('#loggedin').show();
|
|
}
|
|
});
|
|
} else {
|
|
// render initial screen
|
|
$('#login').show();
|
|
$('#loggedin').hide();
|
|
}
|
|
|
|
document.getElementById('get-playlists').addEventListener('click', function () {
|
|
$.ajax({
|
|
url: '/get_playlists',
|
|
data: {
|
|
'access_token': access_token
|
|
}
|
|
}).done(function (data) {
|
|
var pl = data.data.map(function (item, index, array) {
|
|
return {
|
|
pl_uid : item.owner.id,
|
|
pl_name : item.name,
|
|
pl_id : item.id
|
|
}
|
|
});
|
|
|
|
playlistsPlaceholder.innerHTML = playlistsTemplate({
|
|
playlists: pl
|
|
});
|
|
})
|
|
}, false);
|
|
|
|
$(document).on('click', '.pl_item', function (e) {
|
|
e.preventDefault();
|
|
var pl_name = $(this).text();
|
|
$.ajax({
|
|
url: $(this).attr('href'),
|
|
data: {
|
|
'access_token': access_token
|
|
}
|
|
}).done(function (data) {
|
|
var dups = data.data.map(function (item) {
|
|
return {
|
|
dup_trackname: item.track.name,
|
|
dup_artist: item.track.artists[0].name
|
|
}
|
|
});
|
|
if (data.data.length > 0) {
|
|
dupsPlaceholder.innerHTML = dupsTemplate({
|
|
dups : dups,
|
|
pl_name : pl_name
|
|
});
|
|
}
|
|
else {
|
|
dupsPlaceholder.innerHTML = dupsTemplate({
|
|
dups : [],
|
|
message: "No duplicate found.",
|
|
pl_name: pl_name
|
|
});
|
|
}
|
|
})
|
|
});
|
|
|
|
document.getElementById('obtain-new-token').addEventListener('click', function () {
|
|
$.ajax({
|
|
url: '/refresh_token',
|
|
data: {
|
|
'refresh_token': refresh_token
|
|
}
|
|
}).done(function (data) {
|
|
access_token = data.access_token;
|
|
oauthPlaceholder.innerHTML = oauthTemplate({
|
|
access_token: access_token,
|
|
refresh_token: refresh_token
|
|
});
|
|
});
|
|
}, false);
|
|
}
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|
|
|