-
JSON database
From
Kirkman@VERT/GUARDIAN to
All on Fri Sep 26 14:59:39 2014
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 Fri Sep 26 17:32:37 2014
Re: JSON databaseã By: Kirkman to All on Fri Sep 26 2014 14:59:39ãã Ki> Right now I'm just having the Python script overwrite the .json file.ãã Ki> Problem is, it doesn't seem like this is enough. In my tests, when theã Ki> 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.)ã ã Ki> So do I need to make my Python script somehow tell Synchronet to re-fetchã Ki> the data from the .json file once it has written a new version? If so, howã Ki> 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 Mon Sep 29 12:02:42 2014
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 Mon Sep 29 13:57:14 2014
Re: Re: JSON databaseã By: Kirkman to echicken on Mon Sep 29 2014 12:02:42ãã Ki> I'd like to do this reloading on demand (I only scrape new data once orã Ki> twice an hour), rather than have a script constantly running. So tonightã Ki> I'm going to see if I can pull some of the bits from json-svc-ctrl.js intoã Ki> a new script that I could call from the command line to reload sportsstatsã Ki> 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 Tue Sep 30 14:26:09 2014
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 Tue Sep 30 15:53:53 2014
Re: Re: JSON databaseã By: Kirkman to echicken on Tue Sep 30 2014 14:26:09ãã Ki> "!JavaScript : uncaught exception: User not found: [object Object]"ãã Ki> var user = '[removed]';ã Ki> 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.ãã Ki> 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 Tue Sep 30 20:40:46 2014
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ã