Merge pull request #674 from Scritches/master

Bunch of little fixes and style updates
This commit is contained in:
Luke Slater 2018-04-14 20:14:57 +01:00 committed by GitHub
commit 944827c4d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 25 deletions

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"');

View File

@ -297,7 +297,7 @@ var commands = function(dbot) {
event.reply(orderedKickLeague(dbot.db.kicks, 'Kicked')); event.reply(orderedKickLeague(dbot.db.kicks, 'Kicked'));
event.reply(orderedKickLeague(dbot.db.kickers, 'Kickers')); event.reply(orderedKickLeague(dbot.db.kickers, 'Kickers'));
}, },
'~votequiet': function(event) { '~votequiet': function(event) {
var target = event.input[1], var target = event.input[1],
reason = event.input[2]; reason = event.input[2];
@ -353,6 +353,11 @@ var commands = function(dbot) {
if(vq.yes.length == 4) { if(vq.yes.length == 4) {
event.reply('Attempt to quiet ' + target + ' succeeded. Count: Yes (' + vq.yes.length + '). No (' + vq.no.length + ').'); event.reply('Attempt to quiet ' + target + ' succeeded. Count: Yes (' + vq.yes.length + '). No (' + vq.no.length + ').');
this.api.quietUser(event.server, event.rUser, '10m', event.channel, target, reason + '[votequiet]', function(response) { this.api.quietUser(event.server, event.rUser, '10m', event.channel, target, reason + '[votequiet]', function(response) {
clearTimeout(vq.timer);
vq.spent = true;
setTimeout(function() {
delete this.voteQuiets[user.id];
}.bind(this), 600000);
event.reply(response); event.reply(response);
}); });
} }
@ -391,6 +396,10 @@ var commands = function(dbot) {
event.reply('Attempt to quiet ' + target + ' succeeded. Count: Yes (' + vq.yes.length + '). No (' + vq.no.length + ').'); event.reply('Attempt to quiet ' + target + ' succeeded. Count: Yes (' + vq.yes.length + '). No (' + vq.no.length + ').');
this.api.quietUser(event.server, event.rUser, '10m', event.channel, target, vq.reason + '[votequiet]', function(response) { this.api.quietUser(event.server, event.rUser, '10m', event.channel, target, vq.reason + '[votequiet]', function(response) {
clearTimeout(vq.timer); clearTimeout(vq.timer);
vq.spent = true;
setTimeout(function() {
delete this.voteQuiets[user.id];
}.bind(this), 600000);
event.reply(response); event.reply(response);
}); });
} }

View File

@ -155,7 +155,7 @@ var lastfm = function(dbot) {
event.reply(dbot.t('lfm_profile', { event.reply(dbot.t('lfm_profile', {
'user': user.currentNick, 'user': user.currentNick,
'plays': profile.playcount.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"), 'plays': profile.playcount.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"),
'date': moment(profile.registered['#text']).format('DD/MM/YYYY'), 'date': moment(profile.registered['#text'] * 1000).format('DD/MM/YYYY'),
'link': profile.url 'link': profile.url
})); }));
} else { } else {

View File

@ -27,7 +27,7 @@ var link = function(dbot) {
page = request(link.replace('https', 'http'), function(error, response, body) { page = request(link.replace('https', 'http'), function(error, response, body) {
if(!error && response.statusCode == 200) { if(!error && response.statusCode == 200) {
body = body.replace(/(\r\n|\n\r|\n)/gim, " "); body = body.replace(/(\r\n|\n\r|\n)/gim, " ");
var title = body.valMatch(/<title>(.*?)<\/title>/, 2); var title = body.valMatch(/<title>(.*?)<\\?\/title>/, 2);
if(title && title.length < 140) { if(title && title.length < 140) {
callback(ent.decode(title[1]).trim()); callback(ent.decode(title[1]).trim());
} }