Skip to content

Commit

Permalink
add seperate setConfig command and use yargs commands better
Browse files Browse the repository at this point in the history
  • Loading branch information
jkerr321 committed Apr 28, 2019
1 parent d516d82 commit 24e735b
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 51 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ Requirements
Usage
-----

The first time you use the tool you will need to pass in your Good Reads API key, your Good Reads user ID, and a value for an Amazon cookie. These will be stored so you won't need to provide them again, unless you want to update their values:
### Setting config values

The first time you use the tool you will need to pass in your Good Reads API key, your Good Reads user ID, and a value for an Amazon cookie using the `setConfig` command. These will be stored so you won't need to provide them again. You can use the `setConfig` command at any time to update any of these values.

```
$ npx tsundoku --goodReadsId=<your_id> --goodReadsKey=<your_key> --cookie=<cookie_value>
$ npx tsundoku setConfig --goodReadsId=<your_id> --goodReadsKey=<your_key> --cookie=<cookie_value>
```

**Note:** wrap the cookie value in single quotes (`'`) when you pass it into the `cookie` command - this will stop `bash` expanding any of the `=` in the cookie into variables which would cause the script to error

Thereafter you can run the tool without any arguments passed in:
### Running the tool

Once you've set the config values you can run Tsundoku to output your price list csv file using the `run` command:

```
$ npx tsundoku
$ npx tsundoku run
```

The output is a csv file in the module root - the name of the file will be logged to your terminal and you should be able to open it by doing `command + click` on the filename (on Mac) or `control + click` on the filename (on Windows)
Expand Down
22 changes: 2 additions & 20 deletions bin/tsundoku.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
#!/usr/bin/env node
const fs = require('fs');
const argv = require('yargs').argv;
const { init } = require('../server/index')
const yargs = require('yargs');

if (argv.goodReadsId && argv.goodReadsKey && argv.cookie) {
const config = {
GOODREADS_USER: argv.goodReadsId,
GOODREADS_KEY: argv.goodReadsKey,
COOKIE: argv.cookie
}

// add config vars to config file
fs.writeFileSync(`${__dirname}/../config.js`, `module.exports = ${JSON.stringify(config)}`, err => {
if (err) console.error(`error writing config file: ${error}`);
else console.log('updated config with your options');
})
}

//TODO error handling

