Skip to content

Commit

Permalink
App update
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleNBurke committed Jul 29, 2015
0 parents commit a3ffd64
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bootstrap.min.css

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions bootstrap.min.js

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<html>
<head>
<title>iTunify</title>
<link href="bootstrap.min.css" rel="stylesheet">
<link href="styles.css" rel="stylesheet">
</head>
<body>
<script src="jquery-1.11.2.min.js"></script>
<script src="bootstrap.min.js"></script>
<script src="script.js"></script>

<ul class="nav nav-tabs" id="navTabs">
<li class="active"><a href="#allGenres">All Genres</a></li>
<li><a href="#hipHop">Hip Hop</a></li>
<li><a href="#rnb">R&amp;B</a></li>
<button type="button" class="btn btn-default">Authorize</button>
</ul>

<div class="tab-content">
<div class="tab-pane active" id="allGenres">
<button type="button" class="btn btn-default">Update</button>
<div></div>

<table class="table table-condensed">
<thead>
<tr>
<th>#</th>
<th>Artwork</th>
<th>Title</th>
<th>Artist</th>
<th>Album</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<div class="tab-pane" id="hipHop">
<button type="button" class="btn btn-default">Update</button>
<div></div>

<table class="table table-condensed">
<thead>
<tr>
<th>#</th>
<th>Artwork</th>
<th>Title</th>
<th>Artist</th>
<th>Album</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<div class="tab-pane" id="rnb">
<button type="button" class="btn btn-default">Update</button>
<div></div>

<table class="table table-condensed">
<thead>
<tr>
<th>#</th>
<th>Artwork</th>
<th>Title</th>
<th>Artist</th>
<th>Album</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>

</body>
</html>
4 changes: 4 additions & 0 deletions jquery-1.11.2.min.js

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions redirect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<script>
(function() {
var accessToken = window.location.hash.split("#")[1].split("&")[0].split("=")[1];
localStorage.setItem("accessToken", accessToken);
window.close();
})();
</script>
</head>
</html>
193 changes: 193 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
$(document).ready(function(){
$('#navTabs a').click(function (e) {
e.preventDefault()
$(this).tab('show')
})

$("#navTabs button").click(authorize);
$("#allGenres button").click(updateAllGenres);
$("#hipHop button").click(updateHipHop);
$("#rnb button").click(updateRnB);
});

function updateAllGenres()
{
update("https://itunes.apple.com/us/rss/topsongs/limit=50/explicit=true/xml", "allGenres", "2V07Dnbb1oeOi1DuastrUV");
}

function updateHipHop()
{
update("https://itunes.apple.com/us/rss/topsongs/limit=50/genre=18/explicit=true/xml", "hipHop", "2Mvn8DwcRwa36CrX0YpVHt");
}

function updateRnB()
{
update("https://itunes.apple.com/us/rss/topsongs/limit=50/genre=15/explicit=true/xml", "rnb", "2aN17RtUI8TNUF8dQVLhnO");
}

function authorize()
{
var clientID = "8c1c8a8cfdee4f2b8a53f86de34d5ecd";
var scopes = "playlist-modify-public playlist-modify-private";
var redirectURI = "http://localhost/itunify/redirect.html";

var url = "https://accounts.spotify.com/authorize?response_type=token&client_id=" + clientID + "&scope=" + scopes + "&redirect_uri=" + redirectURI;

window.open(url);
}

function update(url, genre, playlistURI)
{
$("#" + genre + " div").text("Updating...");

var xmlHttp = new XMLHttpRequest();

xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
responseSuccessful(xmlHttp.responseXML, genre, playlistURI);
}
};

xmlHttp.open("GET", url, true);
xmlHttp.send();
}

function responseSuccessful(doc, genre, playlistURI)
{
$("#"+genre+" table").html("");
clearPlaylsit(localStorage.getItem("accessToken"), playlistURI);

var entryNodes = doc.getElementsByTagName("entry");
var tracks = "";
var queries = [];

for(var i = 0; i < entryNodes.length; i++)
{
var n = entryNodes[i];

var currentField = n.childNodes[7].textContent;
var trackTitle;
var featured = null;

if(currentField.split(" (feat. ").length > 1)
{
trackTitle = currentField.split(" (feat. ")[0];
featured = currentField.split(" (feat. ")[1].split(")")[0];
}
else if(currentField.split(" [feat. ").length > 1)
{
trackTitle = currentField.split(" [feat. ")[0];
featured = currentField.split(" [feat. ")[1].split("]")[0];
}
else
trackTitle = currentField;

currentField = n.childNodes[17].textContent;
var artistName = currentField;

if(featured != null)
{
if(featured.split(" & ").length > 1)
artistName += ", " + featured.split(" & ")[0] + ", " + featured.split(" & ")[1];
else
artistName += ", " + featured;
}

var artwork = n.childNodes[21].textContent;
var title = n.childNodes[7].textContent;
var artist = n.childNodes[17].textContent;
var album = n.childNodes[31].textContent;

$("#"+genre+" table").append('<tr><td>' + (i + 1) +'</td><td><image src="' + artwork + '" width="32px" height="32px" /></td><td>' + title + "</td><td>" + artist + "</td><td>" + album + "</td></tr>");

var query = "track:" + encodeURI(trackTitle + " ") + "artist:" + encodeURI(artistName);

var forbiddenChar = false;
if(query.split("#").length > 1) //forbidden character
{
query = query.split("#")[0] + query.split("#")[1];
forbiddenChar = true;
}

$.ajax({
url: "https://api.spotify.com/v1/search?q=" + query + "&type=track",
type: "GET",
dataType: "json",
async: false,
success: function(data)
{
if(forbiddenChar)
$("#" + genre + " table tbody").children().eq(i).addClass("warning");

if(data.tracks.items.length == 0)
{
$("#" + genre + " table tbody").children().eq(i).addClass("danger");
return;
}

var uri = data.tracks.items[0].uri;

if(data.tracks.items.length > 1 && !data.tracks.items[0].explicit && data.tracks.items[1].explicit)
{
var name1 = data.tracks.items[0].name;
var artist1 = data.tracks.items[0].artists;
var name2 = data.tracks.items[0].name;
var artist2 = data.tracks.items[0].artists;

if(name1 == name2 && artist1 == artist2)
uri = data.tracks.items[1].uri;
}

tracks += uri + ",";
}
});
}

tracks = tracks.slice(0, -1);

$.ajax({
url: "https://api.spotify.com/v1/users/casualkyle/playlists/" + playlistURI +"/tracks?uris=" + tracks,
type: "POST",
headers: {
"Authorization": "Bearer " + localStorage.getItem("accessToken"),
"Content-Type": "application/json"
}
});

$("#" + genre + " div").text("Done!");
}

function clearPlaylsit(accessToken, playlistURI)
{
$.ajax({
url: "https://api.spotify.com/v1/users/casualkyle/playlists/" + playlistURI,
headers: {
"Authorization": "Bearer " + accessToken
},
dataType: "json",
success: function(data)
{
if(data.tracks.items.length == 0)
return;

var positions = "";

for(var i = 0; i < data.tracks.items.length; i++)
positions += i.toString() + ", ";

positions = positions.slice(0, -2);

$.ajax({
type: "DELETE",
url: "https://api.spotify.com/v1/users/casualkyle/playlists/" + playlistURI + "/tracks",
headers: {
"Authorization": "Bearer " + accessToken,
"Content-Type": "application/json"
},
data: "{\"positions\": ["+positions+"], \"snapshot_id\": \""+data.snapshot_id+"\"}"
});
}
});
}
22 changes: 22 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
body {
margin: 40px;
}

#navTabs button {
float: right;
}

div .btn {
margin-top: 20px;
margin-bottom: 20px;
}

div div div {
float: right;
font-size: medium;
margin-top: 20px;
}

button {
width: 150px;
}

0 comments on commit a3ffd64

Please sign in to comment.