-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>CSE3026: Lab 11 - Song Search</title> | ||
|
||
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.3.0/prototype.js" type="text/javascript"></script> | ||
<script src="song_search.js" type="text/javascript"></script> | ||
</head> | ||
|
||
<body> | ||
<h1>[CSE3026] Lab 11 - Song Search</h1> | ||
|
||
<hr /> | ||
<div> | ||
|
||
<label>Top Rank: | ||
<select id="top" required="required"> | ||
<option value="3">3</option> | ||
<option value="5">5</option> | ||
<option value="7">7</option> | ||
<option value="10">10</option> | ||
</select> | ||
</label> | ||
<button id="b_xml">List Songs (XML)</button> | ||
<button id="b_json">List Songs (JSON)</button> | ||
</div> | ||
|
||
<div id="songarea"> | ||
<p>Top Ranked Songs:</p> | ||
|
||
<ol id="songs"> | ||
|
||
</ol> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
document.observe("dom:loaded", function() { | ||
$("b_xml").observe("click", function(){ | ||
//construct a Prototype Ajax.request object | ||
new Ajax.Request("songs_xml.php", { | ||
method: "GET", | ||
parameters: {top: $F('top')}, | ||
onSuccess: showSongs_XML, | ||
onFailure: ajaxFailed, | ||
onException: ajaxFailed | ||
}); | ||
}); | ||
$("b_json").observe("click", function(){ | ||
//construct a Prototype Ajax.request object | ||
new Ajax.Request("songs_json.php", { | ||
method: "GET", | ||
parameters: {top: $F('top')}, | ||
onSuccess: showSongs_JSON, | ||
onFailure: ajaxFailed, | ||
onException: ajaxFailed | ||
}); | ||
}); | ||
}); | ||
|
||
function showSongs_XML(ajax) { | ||
while ($("songs").firstChild) { | ||
$("songs").removeChild($("songs").firstChild); | ||
} | ||
var songs = ajax.responseXML.getElementsByTagName("song"); | ||
for (var i = 0; i < songs.length; i++) { | ||
var title = songs[i].getElementsByTagName("title")[0].firstChild.nodeValue; | ||
var artist = songs[i].getElementsByTagName("artist")[0].firstChild.nodeValue; | ||
var genre = songs[i].getElementsByTagName("genre")[0].firstChild.nodeValue; | ||
var time = songs[i].getElementsByTagName("time")[0].firstChild.nodeValue; | ||
|
||
var li = document.createElement("li"); | ||
li.innerHTML = title + " - " + artist + " [" + genre + "] " + "("+ time +")"; | ||
$("songs").appendChild(li); | ||
} | ||
} | ||
|
||
function showSongs_JSON(ajax) { | ||
while ($("songs").firstChild) { | ||
$("songs").removeChild($("songs").firstChild); | ||
} | ||
var data = JSON.parse(ajax.responseText); | ||
for (var i = 0; i < data.songs.length; i++) { | ||
var li = document.createElement("li"); | ||
li.innerHTML = data.songs[i].title + " - " + data.songs[i].artist + " [" + data.songs[i].genre + "] " + "("+ data.songs[i].time +")"; | ||
$("songs").appendChild(li); | ||
} | ||
} | ||
|
||
function ajaxFailed(ajax, exception) { | ||
var errorMessage = "Error making Ajax request:\n\n"; | ||
if (exception) { | ||
errorMessage += "Exception: " + exception.message; | ||
} else { | ||
errorMessage += "Server status:\n" + ajax.status + " " + ajax.statusText + | ||
"\n\nServer response text:\n" + ajax.responseText; | ||
} | ||
alert(errorMessage); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Hello|Adele|1|Pop|4:55 | ||
Hotline Bling|Drake|2|Hip-Hop/Rap|4:27 | ||
Sorry|Justin Bieber|3|Pop|3:20 | ||
The Hills|The Weeknd|4|RnB|4:02 | ||
Stitches|Shawn Mendes|5|Pop|3:26 | ||
What Do You Mean?|Justin Bieber|6|Pop|3:25 | ||
679|Fetty Wap|7|Hip-Hop/Rap|3:06 | ||
Wildest Dreams|Taylor Swift|8|Pop|3:40 | ||
Like I'm Gonna Lose You|Meghan Trainor|9|Pop|3:45 | ||
Ex's and Oh's|Elle King|10|Alternative|3:22 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
$SONGS_FILE = "songs_shuffled.txt"; | ||
|
||
if (!isset($_SERVER["REQUEST_METHOD"]) || $_SERVER["REQUEST_METHOD"] != "GET") { | ||
header("HTTP/1.1 400 Invalid Request"); | ||
die("ERROR 400: Invalid request - This service accepts only GET requests."); | ||
} | ||
|
||
$top = ""; | ||
|
||
if (isset($_REQUEST["top"])) { | ||
$top = preg_replace("/[^0-9]*/", "", $_REQUEST["top"]); | ||
} | ||
|
||
if (!file_exists($SONGS_FILE)) { | ||
header("HTTP/1.1 500 Server Error"); | ||
die("ERROR 500: Server error - Unable to read input file: $SONGS_FILE"); | ||
} | ||
|
||
header("Content-type: application/json"); | ||
|
||
print "{\n \"songs\": [\n"; | ||
|
||
// write a code to : | ||
// 1. read the "songs.txt" (or "songs_shuffled.txt" for extra mark!) | ||
// 2. search all the songs that are under the given top rank | ||
// 3. generate the result in JSON data format | ||
//$lines = file($SONGS_FILE); | ||
//for ($i = 0; $i < count($lines); $i++) { | ||
// list($title, $artist, $rank, $genre, $time) = explode("|", trim($lines[$i])); | ||
// if ($rank <= $top) { | ||
// if($i != $top -1){ | ||
// print "\t{\"rank\":\"$rank\", \"title\":\"$title\", \"artist\":\"$artist\", \"genre\":\"$genre\", \"time\":\"$time\"},\n"; | ||
// } else { | ||
// print "\t{\"rank\":\"$rank\", \"title\":\"$title\", \"artist\":\"$artist\", \"genre\":\"$genre\", \"time\":\"$time\"}\n"; | ||
// } | ||
// } | ||
//} | ||
$lines = file($SONGS_FILE); | ||
$count = 1; | ||
$temp = intval($top); | ||
for($count = 1; $count <= $temp; $count++){ | ||
for ($i = 0; $i < count($lines); $i++) { | ||
list($title, $artist, $rank, $genre, $time) = explode("|", trim($lines[$i])); | ||
if ($rank == $count) { | ||
if($count != $top){ | ||
print "\t{\"rank\":\"$rank\", \"title\":\"$title\", \"artist\":\"$artist\", \"genre\":\"$genre\", \"time\":\"$time\"},\n"; | ||
} else { | ||
print "\t{\"rank\":\"$rank\", \"title\":\"$title\", \"artist\":\"$artist\", \"genre\":\"$genre\", \"time\":\"$time\"}\n"; | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
print " ]\n}\n"; | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Stitches|Shawn Mendes|5|Pop|3:26 | ||
Sorry|Justin Bieber|3|Pop|3:20 | ||
679|Fetty Wap|7|Hip-Hop/Rap|3:06 | ||
Wildest Dreams|Taylor Swift|8|Pop|3:40 | ||
The Hills|The Weeknd|4|RnB|4:02 | ||
Hello|Adele|1|Pop|4:55 | ||
Ex's and Oh's|Elle King|10|Alternative|3:22 | ||
Hotline Bling|Drake|2|Hip-Hop/Rap|4:27 | ||
Like I'm Gonna Lose You|Meghan Trainor|9|Pop|3:45 | ||
What Do You Mean?|Justin Bieber|6|Pop|3:25 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
$SONGS_FILE = "songs_shuffled.txt"; | ||
|
||
if (!isset($_SERVER["REQUEST_METHOD"]) || $_SERVER["REQUEST_METHOD"] != "GET") { | ||
header("HTTP/1.1 400 Invalid Request"); | ||
die("ERROR 400: Invalid request - This service accepts only GET requests."); | ||
} | ||
|
||
$top = ""; | ||
|
||
if (isset($_REQUEST["top"])) { | ||
$top = preg_replace("/[^0-9]*/", "", $_REQUEST["top"]); | ||
} | ||
|
||
if (!file_exists($SONGS_FILE)) { | ||
header("HTTP/1.1 500 Server Error"); | ||
die("ERROR 500: Server error - Unable to read input file: $SONGS_FILE"); | ||
} | ||
|
||
header("Content-type: application/xml"); | ||
|
||
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; | ||
print "<songs>\n"; | ||
|
||
|
||
//$lines = file($SONGS_FILE); | ||
//for ($i = 0; $i < count($lines); $i++) { | ||
// list($title, $artist, $rank, $genre, $time) = explode("|", trim($lines[$i])); | ||
// if ($rank <= $top) { | ||
// print "\t<song rank=\"$rank\">\n"; | ||
// print "\t\t<title>$title</title>\n"; | ||
// print "\t\t<artist>$artist</artist>\n"; | ||
// print "\t\t<genre>$genre</genre>\n"; | ||
// print "\t\t<time>$time</time>\n"; | ||
// print "\t</song>\n"; | ||
// } | ||
//} | ||
$lines = file($SONGS_FILE); | ||
$count = 1; | ||
$temp = intval($top); | ||
for($count = 1; $count <= $temp; $count++){ | ||
for ($i = 0; $i < count($lines); $i++) { | ||
list($title, $artist, $rank, $genre, $time) = explode("|", trim($lines[$i])); | ||
if ($rank == $count) { | ||
print "\t<song rank=\"$rank\">\n"; | ||
print "\t\t<title>$title</title>\n"; | ||
print "\t\t<artist>$artist</artist>\n"; | ||
print "\t\t<genre>$genre</genre>\n"; | ||
print "\t\t<time>$time</time>\n"; | ||
print "\t</song>\n"; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
print "</songs>"; | ||
|
||
?> |