Initial commit

master
Avril 6 years ago
commit 5e2c1fa812
Signed by: flanchan
GPG Key ID: 284488987C31F630

1
.gitignore vendored

@ -0,0 +1 @@
node_modules/

@ -0,0 +1,38 @@
Pulls random images from lolibooru.
Usage:
require("hashloli").randomise(function(datas) {
if(!datas)
{
//Error
console.log(this.error);
return;
}
datas.forEach(function(data) {
if(data) {
let file_url = data.file_url;
//Do stuff
} else {
//Error
}
}
});
randomise() can take a second argument to specify configuration. The default is
new require("hashloli").Config();
See randomise.js for an example.
Command line usage:
node randomise.js [--verbose] [--debug] [--range=<range>] [--number=<number>] [--page=<page>] [<tags ...>]
Echos either the file url or `undefined' if there was an error. Echos full data in JSON if --verbose is supplied, also prints debug and error messages if --debug is supplied.
The default range is 5.
The default number is 1.
The default (max) page is 150.
If the number exceeds the range then the range is set equal to the number.
More advanced example:
node randomise.js --verbose --page=10 --number=5 flandre_scarlet blush
Npm dependancies:
xmlhttprequest

@ -0,0 +1,113 @@
const xhr = require("xmlhttprequest").XMLHttpRequest;
const API_URL = "https://lolibooru.moe/post/index.json";
/*function nodes_of(page, path) {
const document = parse5.parse(page);
const xhtml = xmlser.serializeToString(document);
const doc = new dom().parseFromString(xhtml);
const select = xpath.useNamespace({"x": "http://www.w3.org/1999/xhtml"});
return select(path, doc);
}
function nodes(url, path, cb) {
const x = new xhr();
x.onreadystatechange = function() {
if(this.readyState===4) {
cb(nodes_of(this.responseText, path));
}
};
x.open("GET", url);
x.send();
}*/
function query(limit, page, _tags) {
if(_tags)
{
var tags = _tags.join(" ");
}
return API_URL + "?limit="+limit+"&page="+page+(tags?"&tags="+encodeURIComponent(tags):"");
}
function rin(range) {
return Math.floor(Math.random() * (range-1));
}
function getLoli(puts, number, range, page, tags, cb) {
var x = new xhr();
if(number==0) {
puts("Number was 0, returning");
cb([]);
return;
}
x.onreadystatechange = function() {
if(this.readyState===4) {
let response = JSON.parse(this.responseText);
if(response) {
puts("Response okay");
if(number==1) {
let r = rin(range);
puts("Running on single (index "+r+")");
cb([response[r]]);
}
else {
let resp = Array.from({length: number}, (value,key) => key);
puts("Proto resp "+number+ " ar "+ JSON.stringify(resp));
for(let i = (number-1);i>=1;i--)
{
let v = resp[i];
let j = rin(i);
resp[i] = resp[j];
resp[j] = v;
}
puts("Running on multiple (indecies "+ resp.join(", ")+")");
cb(resp.map(v => response[v]));
}
} else {
puts("Invalid response");
cb.error = "Invalid JSON from server";
cb();
}
}
};
x.open("GET", query(range, page, tags));
x.send();
}
function randomiseWith(puts, number, range, maxPage, tags, cb) {
let page = Math.floor(Math.random() * (range-1));
puts("Using page "+page);
if(number > range || number<0) {
cb.error = "Invalid range";
cb();
}
else
getLoli(puts, number,range, page, tags, cb);
}
function Config() {
this.range = 5;
this.page = 150;
this.tags = undefined;
this.number = 1;
this.debug = function(){};
}
var cfg = new Config();
exports.defaultConfig = cfg;
exports.Config = Config;
function randomise(cb, conf) {
if(conf)
var cfg = conf;
cfg.debug("Calling with params "+JSON.stringify(cfg));
randomiseWith(cfg.debug, cfg.number, cfg.range, cfg.page, cfg.tags, cb);
}
exports.randomise = randomise;

13
package-lock.json generated

@ -0,0 +1,13 @@
{
"name": "hashloli",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"xmlhttprequest": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz",
"integrity": "sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw="
}
}
}

@ -0,0 +1,18 @@
{
"name": "hashloli",
"version": "1.0.0",
"description": "Loli randomiser",
"main": "loli.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@flanchan.moe:hashloli"
},
"author": "Avril",
"license": "ISC",
"dependencies": {
"xmlhttprequest": "^1.8.0"
}
}

@ -0,0 +1,53 @@
const loli = require("./loli");
var cfg = new loli.Config();
var verbose = false;
var debug = false;
process.argv.forEach(function (val, index, array) {
if(index>1) {
if (val == "--verbose")
verbose = true;
else if(val == "--debug")
debug = true;
else if (/^--number=/.test(val))
cfg.number = val.replace(/^--number=/, "");
else if (/^--range=/.test(val))
cfg.range = val.replace(/^--range=/, "");
else if (/^--page=/.test(val))
cfg.page = val.replace(/^--page=/, "");
else {
if(!cfg.tags)
cfg.tags = [];
cfg.tags.push(val);
}
}
});
function puts(str) {
console.log("DBG "+str);
}
if (debug)
cfg.debug = puts;
if (cfg.number > cfg.range)
cfg.range = cfg.number;
loli.randomise(function(datas) {
if (this.error) {
if(verbose)
console.log(JSON.stringify([{error: this.error}]));
else if(debug) console.log("Error: "+this.error);
}
else if(verbose)
console.log(JSON.stringify(datas));
else {
datas.forEach(function(data, i, a) {
if(data)
var url = data.file_url;
console.log(url);
});
}
}, cfg);
Loading…
Cancel
Save