Skip to content

Instantly share code, notes, and snippets.

@jsieber
Last active April 25, 2017 04:39
Show Gist options
  • Save jsieber/8e0c3673073895024e1a3ea4b9ad8ad6 to your computer and use it in GitHub Desktop.
Save jsieber/8e0c3673073895024e1a3ea4b9ad8ad6 to your computer and use it in GitHub Desktop.
Spotify Favorites
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<script>
window.onload = function () {
var hash = window.location.hash;
if (window.location.search.substring(1).indexOf("error") !== -1) {
window.close();
} else if (hash) {
var token = window.location.hash.split('&')[0].split('=')[1];
localStorage.setItem('spotify-token', token);
}
}
</script>
</head>
<body>
</body>
</html>
body {
padding-top: 50px;
font-family: 'Gloria Hallelujah', cursive;
}
.favorite-template {
padding: 40px 15px;
// text-align: center;
}
.form-inline {
padding:10px;
}
.form-inline > * {
margin:15px 3px !important;
}
<!DOCTYPE html>
<html lang="en" ng-app="asTestApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="See your favorite Artists and Tracks based on your listening through Spotify.com.">
<title>Spotify Tools</title>
<!-- Google Tag Manager -->
<script>
(function(w, d, s, l, i) {
w[l] = w[l] || [];
w[l].push({
'gtm.start': new Date().getTime(),
event: 'gtm.js'
});
var f = d.getElementsByTagName(s)[0],
j = d.createElement(s),
dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src =
'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-W35T37N');
</script>
<!-- End Google Tag Manager -->
<!-- angular.js -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular-cookies.js"></script>
<script src="favorite.js"></script>
<script src="angular-spotify.js"></script>
<!-- jquery -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Web fonts -->
<link href="https://fonts.googleapis.com/css?family=Gloria+Hallelujah" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="favorite.css" rel="stylesheet">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body ng-controller="favoriteController">
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-W35T37N"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<!---
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
--->
<a class="navbar-brand" href="#">Spotify Tools</a>
</div>
<!--
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#topArtists">Top Artists</a></li>
<li><a href="#topTracks">Top Tracks</a></li>
</ul>
</div>
-->
<!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<div class="row">
<div id="login" ng-show="showLogin" class="text-center">
<hr>
<p>Login to Spotify to see your most listened to artists and tracks.</p>
<button ng-click="login()" class="btn btn-success" ng-show="showLogin">Login with Spotify</button>
<hr>
</div>
<a name="results"></a>
<div id="main" ng-hide="showLogin">
<div id="form">
<div id="topArtists" class="col-xs-12 col-sm-12 col-md-6">
<form ng-submit="submitArtist()" class="">
<h3>Top Artists</h3>
<div class="form-group">
<label for="artistTime_range">Term</label>
<select name="artistTime_range" id="artistTime_range" class="form-control" ng-model="topArtists.term" required>
<option value="">Select term</option>
<option value="long_term">Long Term</option>
<option value="medium_term">Medium Term</option>
<option value="short_term">Short Term</option>
</select>
</div>
<div class="form-group">
<label for="artistLimit">Results</label>
<select name="artistLimit" id="artistLimit" class="form-control" ng-model="topArtists.limit" required>
<option value="">Select limit</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
</select>
</div>
<input type="submit" id="submit" value="Submit" class="btn btn-primary" />
</form>
<hr>
</div>
<div id="topTracks" class="col-xs-12 col-sm-12 col-md-6">
<form ng-submit="submitTracks()" class="">
<h3>Top Tracks</h3>
<div class="form-group">
<label for="trackTime_range">Term</label>
<select name="trackTime_range" id="trackTime_range" class="form-control" ng-model="topTracks.term" required>
<option value="">Select term</option>
<option value="long_term">Long Term</option>
<option value="medium_term">Medium Term</option>
<option value="short_term">Short Term</option>
</select>
</div>
<div class="form-group">
<label for="topTrackLimit">Results</label>
<select name="topTrackLimit" id="topTrackLimit" class="form-control" ng-model="topTracks.limit" required>
<option value="">Select limit</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
</select>
</div>
<input type="submit" id="submitTracks" value="Submit" class="btn btn-primary" />
</form>
<hr>
</div>
</div>
<a name="results"></a>
</div>
</div>
<div id="favorites">
<div class="row">
<div ng-show="displayType" class="alert alert-warning">
<h4>Results returned by genre {{genre}}</h4>
</div>
</div>
<ul class="list-unstyled">
<li ng-repeat="favorite in favorites">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-8">
<div class="media-details">
<h2><a class="media" ng-href="{{favorite.external_urls.spotify}}" target="_blank">{{favorite.name}}</a></h2>
<h3 ng-if="favorite.artists[0]"><a class="media" ng-href="{{favorite.artists[0].external_urls.spotify}}" target="_blank">{{favorite.artists[0].name}}</h3>
<ul class="list-inline" ng-if="favorite.genres">
<li ng-repeat="genre in favorite.genres" style="padding-bottom: 8px;">
<a type="button" class="btn btn-success btn-xs" href="#results" ng-click="genreSearch(genre)">{{genre}}</a>
</li>
</ul>
</div>
</div>
<div class="col-xs-6 col-md-4">
<a class="media" ng-href="{{favorite.external_urls.spotify}}" target="_blank">
<img ng-if="favorite.images[2].url" ng-src="{{favorite.images[2].url}}" alt="{{favorite.name}}" class="img-responsive center-block img-thumbnail">
<img ng-if="favorite.album.images[1].url" ng-src="{{favorite.album.images[1].url}}" alt="{{favorite.name}}" class="img-responsive center-block img-thumbnail">
</a>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-4">
<dl ng-if="favorite.followers">
<dt>Followers</dt>
<dd><a type="button" class="btn btn-info btn-sm disabled">{{favorite.followers.total}}</a></dd>
</dl>
<h4 ng-if="favorite.album.name">Album: <a class="media" ng-href="{{favorite.album.external_urls.spotify}}" target="_blank">{{favorite.album.name}}</a></h4>
</div>
<!-- Optional: clear the XS cols if their content doesn't match in height -->
<div class="clearfix visible-xs-block"></div>
<div class="col-xs-6 col-sm-4">
<dl ng-if="favorite.popularity">
<dt>Popularity</dt>
<dd><a type="button" class="btn btn-info btn-sm disabled">{{favorite.popularity}}</a></dd>
</dl>
</div>
</div>
<hr>
</li>
</ul>
</div>
</div>
</div>
</body>
</html>
angular
.module('asTestApp', [
'spotify'
])
.config(function (SpotifyProvider) {
SpotifyProvider.setClientId('CLIENT_ID_GOES_HERE');
SpotifyProvider.setRedirectUri('http://localhost:8080/node_modules/angular-spotify/examples/callback.html');
SpotifyProvider.setScope('playlist-read-private user-top-read');
})
.controller('favoriteController', ['$scope', '$window', '$timeout', 'Spotify', function ( $scope, $window, $timeout, Spotify) {
$scope.showLogin = true;
$scope.displayType = 0;
if($window.localStorage.getItem('spotify-token')) {
Spotify.setAuthToken($window.localStorage.getItem('spotify-token'));
} else {
$scope.showLogin = true;
};
//login
$scope.login = function () {
Spotify.login().then(function (data) {
//console.log("logged in " + data);
$scope.showLogin = false;
//alert("You are now logged in");
}, function () {
console.log('didn\'t log in');
})
};
//search genre
$scope.genreSearch = function(genre) {
$scope.displayType = 1;
$scope.genre = genre;
var q = 'genre:' + '"' + genre + '"';
Spotify.search(q, 'artist').then(function (data){
console.log(data.artists.items);
$timeout(function() {
$scope.favorites = data.artists.items;
}, 0);
})
};
//list top artists
$scope.submitArtist = function() {
$scope.displayType = 0;
$scope.genre = '';
Spotify.getUserTopArtists({limit: $scope.topArtists.limit, time_range:$scope.topArtists.term}).then(function (data) {
console.log(data.items);
$timeout(function() {
$scope.favorites = data.items;
}, 0);
});
};
//list top Tracks
$scope.submitTracks = function() {
$scope.displayType = 0;
$scope.genre = '';
Spotify.getUserTopTracks({ limit: $scope.topTracks.limit, time_range:$scope.topTracks.term }).then(function (data) {
console.log(data.items);
$timeout(function(){
$scope.favorites = data.items;
}, 0);
});
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment