• JSON database

    From Kirkman@VERT/GUARDIAN to All on Friday, September 26, 2014 14:59:39
    For my Sports Stats door, I am want to dump a bunch of stuff into the door's JSON service from a Python script.

    Right now I'm just having the Python script overwrite the .json file.

    Problem is, it doesn't seem like this is enough. In my tests, when the door requests data from the json service, the data it gets is old.

    So do I need to make my Python script somehow tell Synchronet to re-fetch the data from the .json file once it has written a new version? If so, how do I do this?

    --Josh

    ////--------------------------------------------------
    BiC -=- http://breakintochat.com -=- bbs wiki and blog

    ---
    þ Synchronet
  • From echicken@VERT/ECBBS to Kirkman on Friday, September 26, 2014 17:32:37
    Re: JSON database
    By: Kirkman to All on Fri Sep 26 2014 14:59:39

    Right now I'm just having the Python script overwrite the .json file.

    Problem is, it doesn't seem like this is enough. In my tests, when the door requests data from the json service, the data it gets is old.

    I believe that the JSON service only reads from that file when loading a DB module (which happens when the service is started/restarted, possibly when a given module is reloaded via json-svc-ctrl.js.)

    So do I need to make my Python script somehow tell Synchronet to re-fetch the data from the .json file once it has written a new version? If so, how do I do this?

    There may be an easier way to do this, however I would suggest the following:

    Create a script named "service.js" residing in the path for this module that you specified in json-service.ini. This script will be loaded automatically by the JSON service when it loads this module.

    Instead of having your Python script overwrite the JSON DB file, have it save its JSON output to some other file. The following would monitor that file for a change in timestamp:

    load("json-client.js");

    var jsonClient = new JSONClient("localhost", 10088); // adjust accordingly

    var theFile = "/path/to/json/file/from/python/script";
    var utime = file_date(theFile);

    while(!js.terminated) {
    mswait(5);
    if(file_date(theFile) == utime)
    continue;
    utime = file_date(theFile);
    var f = new File(theFile);
    f.open("r");
    var stuff = JSON.parse(f.read());
    f.close();
    /* The next part depends on how your DB is laid out, but at this
    point you can copy values from 'stuff' into your DB */
    }

    jsonClient.disconnect();

    Obviously this could be improved in various ways, but that should get you started. (Checking that the file exists and is openable / opened, etc. would be suggested improvements.)

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com - 416-273-7230
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From Kirkman@VERT/GUARDIAN to echicken on Monday, September 29, 2014 12:02:42
    I believe that the JSON service only reads from that file when loading a DB module (which happens when the service is started/restarted, possibly when
    a given module is reloaded via json-svc-ctrl.js.)

    I played around with json-svc-ctrl.js, and using it to reload my sportsstats module does indeed cause the latest info to display in the door.

    I'd like to do this reloading on demand (I only scrape new data once or twice an hour), rather than have a script constantly running. So tonight I'm going
    to see if I can pull some of the bits from json-svc-ctrl.js into a new script that I could call from the command line to reload sportsstats json data.

    --Josh

    ////--------------------------------------------------
    BiC -=- http://breakintochat.com -=- bbs wiki and blog

    ---
    þ Synchronet
  • From echicken@VERT/ECBBS to Kirkman on Monday, September 29, 2014 13:57:14
    Re: Re: JSON database
    By: Kirkman to echicken on Mon Sep 29 2014 12:02:42

    I'd like to do this reloading on demand (I only scrape new data once or twice an hour), rather than have a script constantly running. So tonight I'm going to see if I can pull some of the bits from json-svc-ctrl.js into a new script that I could call from the command line to reload sportsstats json data.

    Overwriting the .json file will probably work. The sequence of events will be important. I imagine it'll be fine if you "close" the DB module, overwrite the file, then reload the DB module.

    Another option would be to make a script like the one I described, but run it as a timed event (or have your Python script kick it off after it creates its JSON file) instead of as a module service. That way it wouldn't be constantly running, and could update your DB as needed.

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com - 416-273-7230
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From Kirkman@VERT/GUARDIAN to echicken on Tuesday, September 30, 2014 14:26:09
    Overwriting the .json file will probably work. The sequence of events will be important. I imagine it'll be fine if you "close" the DB module, overwrite the file, then reload the DB module.

    I'm trying to recreate the "reload" function in json-svc-ctrl.js. I think I have put together something really short and simple that should woek, but I keep getting an error:

    "!JavaScript : uncaught exception: User not found: [object Object]"

    Here's what I have. The error seems to be produced by the wait("OK");

    Any idea what I'm doing wrong? My username and password are correct in the script itself.




    load('json-client.js');

    var db;
    var user = '[removed]';
    var pass = '[removed]';

    var server = 'localhost';
    var port = '10088';

    function wait(func) {
    var start = Date.now();
    do {
    var packet = db.receive();
    log(packet);
    if(!packet)
    continue;
    else if(packet.func == func)
    return packet.data;
    else
    db.updates.push(packet.data);
    } while(Date.now() - start < db.settings.SOCK_TIMEOUT);
    throw("timed out waiting for server response");
    }

    if(!db) {
    db = new JSONClient(server,port);
    }
    if(!db.socket.is_connected)
    db.connect();
    db.ident('ADMIN',user,pass);

    var trash = wait("OK");

    db.send({scope:'sportsstats',func:'RELOAD'});

    ////--------------------------------------------------
    BiC -=- http://breakintochat.com -=- bbs wiki and blog

    ---
    þ Synchronet
  • From echicken@VERT/ECBBS to Kirkman on Tuesday, September 30, 2014 15:53:53
    Re: Re: JSON database
    By: Kirkman to echicken on Tue Sep 30 2014 14:26:09

    "!JavaScript : uncaught exception: User not found: [object Object]"

    var user = '[removed]';
    var pass = '[removed]';

    Just to be sure, your actual username & password aren't wrapped in [], are they? If they are, the JSON service may be treating them as arrays and failing to find a match.

    db.ident('ADMIN',user,pass);

    I've never had any problems with this, when 'user' and 'pass' are the valid username & password for an account on my BBS. Out of laziness I have done this in the past:

    var u = new User(1);
    db.ident('ADMIN', u.alias, u.security.password);

    Which idents as the sysop account, and saves me needing to update scripts if I change my password.

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com - 416-273-7230
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From Kirkman@VERT/GUARDIAN to echicken on Tuesday, September 30, 2014 20:40:46
    Just to be sure, your actual username & password aren't wrapped in [], are they? If they are, the JSON service may be treating them as arrays and failing to find a match.

    No, I didn't have them wrapped in brackets. They were just plain text in quotes. Not sure why it didn't work.

    But your alternative solution of u = New User (1); worked perfectly!

    I like that because there's no chance of me accidentally exposing credentials on Github or whatever.

    Very nice! Thanks!

    --Josh

    ////--------------------------------------------------
    BiC -=- http://breakintochat.com -=- bbs wiki and blog

    ---
    þ Synchronet