Added parser, now returns JSON.
This commit is contained in:
parent
78a6c49198
commit
dfc3336197
1 changed files with 70 additions and 6 deletions
76
ubuntuone.js
76
ubuntuone.js
|
@ -1,21 +1,21 @@
|
||||||
var http = require('http'),
|
var http = require('http'),
|
||||||
exec = require('child_process').exec,
|
exec = require('child_process').exec,
|
||||||
pieces, interface, method, dbus_send, child;
|
pieces, interface, method, dbus_send, child,
|
||||||
|
lines, length, i, line,
|
||||||
|
variable = '', value = '', payload = '';
|
||||||
|
|
||||||
http.createServer(function(request, response)
|
http.createServer(function(request, response)
|
||||||
{
|
{
|
||||||
pieces = request.url.split('/');
|
pieces = request.url.split('/');
|
||||||
|
|
||||||
response.writeHead(200, {'Content-Type': 'text/plain'});
|
response.writeHead(200, {'Content-Type': 'application/json'});
|
||||||
|
|
||||||
if (pieces.length == 3)
|
if (pieces.length == 3)
|
||||||
{
|
{
|
||||||
interface = pieces[1].toLowerCase();
|
interface = pieces[1].toLowerCase();
|
||||||
method = pieces[2].toLowerCase();
|
method = pieces[2].toLowerCase();
|
||||||
|
|
||||||
dbus_send = 'dbus-send --session --print-reply --type=method_call'
|
dbus_send = 'dbus-send --session --print-reply --type=method_call --dest=com.ubuntuone.SyncDaemon /' + interface + ' com.ubuntuone.SyncDaemon.' + interface.charAt(0).toUpperCase() + interface.slice(1) + '.' + method.toLowerCase();
|
||||||
+ ' --dest=com.ubuntuone.SyncDaemon /' + interface
|
|
||||||
+ ' com.ubuntuone.SyncDaemon.' + interface.charAt(0).toUpperCase() + interface.slice(1) + '.' + method.toLowerCase();
|
|
||||||
|
|
||||||
child = exec(dbus_send, function(error, stdout, stderr)
|
child = exec(dbus_send, function(error, stdout, stderr)
|
||||||
{
|
{
|
||||||
|
@ -25,7 +25,71 @@ http.createServer(function(request, response)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
response.end(stdout);
|
lines = stdout.split("\n");
|
||||||
|
lines.shift();
|
||||||
|
|
||||||
|
length = lines.length;
|
||||||
|
|
||||||
|
payload = '';
|
||||||
|
variable = '';
|
||||||
|
value = '';
|
||||||
|
|
||||||
|
for (i = 0; i < length; ++i)
|
||||||
|
{
|
||||||
|
line = lines[i].replace(/^\s*|\s*$/, '');
|
||||||
|
|
||||||
|
switch (line)
|
||||||
|
{
|
||||||
|
case 'array [':
|
||||||
|
if (payload.charAt(payload.length - 1) == '}')
|
||||||
|
{
|
||||||
|
payload += ',';
|
||||||
|
}
|
||||||
|
|
||||||
|
payload += '{';
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ']':
|
||||||
|
payload += '}';
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'dict entry(':
|
||||||
|
case ')':
|
||||||
|
// Ignored
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
pieces = line.split('"');
|
||||||
|
|
||||||
|
if (variable == '')
|
||||||
|
{
|
||||||
|
variable = pieces[1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value = pieces[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
line = '';
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (variable != '' && value != '')
|
||||||
|
{
|
||||||
|
if (payload.charAt(payload.length - 1) == '"')
|
||||||
|
{
|
||||||
|
payload += ',';
|
||||||
|
}
|
||||||
|
|
||||||
|
payload += '"' + variable + '":"' + value + '"';
|
||||||
|
|
||||||
|
variable = '';
|
||||||
|
value = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
response.end(payload.replace('{{', '[{').replace('}}', '}]'));
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue