Skip to content

Commit

Permalink
Merge pull request #7 from ezpaarse-project/ezu-command
Browse files Browse the repository at this point in the history
Ezu command
  • Loading branch information
nojhamster authored Jun 23, 2021
2 parents 5affabb + 054f6f2 commit e3093ca
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 98 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,22 @@ Update config to fetch ez-unpyawall.
| --- | --- |
| -g --get | display the configuration |
| -s --set | initialize the configuration file in $HOME/.config |
| -u --url | ezunpaywall url |
| -p --port | ezunpaywall port |
| --ezunpaywallURL | ezunpaywall url |
| --ezunpaywallPort | ezunpaywall port |
| --ezmetaURL | ezmeta url |
| --ezmetaPort | ezmeta port |
| --ezmetaUser | ezmeta url |
| --ezmetaPassword | ezmeta password |
| -k --apikey | admin apikey |
| -l --list | list of attributes required for configuration |
#### Examples
```bash
$ ezunpaywall config --url http://localhost
$ ezunpaywall config --port 8080
$ ezunpaywall config --ezunpaywallURL http://localhost
$ ezunpaywall config --ezunpaywallPort 8080
$ ezunpaywall config --ezmetaURL http://localhost
$ ezunpaywall config --ezmetaPort 9200
$ ezunpaywall config --ezmetaUser elastic
$ ezunpaywall config --ezmetaPassword changeme
$ ezunpaywall config --apikey admin
```
### ezu ping
Expand Down
91 changes: 70 additions & 21 deletions bin/cmds/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,116 @@ const os = require('os');
*/
const setConfig = async () => {
const pathConfig = path.resolve(os.homedir(), '.config', '.ezunpaywallrc');

const config = {
url: 'http://localhost',
port: 8080,
ezunpaywallURL: 'http://localhost',
ezunpaywallPort: 8080,
ezmetaURL: 'http://localhost',
ezmetaPort: 9200,
ezmetaUser: 'elastic',
ezmetaPassword: 'changeme',
apikey: 'admin',
};

try {
await fs.writeFile(pathConfig, JSON.stringify(config, null, 2), 'utf8');
} catch (err) {
console.error(err);
}

console.log(`configuration has been initialized in ${pathConfig}`);
console.log(JSON.stringify(config, null, 2));
};

