Continue working on API

This commit is contained in:
calzoneman 2013-04-25 16:16:53 -05:00
parent 3c2b5fcb2c
commit a88088f2d6
5 changed files with 177 additions and 20 deletions

53
api.js
View file

@ -9,12 +9,14 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var Auth = require("./auth.js");
var Server = require("./server.js");
var Logger = require("./logger.js");
var apilog = new Logger.Logger("api.log");
var jsonHandlers = {
"channeldata": handleChannelData
"channeldata": handleChannelData,
"listloaded" : handleChannelList
};
function handle(path, req, res) {
@ -58,39 +60,54 @@ function handle(path, req, res) {
exports.handle = handle;
function handleChannelData(params, req, res) {
var cname = params["channel"] || "";
var data;
if(!cname.match(/^[a-zA-Z0-9]+$/)) {
data = {
error: "Invalid channel name"
};
}
else {
data = {
var clist = params.channel || "";
clist = clist.split(",");
var data = [];
for(var j = 0; j < clist.length; j++) {
var cname = clist[j];
if(!cname.match(/^[a-zA-Z0-9]+$/)) {
continue;
}
var d = {
name: cname,
loaded: (cname in Server.channels)
};
if(data.loaded) {
if(d.loaded) {
var chan = Server.channels[cname];
data.title = chan.media ? chan.media.title : "-";
data.usercount = chan.users.length;
data.users = [];
d.title = chan.media ? chan.media.title : "-";
d.usercount = chan.users.length;
d.users = [];
for(var i = 0; i < chan.users.length; i++) {
if(chan.users[i].name) {
data.users.push(chan.users[i].name);
d.users.push(chan.users[i].name);
}
}
data.chat = [];
d.chat = [];
for(var i = 0; i < chan.chatbuffer.length; i++) {
data.chat.push(chan.chatbuffer[i]);
d.chat.push(chan.chatbuffer[i]);
}
}
data.push(d);
}
var response = JSON.stringify(data, null, 4);
res.setHeader("Content-Type", "text/plain");
res.setHeader("Content-Type", "application/json");
res.setHeader("Content-Length", response.length);
res.end(response);
}
function handleChannelList(params, req, res) {
var name = params.name || "";
var pw = params.pw || "";
if(!Auth.login(name, pw)) {
res.send(403);
return;
}
var clist = [];
for(var key in Server.channels) {
clist.push(key);
}
handleChannelData({channel: clist.join(",")}, req, res);
}

View file

@ -2,7 +2,7 @@
"author": "Calvin Montgomery",
"name": "CyTube",
"description": "Online media synchronizer and chat",
"version": "1.2.6",
"version": "1.2.7",
"repository": {
"url": "http://github.com/calzoneman/sync"
},

View file

@ -9,7 +9,7 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const VERSION = "1.2.6";
const VERSION = "1.2.7";
var fs = require("fs");
var Logger = require("./logger.js");

88
www/channellist.html Normal file
View file

@ -0,0 +1,88 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CyTube Channel Listing</title>
<link href="assets/css/bootstrap.css" rel="stylesheet">
<style type="text/css">
#channeldata td, #channeldata th {
text-align: center;
vertical-align: middle;
}
.loginform {
margin: 100px auto 20px;
padding: 19px 29px 29px;
border-radius: 5px 5px 5px 5px;
border: 1px solid #dedede;
max-width: 300px;
}
</style>
</head>
<body>
<div class="container">
<form class="form-horizontal loginform" action="javascript:void(0);">
<div class="control-group">
<input id="name" type="text" placeholder="Username" class="input-block-level">
</div>
<div class="control-group">
<input id="pw" type="password" placeholder="Password" class="input-block-level">
</div>
<button id="login" class="btn btn-block">Authenticate</button>
</form>
</div>
<table id="channeldata" class="table table-striped table-bordered">
<thead>
<tr>
<th>Channel</th>
<th>Connected</th>
<th>Playing</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script src="assets/js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var q = "";
$("#login").click(function() {
q = "name=" + $("#name").val() + "&pw=" + $("#pw").val();
refresh();
setInterval(refresh, 5000);
});
function refresh() {
$.getJSON("api/json/listloaded?" + q, function(data) {
var host = document.location + "";
host = host.substring(0, host.indexOf("/widget.html"));
if(data.error) {
alert(data.error);
return;
}
$(".loginform").remove();
$("#channeldata").find("tbody").remove();
data.sort(function(a, b) {
var x = a.usercount;
var y = b.usercount;
if(x == y) {
var c = a.name.toLowerCase();
var d = b.name.toLowerCase();
return c == d ? 0 : (c < d ? -1 : 1);
}
return y - x;
});
for(var i = 0; i < data.length; i++) {
var d = data[i];
var tr = $("<tr/>").appendTo($("#channeldata"));
var name = $("<td/>").appendTo(tr);
$("<a/>").attr("href", host + "/r/" + d.name)
.text(d.name)
.appendTo(name);
$("<td/>").text(d.usercount || 0).appendTo(tr);
$("<td/>").text(d.title || "-").appendTo(tr);
}
});
}
</script>
</body>
</html>

52
www/channelwidget.html Normal file
View file

@ -0,0 +1,52 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CyTube</title>
<link href="assets/css/bootstrap.css" rel="stylesheet">
<style type="text/css">
#channeldata td, #channeldata th {
text-align: center;
vertical-align: middle;
}
</style>
</head>
<body>
<table id="channeldata" class="table table-striped table-bordered">
<thead>
<tr>
<th>Channel</th>
<th>Connected</th>
<th>Playing</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script src="assets/js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var p = document.location + "";
if(p.indexOf("?") != -1) {
p = p.substring(p.indexOf("?")+1);
}
$.getJSON("api/json/channeldata?" + p, function(data) {
var host = document.location + "";
host = host.substring(0, host.indexOf("/widget.html"));
if(data.error) {
return;
}
$("#channeldata").find("tbody").remove();
for(var i = 0; i < data.length; i++) {
var d = data[i];
var tr = $("<tr/>").appendTo($("#channeldata"));
var name = $("<td/>").appendTo(tr);
$("<a/>").attr("href", host + "/r/" + d.name)
.text(d.name)
.appendTo(name);
$("<td/>").text(d.usercount || 0).appendTo(tr);
$("<td/>").text(d.title || "-").appendTo(tr);
}
});
</script>
</body>
</html>