Got rid of all the const

But muh self-documenting code :'(
This commit is contained in:
Scritches 2018-04-14 15:07:26 -04:00
parent 2d20f19572
commit 97642f35a9

View File

@ -3,12 +3,12 @@
* Description: Interacts with the GoodReads API to provide book-oriented functionality to dbot * Description: Interacts with the GoodReads API to provide book-oriented functionality to dbot
*/ */
const util = require('util'), var util = require('util'),
_ = require('underscore')._, _ = require('underscore')._,
rp = require('request-promise-native'), rp = require('request-promise-native'),
parseString = util.promisify(require('xml2js').parseString); parseString = util.promisify(require('xml2js').parseString);
const GoodReads = function(dbot) { var GoodReads = function(dbot) {
this.apiRoot = 'https://www.goodreads.com'; this.apiRoot = 'https://www.goodreads.com';
this.internalAPI = { this.internalAPI = {
@ -37,7 +37,7 @@ const GoodReads = function(dbot) {
this.api = { this.api = {
'findBook': async term => { 'findBook': async term => {
//https://www.goodreads.com/search/index.xml //https://www.goodreads.com/search/index.xml
const body = await rp({ var body = await rp({
uri: this.apiRoot + '/search/index.xml', uri: this.apiRoot + '/search/index.xml',
qs: { qs: {
key: this.config.api_key, key: this.config.api_key,
@ -45,10 +45,10 @@ const GoodReads = function(dbot) {
} }
}); });
const response = await parseString(body, { explicitArray: false }); var response = await parseString(body, { explicitArray: false });
if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error'; if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error';
const result = response.GoodreadsResponse.search.results; var result = response.GoodreadsResponse.search.results;
if(!result || !_.has(result, 'work')) throw 'book-not-found'; if(!result || !_.has(result, 'work')) throw 'book-not-found';
if(!result.work[0]) throw 'book-not-found'; if(!result.work[0]) throw 'book-not-found';
@ -62,7 +62,7 @@ const GoodReads = function(dbot) {
'getSummaryForBook': async id => { 'getSummaryForBook': async id => {
//https://www.goodreads.com/book/show.xml //https://www.goodreads.com/book/show.xml
const body = await rp({ var body = await rp({
uri: this.apiRoot + '/book/show.xml', uri: this.apiRoot + '/book/show.xml',
qs: { qs: {
key: this.config.api_key, key: this.config.api_key,
@ -70,10 +70,10 @@ const GoodReads = function(dbot) {
} }
}); });
const response = await parseString(body, { explicitArray: false }); var response = await parseString(body, { explicitArray: false });
if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error'; if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error';
const result = response.GoodreadsResponse.book; var result = response.GoodreadsResponse.book;
if(!result) throw 'book-not-found'; if(!result) throw 'book-not-found';
if(!_.has(result, 'description')) throw 'no-description'; if(!_.has(result, 'description')) throw 'no-description';
@ -82,17 +82,17 @@ const GoodReads = function(dbot) {
'findAuthor': async term => { 'findAuthor': async term => {
//https://www.goodreads.com/api/author_url/<ID> //https://www.goodreads.com/api/author_url/<ID>
const body = await rp({ var body = await rp({
url: this.apiRoot + '/api/author_url/' + term, url: this.apiRoot + '/api/author_url/' + term,
qs: { qs: {
key: this.config.api_key key: this.config.api_key
} }
}); });
const response = await parseString(body, {explicitArray: false }); var response = await parseString(body, {explicitArray: false });
if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error'; if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error';
const result = response.GoodreadsResponse.author; var result = response.GoodreadsResponse.author;
if(!result) throw 'author-not-found'; if(!result) throw 'author-not-found';
return { return {
@ -121,10 +121,10 @@ const GoodReads = function(dbot) {
throw e; throw e;
} }
const response = await parseString(body, { explicitArray: false }); var response = await parseString(body, { explicitArray: false });
if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error'; if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error';
const result = response.GoodreadsResponse.user; var result = response.GoodreadsResponse.user;
if(!result) throw 'user-not-found'; if(!result) throw 'user-not-found';
return this.internalAPI.formatProfile(result); return this.internalAPI.formatProfile(result);
@ -150,10 +150,10 @@ const GoodReads = function(dbot) {
throw e; throw e;
} }
const response = await parseString(body, { explicitArray: false }); var response = await parseString(body, { explicitArray: false });
if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error'; if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error';
const result = response.GoodreadsResponse.user; var result = response.GoodreadsResponse.user;
if(!result) throw 'user-not-found'; if(!result) throw 'user-not-found';
return this.internalAPI.formatProfile(result); return this.internalAPI.formatProfile(result);
@ -171,7 +171,7 @@ const GoodReads = function(dbot) {
} }
}); });
const response = await parseString(body, { explicitArray: false }); var response = await parseString(body, { explicitArray: false });
if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error'; if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error';
let result = response.GoodreadsResponse.reviews.review; let result = response.GoodreadsResponse.reviews.review;
@ -193,7 +193,7 @@ const GoodReads = function(dbot) {
this.commands = { this.commands = {
'~book' : async evt => { '~book' : async evt => {
try { try {
const book = await this.api.findBook(evt.input[1]); var book = await this.api.findBook(evt.input[1]);
evt.reply(dbot.t('gr_book', { evt.reply(dbot.t('gr_book', {
author: book.author, author: book.author,
title: book.title, title: book.title,
@ -206,9 +206,8 @@ const GoodReads = function(dbot) {
'~booksummary': async evt => { '~booksummary': async evt => {
try { try {
console.log(evt.input[1]); var book = await this.api.findBook(evt.input[1]);
const book = await this.api.findBook(evt.input[1]); var summary = await this.api.getSummaryForBook(book.id);
const summary = await this.api.getSummaryForBook(book.id);
evt.reply(dbot.t('gr_summary', { evt.reply(dbot.t('gr_summary', {
title: book.title, title: book.title,
summary: summary, summary: summary,
@ -228,7 +227,7 @@ const GoodReads = function(dbot) {
'~reading': async (evt, profile) => { '~reading': async (evt, profile) => {
try { try {
let books = await this.api.getShelfForUserId(profile.id, 'currently-reading'); let books = await this.api.getShelfForUserId(profile.id, 'currently-reading');
const booksCount = books.length; var booksCount = books.length;
if(!booksCount) { if(!booksCount) {
evt.reply(dbot.t('gr_not_reading', { user: evt.rUser.currentNick })); evt.reply(dbot.t('gr_not_reading', { user: evt.rUser.currentNick }));
return; return;
@ -259,7 +258,7 @@ const GoodReads = function(dbot) {
_.each(this.commands, ((cmd, cmdName) => { _.each(this.commands, ((cmd, cmdName) => {
if(cmd.requiresProfile) { if(cmd.requiresProfile) {
this.commands[cmdName] = (async evt => { this.commands[cmdName] = (async evt => {
const grUsername = evt.rProfile.goodreads; var grUsername = evt.rProfile.goodreads;
if(!grUsername) { if(!grUsername) {
evt.reply(evt.rUser.currentNick + ': Set a Goodreads username with "~set goodreads username"'); evt.reply(evt.rUser.currentNick + ': Set a Goodreads username with "~set goodreads username"');