/**
* config management command to establish the connection between the command and ezunpaywall
* @param {Object} args commander arguments
* @param -g --get - display the configuration
* @param -s --set - initialize the configuration file in $HOME/.config
* @param --url <url> - ezunpaywall url
* @param --port <port> - ezunpaywall port
* @param --apikey <apikey> - admin apikey
* @param -l --list - list of attributes required for configuration
*
* @param {boolean} args.get -g --get - display the configuration
* @param {boolean} args.set -s --set - initialize the configuration file in $HOME/.config
* @param {string} args.ezunpaywallURL --ezunpaywallURL <ezunpaywallURL> - ezunpaywall url
* @param {number} args.ezunpaywallPort --ezunpaywallPort <ezunpaywallPort> - ezunpaywall port
* @param {string} args.ezmetaURL --ezmetalURL <ezmetalURL> - ezmetal url
* @param {number} args.ezmetaPort --ezmetalPort <ezmetalPort> - ezmetal port
* @param {string} args.apikey --apikey <apikey> - admin apikey
* @param {boolean} args.list -l --list - list of attributes required for configuration
*/
const manageConfig = async (args) => {
if (args.list) {
console.log('--url <url> ezunpaywall url');
console.log('--port <port> ezunpaywall port');
console.log('--ezunpaywallURL <ezunpaywallURL> ezunpaywall url');
console.log('--ezunpaywallPort <ezunpaywallPort> ezunpaywall port');
console.log('--ezmetaURL <ezmetaURL> ezmeta url');
console.log('--ezmetaPort <ezmetaPort> ezmeta port');
console.log('--ezmetaUser <ezmetaUser> ezmeta port');
console.log('--ezmetaPassword <ezmetaPassword> ezmeta port');
console.log('-k --apikey <apikey> admin apikey');
process.exit(0);
}

const configPath = path.resolve(os.homedir(), '.config', '.ezunpaywallrc');

if (!await fs.pathExists(configPath)) {
await setConfig();
}

if (args.set) {
await setConfig();
process.exit(0);
}

const config = JSON.parse(await fs.readFile(configPath, 'utf-8'));

if (args.get) {
console.log(JSON.stringify(config, null, 2));
console.log(`from ${configPath}`);
process.exit(0);
}
if (args.url) {
const regexURL = /^(ftp|http|https):\/\/[^ "]+$/;
const valideURL = regexURL.test(args.url);
if (valideURL) {
config.url = args.url;

if (args.ezunpaywallURL) {
const regexURL = /^(http|https):\/\/[^ "]+$/;
const isValidURL = regexURL.test(args.url);
if (isValidURL) {
config.ezunpaywallURL = args.ezunpaywallURL;
} else {
console.error(`'${args.url}' is not a valide URL`);
console.error(`'${args.ezunpaywallURL}' is not a valide URL`);
process.exit(1);
}
}
if (args.port) {
if (Number.isNaN(args.port)) {
console.error(`${args.port} is not a number`);

if (args.ezmetaURL) {
const regexURL = /^(http|https):\/\/[^ "]+$/;
const isValidURL = regexURL.test(args.url);
if (isValidURL) {
config.ezmetaURL = args.ezmetaURL;
} else {
console.error(`'${args.ezmetaURL}' is not a valide URL`);
process.exit(1);
}
config.port = args.port;
}

if (args.ezunpaywallPort) {
if (Number.isNaN(args.ezunpaywallPort)) {
console.error(`${args.ezunpaywallPort} is not a number`);
process.exit(1);
}
config.ezunpaywallPort = args.ezunpaywallPort;
}

if (args.ezmetaPort) {
if (Number.isNaN(args.ezmetaPort)) {
console.error(`${args.ezmetaPort} is not a number`);
process.exit(1);
}
config.ezmetaPort = args.ezmetaPort;
}

if (args.ezmetaUser) {
config.ezmetaUser = args.ezmetaUser;
}

if (args.ezmetaPassword) {
config.ezmetaPassword = args.ezmetaPassword;
}

if (args.apikey) {
config.apikey = args.apikey;
}
Expand Down
61 changes: 37 additions & 24 deletions bin/cmds/enrich.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
/* eslint-disable no-await-in-loop */
/* eslint-disable camelcase */
const axios = require('axios');
const fs = require('fs-extra');
const path = require('path');
const uuid = require('uuid');

const { connection } = require('../../lib/axios');
const { getConfig } = require('../../lib/config');
/**
* start a csv file enrichment
* @param {Object} args commander arguments
* @param -f --file <file> - file which must be enriched
* @param -a --attributes <attributes> - attributes which must be enriched
* in graphql format. By default, all attributes are added
*
* @param -s --separator <separator> - separator of csv out file
* @param -o --out <out> - name of enriched file
* @param -v --verbose - logs how much lines are enriched
* @param -u --use <use> - use a custom config
* @param {string} args.file -f --file <file> - file which must be enriched
* @param {string} args.attributes -a --attributes <attributes> - attributes which must be enriched
* in graphql format. By default, all attributes are added
* @param {string} args.separator -s --separator <separator> - separator of csv out file
* @param {string} args.out -o --out <out> - name of enriched file
* @param {boolean} args.verbose -v --verbose - logs how much lines are enriched
* @param {boolean} args.use -u --use <use> - pathfile of custom config
*/
const enrichCSV = async (args) => {
const axios = await connection(args.use);
const config = await getConfig(args.use);

const ezunpaywall = `${config.ezunpaywallURL}:${config.ezunpaywallPort}`;

if (!args.file) {
console.error('file expected');
process.exit(1);
}

const file = path.resolve(args.file);
const fileExist = await fs.pathExists(file);

if (!fileExist) {
console.error('file not found');
process.exit(1);
}

const ext = path.extname(file).substring(1);

if (ext !== 'csv') {
console.error(`${ext} is not suported for enrichCSV. Required .csv`);
process.exit(1);
Expand All @@ -42,16 +47,18 @@ const enrichCSV = async (args) => {
const query = {
separator: ',',
};

if (args.separator) query.separator = args.separator;
if (args.attributes) query.args = args.attributes;
if (args.index) query.index = args.index;

const id = uuid.v4();
const stat = await fs.stat(args.file);

try {
await axios({
method: 'POST',
url: `/enrich/csv/${id}`,
url: `${ezunpaywall}/enrich/csv/${id}`,
params: query,
data: fs.createReadStream(args.file),
headers: {
Expand All @@ -67,20 +74,22 @@ const enrichCSV = async (args) => {
}

let res2;

while (!res2?.data?.state?.done) {
res2 = await axios({
method: 'GET',
url: `/enrich/state/${id}.json`,
url: `${ezunpaywall}/enrich/state/${id}.json`,
responseType: 'json',
});
await new Promise((resolve) => setTimeout(resolve, 1000));
}

let res3;

try {
res3 = await axios({
method: 'GET',
url: `/enrich/${id}.csv`,
url: `${ezunpaywall}/enrich/${id}.csv`,
responseType: 'stream',
});
} catch (err) {
Expand All @@ -94,25 +103,27 @@ const enrichCSV = async (args) => {

/**
* start a jsonl file enrichment
* @param {Object} args commander arguments
* @param -f --file <file> - file which must be enriched
* @param -a --attributes <attributes> - attributes which must be enriched
* @param {string} args.file -f --file <file> - file which must be enriched
* @param {string} args.attributes -a --attributes <attributes> - attributes which must be enriched
* in graphql format. By default, all attributes are added
*
* @param -o --out <out> - name of enriched file. By default, the output file is named: out.jsonl
* @param -v --verbose - logs how much lines are enriched
* @param -u --use <use> - use a custom config
* @param {string} args.out -o --out <out> - name of enriched file
* @param {boolean} args.verbose -v --verbose - logs how much lines are enriched
* @param {boolean} args.use -u --use <use> - pathfile of custom config
*/
const enrichJSON = async (args) => {
const axios = await connection(args.use);
const config = await getConfig(args.use);

const ezunpaywall = `${config.ezunpaywallURL}:${config.ezunpaywallPort}`;

if (!args.file) {
console.error('file expected');
process.exit(1);
}

const file = path.resolve(args.file);
const fileExist = await fs.pathExists(file);

if (!fileExist) {
console.error('file not found');
process.exit(1);
Expand All @@ -130,10 +141,11 @@ const enrichJSON = async (args) => {

const id = uuid.v4();
const stat = await fs.stat(args.file);

try {
await axios({
method: 'POST',
url: `/enrich/json/${id}`,
url: `${ezunpaywall}/enrich/json/${id}`,
params: query,
data: fs.createReadStream(args.file),
headers: {
Expand All @@ -149,20 +161,22 @@ const enrichJSON = async (args) => {
}

let res2;

while (!res2?.data?.state?.done) {
res2 = await axios({
method: 'GET',
url: `/enrich/state/${id}.json`,
url: `${ezunpaywall}/enrich/state/${id}.json`,
responseType: 'json',
});
await new Promise((resolve) => setTimeout(resolve, 1000));
}

let res3;

try {
res3 = await axios({
method: 'GET',
url: `/enrich/${id}.jsonl`,
url: `${ezunpaywall}/enrich/${id}.jsonl`,
responseType: 'stream',
});
} catch (err) {
Expand All @@ -171,7 +185,6 @@ const enrichJSON = async (args) => {
}

const writer = fs.createWriteStream(out);

res3.data.pipe(writer);
};

Expand Down
Loading

0 comments on commit e3093ca

Please sign in to comment.