Skip to content

Commit

Permalink
11.26
Browse files Browse the repository at this point in the history
  • Loading branch information
koodin9 committed Nov 26, 2015
1 parent 2558884 commit b0a97fa
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 0 deletions.
Binary file added 11.26/.DS_Store
Binary file not shown.
37 changes: 37 additions & 0 deletions 11.26/songSearch/song_search.html
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>
62 changes: 62 additions & 0 deletions 11.26/songSearch/song_search.js
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);
}
10 changes: 10 additions & 0 deletions 11.26/songSearch/songs.txt
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
57 changes: 57 additions & 0 deletions 11.26/songSearch/songs_json.php
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";

?>
10 changes: 10 additions & 0 deletions 11.26/songSearch/songs_shuffled.txt
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
59 changes: 59 additions & 0 deletions 11.26/songSearch/songs_xml.php
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>";

?>

0 comments on commit b0a97fa

Please sign in to comment.