feat: scripts
This commit is contained in:
105
scripts/async.js
Normal file
105
scripts/async.js
Normal file
@@ -0,0 +1,105 @@
|
||||
#! /usr/bin/env runjs
|
||||
|
||||
var Thread = java.lang.Thread;
|
||||
var Runtime = java.lang.Runtime;
|
||||
|
||||
var argsjs = require('component-args.js');
|
||||
var jsonjs = require('component-json-ex.js');
|
||||
|
||||
var printHelp = () => {
|
||||
println('async.js [options] <id>');
|
||||
println();
|
||||
println('-h, --help Print help');
|
||||
println('-i, --interval Time every refresh, default 1 (s)');
|
||||
println('-t, --token Token, default read from ~/.jssp/config/asyncjs.token');
|
||||
return;
|
||||
};
|
||||
|
||||
var main = () => {
|
||||
var args = argsjs.parseDefARGs(['h', 'help']);
|
||||
if (args.flg('h', 'help')) {
|
||||
printHelp();
|
||||
return;
|
||||
}
|
||||
if (args.length < 1) {
|
||||
xprintln('[ERROR] Target id and filename is not assigned.');
|
||||
printHelp();
|
||||
return;
|
||||
}
|
||||
var id = args[0];
|
||||
|
||||
var interval = args.val('i', 'interfal') || 1;
|
||||
var token = args.val('t', 'token');
|
||||
if (token == null) {
|
||||
var tokenFile = $$.file('~/.jssp/config/asyncjs.token');
|
||||
if (tokenFile.exists()) { token = $$.rFile(tokenFile).string().trim(); }
|
||||
}
|
||||
if (token == null) {
|
||||
xprintln('[ERROR] Token is not assigned!');
|
||||
return;
|
||||
}
|
||||
|
||||
var getResultStr = $$.httpRequest().url('https://playsecurity.org/editItem.jsonp').post($$.keyValues()
|
||||
.kv('token', token)
|
||||
.kv('id', id)
|
||||
.kv('act', 'GET')
|
||||
.join()).toString();
|
||||
var getResultJSON = JSON.parse(getResultStr);
|
||||
|
||||
if (getResultJSON.status != 200) {
|
||||
xprintln('[ERROR] Get item failed: ' + getResultStr);
|
||||
return;
|
||||
}
|
||||
|
||||
var fn = getResultJSON.data.fileName;
|
||||
var body = getResultJSON.data.body;
|
||||
|
||||
var localFile = $$.file(id + '_' + fn);
|
||||
if (localFile.exists()) {
|
||||
xprintln('[ERROR] File exists: ' + localFile);
|
||||
return;
|
||||
}
|
||||
|
||||
$$.rFile(localFile).write(body);
|
||||
xprintln('[OK] Write file success: ' + localFile);
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() => {
|
||||
xprintln('[INFO] Watch ended: ' + localFile);
|
||||
}));
|
||||
|
||||
var localFileLastModifiled = localFile.lastModified();
|
||||
xprintln('[INFO] Last modified: ' + localFileLastModifiled);
|
||||
xprintln('[INFO] Watching thread is running...');
|
||||
|
||||
while (true) {
|
||||
$$.sleep(500);
|
||||
var modified = localFile.lastModified();
|
||||
if (modified > localFileLastModifiled) {
|
||||
var now = $$.date().millis();
|
||||
if ((now - modified) < 100) { $$.sleep(100); }
|
||||
|
||||
var secs = '' + $$.num(modified).sub(localFileLastModifiled).divr(1000, 0, null);
|
||||
xprintln('[INFO] File last modified updated: '
|
||||
+ localFileLastModifiled + ' -> ' + modified
|
||||
+ ', diff: '
|
||||
+ repeat(' ', (6 - secs.length)) + secs + ' s');
|
||||
|
||||
var putResultStr = $$.httpRequest().url('https://playsecurity.org/editItem.jsonp').post($$.keyValues()
|
||||
.kv('token', token)
|
||||
.kv('id', id)
|
||||
.kv('act', 'PUT')
|
||||
.kv('body', $$.rFile(localFile).string())
|
||||
.join()).toString();
|
||||
var putResultJSON = JSON.parse(putResultStr);
|
||||
|
||||
if (getResultJSON.status != 200) {
|
||||
xprintln('[ERROR] Put item failed: ' + getResultStr); // STOP ?
|
||||
} else {
|
||||
xprintln('[OK] Put item successed!')
|
||||
}
|
||||
localFileLastModifiled = modified;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user