init();
yargs.commandDir('../server/commands').argv;
6 changes: 5 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module.exports = { "GOODREADS_USER": "xxxxxxxxxxxxx", "GOODREADS_KEY": "xxxxxxxxxxxxxxxxxxxxxxx", "COOKIE": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }
module.exports = {
"GOODREADS_USER": "",
"GOODREADS_KEY": "",
"COOKIE": ""
}
96 changes: 74 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,76 @@
{
"name": "tsundoku",
"version": "3.0.3",
"description": "",
"bin": "./bin/tsundoku.js",
"author": "jennifer johnson",
"repository": {
"type": "git",
"url": "https://github.com/jkerr321/tsundoku.git"
},
"license": "MIT",
"dependencies": {
"async-sema": "^3.0.0",
"cheerio": "^1.0.0-rc.2",
"node-fetch": "^2.3.0",
"nodemon": "^1.18.5",
"xml-js": "^1.6.8",
"yargs": "^13.2.2"
},
"devDependencies": {},
"scripts": {
"start": "node server/index.js"
}
"_from": "tsundoku",
"_id": "[email protected]",
"_inBundle": false,
"_integrity": "sha512-/HxPrAfN+CR9jKNOgus9kMl2FrrA6RCEcDzyTW3U4lh543SCAicb1qMR0qCDxwFrsFOVvmHABZtBXQjGGyNI7w==",
"_location": "/tsundoku",
"_phantomChildren": {
"decamelize": "1.2.0",
"emoji-regex": "7.0.3",
"is-stream": "1.1.0",
"locate-path": "3.0.0",
"mem": "4.3.0",
"nice-try": "1.0.5",
"npm-run-path": "2.0.2",
"p-finally": "1.0.0",
"path-key": "2.0.1",
"pump": "3.0.0",
"require-directory": "2.1.1",
"set-blocking": "2.0.0",
"shebang-command": "1.2.0",
"signal-exit": "3.0.2",
"strip-eof": "1.0.0",
"which": "1.2.12",
"wrap-ansi": "2.1.0"
},
"_requested": {
"type": "tag",
"registry": true,
"raw": "tsundoku",
"name": "tsundoku",
"escapedName": "tsundoku",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/tsundoku/-/tsundoku-3.1.0.tgz",
"_shasum": "095f9afeeb7df643c69d8827430e9f29f5390832",
"_spec": "tsundoku",
"_where": "/Users/jenniferkerr/projects",
"author": {
"name": "jennifer johnson"
},
"bin": {
"tsundoku": "./bin/tsundoku.js"
},
"bugs": {
"url": "https://github.com/jkerr321/tsundoku/issues"
},
"bundleDependencies": false,
"dependencies": {
"async-sema": "^3.0.0",
"cheerio": "^1.0.0-rc.2",
"node-fetch": "^2.3.0",
"nodemon": "^1.18.5",
"xml-js": "^1.6.8",
"yargs": "^13.2.2"
},
"deprecated": false,
"description": "Tsundoku ========== A tool that outputs the books in your Goodreads `To Read` list in a csv file, along with their Amazon prices for different formats.",
"devDependencies": {},
"homepage": "https://github.com/jkerr321/tsundoku#readme",
"license": "MIT",
"name": "tsundoku",
"repository": {
"type": "git",
"url": "git+https://github.com/jkerr321/tsundoku.git"
},
"scripts": {
"start": "node server/index.js"
},
"version": "3.1.0"
}
9 changes: 9 additions & 0 deletions server/commands/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { init } = require('../index');

exports.command = 'run';

exports.describe = 'get goodReads book list - you need to add your config details first';

exports.handler = (argv = {}) => {
init();
}
42 changes: 42 additions & 0 deletions server/commands/setConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

const fs = require('fs');
const configFile = require('../../config');

exports.command = 'setConfig [goodReadsId] [goodReadsKey] [cookie]';

exports.describe = 'Search a file within a repository';

exports.builder = yargs => {
return yargs
.positional('goodReadsId', {
type: 'string',
describe: '8 character Good Reads user ID'
})
.positional('goodReadsKey', {
type: 'string',
describe: 'good reads developer API key'
})
.positional('cookie', {
type: 'string',
describe: 'Amazon cookie string'
})
};

exports.handler = (argv = {}) => {
const { goodReadsId, goodReadsKey, cookie } = argv;

let configObj = {};
goodReadsId ? configObj.GOODREADS_USER = goodReadsId : configObj.GOODREADS_USER = configFile.GOODREADS_USER;
goodReadsKey ? configObj.GOODREADS_KEY = goodReadsKey : configObj.GOODREADS_KEY = configFile.GOODREADS_KEY;
cookie ? configObj.COOKIE = cookie : configObj.COOKIE = configFile.COOKIE;

// add config vars to config file
fs.writeFileSync(`${__dirname}/../../config.js`, `module.exports = ${JSON.stringify(configObj)}`, err => {
if (err) console.error(`error writing config file: ${error}`);
else {
console.log('updated config with your options');
}
})

console.log('** Success! Your config has been set **')
}
4 changes: 2 additions & 2 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ const init = async () => {
toReadList.forEach(book => {
if (book.title === amazonPriceObject.title) {
mergedBookData = Object.assign(
{},
book,
{},
book,
{ prices: amazonPriceObject.prices },
{ amazon_link: amazonPriceObject.amazon_link }
);
Expand Down
4 changes: 2 additions & 2 deletions server/lib/csv-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const deleteOldCsv = (csvName) => {
}

const exportToCsv = (booksArray, timestamp) => {
const filename = `${__dirname}/../../${timestamp}.output.csv`;
const filename = `${__dirname}/../../${timestamp}.output.csv`;
const output = [['Title', 'Author', 'Goodreads Rating', 'Kindle Price', 'Hardcover Price New', 'Hardcover Price Used', 'Paperback Price New', 'Paperback Price New', 'link']]; // holds all rows of data

booksArray.forEach((book) => {
Expand All @@ -49,4 +49,4 @@ const exportToCsv = (booksArray, timestamp) => {
fs.writeFileSync(filename, output.join(os.EOL));
}

module.exports = { exportToCsv, deleteOldCsv, getCsvName };
module.exports = {exportToCsv, deleteOldCsv, getCsvName};
7 changes: 7 additions & 0 deletions server/lib/good-reads-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ const convertToJson = async result => {
};

const getBookInfo = async () => {
console.log('==================');
console.log('gothere3');
console.log('==================');
console.log('==================');
console.log('GOODREADS_KEY', GOODREADS_KEY);
console.log('==================');

try {
const apiResponse = await fetch(
`https://www.goodreads.com/review/list/${GOODREADS_USER}.xml?key=${GOODREADS_KEY}&v=2&shelf=to-read&per_page=200&page=1`
Expand Down

0 comments on commit 24e735b

Please sign in to comment.